Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Stopwatch watch = Stopwatch.StartNew();
- // Do work
- watch.Stop();
- // elapsed time is in watch.Elapsed
- using System;
- using System.Diagnostics;
- using System.Linq;
- static class Test
- {
- static void Main()
- {
- var processorCategory = PerformanceCounterCategory.GetCategories()
- .FirstOrDefault(cat => cat.CategoryName == "Processor");
- var countersInCategory = processorCategory.GetCounters("_Total");
- DisplayCounter(countersInCategory.First(cnt => cnt.CounterName == "% Processor Time"));
- }
- private static void DisplayCounter(PerformanceCounter performanceCounter)
- {
- while (!Console.KeyAvailable)
- {
- Console.WriteLine("{0}t{1} = {2}",
- performanceCounter.CategoryName, performanceCounter.CounterName, performanceCounter.NextValue());
- System.Threading.Thread.Sleep(1000);
- }
- }
- }
- private static EnterpriseLibraryPerformanceCounter averageRequestTimeCounter = PerformanceCounterManager.GetEnterpriseLibraryCounter(MadPerformanceCountersListener.AverageRequestTime);
- private static EnterpriseLibraryPerformanceCounter averageRequestTimeCounterBase = PerformanceCounterManager.GetEnterpriseLibraryCounter(MadPerformanceCountersListener.AverageRequestTimeBase);
- public void DoSomethingWeWantToMonitor()
- {
- using (new AverageTimeMeter(averageRequestTimeCounter, averageRequestTimeCounterBase))
- {
- // code here that you want to perf mon
- }
- }
- public sealed class AverageTimeMeter : IDisposable
- {
- private EnterpriseLibraryPerformanceCounter averageCounter;
- private EnterpriseLibraryPerformanceCounter baseCounter;
- private Stopwatch stopWatch;
- private string instanceName;
- public AverageTimeMeter(EnterpriseLibraryPerformanceCounter averageCounter, EnterpriseLibraryPerformanceCounter baseCounter, string instanceName = null)
- {
- this.stopWatch = new Stopwatch();
- this.averageCounter = averageCounter;
- this.baseCounter = baseCounter;
- this.instanceName = instanceName;
- this.stopWatch.Start();
- }
- public void Dispose()
- {
- this.stopWatch.Stop();
- if (this.baseCounter != null)
- {
- this.baseCounter.Increment();
- }
- if (this.averageCounter != null)
- {
- if (string.IsNullOrEmpty(this.instanceName))
- {
- this.averageCounter.IncrementBy(this.stopWatch.ElapsedTicks);
- }
- else
- {
- this.averageCounter.SetValueFor(this.instanceName, this.averageCounter.Value + this.stopWatch.ElapsedTicks);
- }
- }
- }
- }
- using System.Diagnostics;
- using System.Threading;
- public static T Profile<T>(Func<T> codeBlock, string description = "")
- {
- Stopwatch stopWatch = new Stopwatch();
- stopWatch.Start();
- T res = codeBlock();
- stopWatch.Stop();
- TimeSpan ts = stopWatch.Elapsed;
- const double thresholdSec = 2;
- double elapsed = ts.TotalSeconds;
- if(elapsed > thresholdSec)
- System.Diagnostics.Debug.Write(description + " code was too slow! It took " +
- elapsed + " second(s).");
- return res;
- }
- Profile(() => MyObj.MySlowMethod());
- Profile(() => MyObj.MySlowMethod(), "I can explain why");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement