/// <summary> /// Called at the beginning of a code block that we intend to measure the performance of, /// the returned <see cref="IDisposable"/> should be called at the end of that code block /// for the measurement to persist, failure to do so will result in an exception when calling /// <see cref="GetReport"/> /// </summary> public IDisposable Block(string name) { if (_isDisabled) { return(_doNothing); } else { _overhead.Start(); // Add (or retrieve) a sub-block var subBlock = _current.AddSubBlock(name); // Push the state in the stack _stack.Push(_current); _current = subBlock; var scope = new CodeBlockScope(_overhead, onDispose: (long time) => { subBlock.T += time; // Pop the stack when we're done with a block _current = _stack.Pop(); }); _overhead.Stop(); return(scope); } }
/// <summary> /// Adds a step with the specified name /// </summary> /// <param name="name">The name of the step</param> public CodeBlockInstrumentation AddSubBlock(string name) { return(_dic.GetOrAdd(name, (name) => { var instrumentation = new CodeBlockInstrumentation { N = name }; // Add to the breakdown _breakdown ??= new List <CodeBlockInstrumentation>(); _breakdown.Add(instrumentation); return instrumentation; })); }