Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace Infrastructure
  9. {
  10. public class RetardedProfiler
  11. {
  12. public static void Reset()
  13. {
  14. foreach (var step in steps)
  15. step.Value.Stopwatch.Stop();
  16.  
  17. steps.Clear();
  18. }
  19.  
  20. public static IDisposable Step(string name)
  21. {
  22. InternalStep step;
  23. if (!steps.TryGetValue(name, out step))
  24. {
  25. step = new InternalStep(new Stopwatch());
  26. steps[name] = step;
  27. }
  28.  
  29. step.Count++;
  30. step.Stopwatch.Start();
  31. return step;
  32. }
  33.  
  34. public static string GetResults()
  35. {
  36. var sorted = steps.OrderByDescending(x => x.Value.Stopwatch.Elapsed)
  37. .ToList();
  38. var sb = new StringBuilder();
  39. sorted.ForEach(x => sb.AppendFormat("{0}: {1:.###} ({2}){3}", x.Key, x.Value.Stopwatch.Elapsed.TotalSeconds, x.Value.Count, Environment.NewLine));
  40. return sb.ToString();
  41. }
  42.  
  43. private static readonly IDictionary<string, InternalStep> steps = new ConcurrentDictionary<string, InternalStep>();
  44.  
  45. private class InternalStep : IDisposable
  46. {
  47. public int Count { get; set; }
  48. public Stopwatch Stopwatch { get; private set; }
  49.  
  50. public InternalStep(Stopwatch sw)
  51. {
  52. Count = 0;
  53. Stopwatch = sw;
  54. }
  55.  
  56. public void Dispose()
  57. {
  58. Stopwatch.Stop();
  59. }
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement