Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- namespace Infrastructure
- {
- public class RetardedProfiler
- {
- public static void Reset()
- {
- foreach (var step in steps)
- step.Value.Stopwatch.Stop();
- steps.Clear();
- }
- public static IDisposable Step(string name)
- {
- InternalStep step;
- if (!steps.TryGetValue(name, out step))
- {
- step = new InternalStep(new Stopwatch());
- steps[name] = step;
- }
- step.Count++;
- step.Stopwatch.Start();
- return step;
- }
- public static string GetResults()
- {
- var sorted = steps.OrderByDescending(x => x.Value.Stopwatch.Elapsed)
- .ToList();
- var sb = new StringBuilder();
- sorted.ForEach(x => sb.AppendFormat("{0}: {1:.###} ({2}){3}", x.Key, x.Value.Stopwatch.Elapsed.TotalSeconds, x.Value.Count, Environment.NewLine));
- return sb.ToString();
- }
- private static readonly IDictionary<string, InternalStep> steps = new ConcurrentDictionary<string, InternalStep>();
- private class InternalStep : IDisposable
- {
- public int Count { get; set; }
- public Stopwatch Stopwatch { get; private set; }
- public InternalStep(Stopwatch sw)
- {
- Count = 0;
- Stopwatch = sw;
- }
- public void Dispose()
- {
- Stopwatch.Stop();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement