Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. Stopwatch watch = Stopwatch.StartNew();
  2.  
  3. // Do work
  4.  
  5. watch.Stop();
  6. // elapsed time is in watch.Elapsed
  7.  
  8. using System;
  9. using System.Diagnostics;
  10. using System.Linq;
  11.  
  12. static class Test
  13. {
  14. static void Main()
  15. {
  16. var processorCategory = PerformanceCounterCategory.GetCategories()
  17. .FirstOrDefault(cat => cat.CategoryName == "Processor");
  18. var countersInCategory = processorCategory.GetCounters("_Total");
  19.  
  20. DisplayCounter(countersInCategory.First(cnt => cnt.CounterName == "% Processor Time"));
  21. }
  22.  
  23. private static void DisplayCounter(PerformanceCounter performanceCounter)
  24. {
  25. while (!Console.KeyAvailable)
  26. {
  27. Console.WriteLine("{0}t{1} = {2}",
  28. performanceCounter.CategoryName, performanceCounter.CounterName, performanceCounter.NextValue());
  29. System.Threading.Thread.Sleep(1000);
  30. }
  31. }
  32. }
  33.  
  34. private static EnterpriseLibraryPerformanceCounter averageRequestTimeCounter = PerformanceCounterManager.GetEnterpriseLibraryCounter(MadPerformanceCountersListener.AverageRequestTime);
  35. private static EnterpriseLibraryPerformanceCounter averageRequestTimeCounterBase = PerformanceCounterManager.GetEnterpriseLibraryCounter(MadPerformanceCountersListener.AverageRequestTimeBase);
  36.  
  37. public void DoSomethingWeWantToMonitor()
  38. {
  39. using (new AverageTimeMeter(averageRequestTimeCounter, averageRequestTimeCounterBase))
  40. {
  41. // code here that you want to perf mon
  42. }
  43. }
  44.  
  45. public sealed class AverageTimeMeter : IDisposable
  46. {
  47. private EnterpriseLibraryPerformanceCounter averageCounter;
  48. private EnterpriseLibraryPerformanceCounter baseCounter;
  49. private Stopwatch stopWatch;
  50. private string instanceName;
  51.  
  52. public AverageTimeMeter(EnterpriseLibraryPerformanceCounter averageCounter, EnterpriseLibraryPerformanceCounter baseCounter, string instanceName = null)
  53. {
  54. this.stopWatch = new Stopwatch();
  55. this.averageCounter = averageCounter;
  56. this.baseCounter = baseCounter;
  57. this.instanceName = instanceName;
  58. this.stopWatch.Start();
  59. }
  60.  
  61. public void Dispose()
  62. {
  63. this.stopWatch.Stop();
  64. if (this.baseCounter != null)
  65. {
  66. this.baseCounter.Increment();
  67. }
  68.  
  69. if (this.averageCounter != null)
  70. {
  71. if (string.IsNullOrEmpty(this.instanceName))
  72. {
  73. this.averageCounter.IncrementBy(this.stopWatch.ElapsedTicks);
  74. }
  75. else
  76. {
  77. this.averageCounter.SetValueFor(this.instanceName, this.averageCounter.Value + this.stopWatch.ElapsedTicks);
  78. }
  79. }
  80. }
  81.  
  82. }
  83.  
  84. using System.Diagnostics;
  85. using System.Threading;
  86.  
  87. public static T Profile<T>(Func<T> codeBlock, string description = "")
  88. {
  89. Stopwatch stopWatch = new Stopwatch();
  90. stopWatch.Start();
  91. T res = codeBlock();
  92. stopWatch.Stop();
  93. TimeSpan ts = stopWatch.Elapsed;
  94. const double thresholdSec = 2;
  95. double elapsed = ts.TotalSeconds;
  96. if(elapsed > thresholdSec)
  97. System.Diagnostics.Debug.Write(description + " code was too slow! It took " +
  98. elapsed + " second(s).");
  99. return res;
  100. }
  101.  
  102. Profile(() => MyObj.MySlowMethod());
  103.  
  104. Profile(() => MyObj.MySlowMethod(), "I can explain why");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement