public StatSnapshot(Stopwatch sw, bool start) { this = new StatSnapshot(); if (!start) { // end of measurement, first stop timer then collect/count sw.Stop(); Elapsed = sw.Elapsed.Ticks; // NB we exclude forced GC from counters, // by measuring memory before forced GC we could // calculate uncollected garbage Memory = GC.GetTotalMemory(false); } //GC.Collect(2, GCCollectionMode.Forced, true); //GC.WaitForPendingFinalizers(); //GC.Collect(2, GCCollectionMode.Forced, true); //GC.WaitForPendingFinalizers(); Gc0 = GC.CollectionCount(0); Gc1 = GC.CollectionCount(1); Gc2 = GC.CollectionCount(2); if (start) { Memory = GC.GetTotalMemory(false); // start timer after collecting GC stat sw.Restart(); } }
/// <inheritdoc /> public void Dispose() { var statEntry = new StatSnapshot(Stopwatch, false); Interlocked.Exchange(ref _sw, Stopwatch); _statSnapshot.Elapsed = statEntry.Elapsed; _statSnapshot.Gc0 = statEntry.Gc0 - _statSnapshot.Gc0; _statSnapshot.Gc1 = statEntry.Gc1 - _statSnapshot.Gc1; _statSnapshot.Gc2 = statEntry.Gc2 - _statSnapshot.Gc2; _statSnapshot.Memory = statEntry.Memory - _statSnapshot.Memory; var list = Stats.GetOrAdd(CaseName, (s1) => new List <Stat>()); list.Add(this); if (!_silent && !ForceSilence) { if (!_headerIsPrinted) { PrintHeader(null, null, unit: _unit); _headerIsPrinted = true; } Console.WriteLine(ToString()); } }
/// <inheritdoc /> public void Dispose() { var statEntry = new StatSnapshot(_stopwatch, false); Interlocked.Exchange(ref _sw, _stopwatch); _statSnapshot._elapsed = statEntry._elapsed; _statSnapshot._gc0 = statEntry._gc0 - _statSnapshot._gc0 - 2; _statSnapshot._gc1 = statEntry._gc1 - _statSnapshot._gc1 - 2; _statSnapshot._gc2 = statEntry._gc2 - _statSnapshot._gc2 - 2; _statSnapshot._memory = statEntry._memory - _statSnapshot._memory; var list = _stats.GetOrAdd(_caseName, (s1) => new List <Stat>()); list.Add(this); if (!_silent && !ForceSilence) { if (!_headerIsPrinted) { PrintHeader(null, null); _headerIsPrinted = true; } Console.WriteLine(ToString()); } }
internal Stat(string caseName, Stopwatch sw, int innerLoopCount, bool silent = false) { _caseName = caseName; _stopwatch = sw; _innerLoopCount = innerLoopCount; _silent = silent; _statSnapshot = new StatSnapshot(_stopwatch, true); }
internal Stat(string caseName, Stopwatch sw, long innerLoopCount, bool silent = false, string unit = null) { CaseName = caseName; Stopwatch = sw; InnerLoopCount = innerLoopCount; _silent = silent; _unit = unit; _statSnapshot = new StatSnapshot(Stopwatch, true); }