All pastes #1463513 Raw Edit

Profiler with stack

public text v1 · immutable
#1463513 ·published 2009-06-17 14:58 UTC
rendered paste body
Index: src/DbLinq/Util/Profiler.cs
===================================================================
--- src/DbLinq/Util/Profiler.cs	(revision 1170)
+++ src/DbLinq/Util/Profiler.cs	(working copy)
@@ -11,30 +11,67 @@
 #endif
     static class Profiler
     {
+        [ThreadStatic]
         private static Stopwatch timer = new Stopwatch();
+
+        [ThreadStatic]
         private static long prevTicks;
 
+        [ThreadStatic]
+        private static Stack<string> stack = new Stack<string>();
+
+        [ThreadStatic]
+        private static bool profiling;
+
         [Conditional("DEBUG")]
         public static void Start()
         {
+            prevTicks = 0;
+            profiling = true;
             timer.Reset();
             timer.Start();
-            prevTicks = 0;
         }
 
         [Conditional("DEBUG")]
+        public static void Begin(string format, params object[] args)
+        {
+            if (profiling)
+            {
+                timer.Stop();
+                string message = string.Format(format, args);
+                stack.Push(message);
+                At("START " + message);
+            }
+        }
+
+        [Conditional("DEBUG")]
+        public static void End()
+        {
+            if (profiling)
+            {
+                timer.Stop();
+                string message = stack.Pop();
+                At("END " + message);
+            }
+        }
+
+        [Conditional("DEBUG")]
         public static void At(string format, params object[] args)
         {
-            timer.Stop();
-            Console.Write("#AT(time={0:D10}, elapsed={1:D10}) ", timer.ElapsedTicks, timer.ElapsedTicks - prevTicks);
-            prevTicks = timer.ElapsedTicks;
-            Console.WriteLine(format, args);
-            timer.Start();
+            if (profiling)
+            {
+                timer.Stop();
+                Console.Write("#AT(time={0:D10}, elapsed={1:D10}) ", timer.ElapsedTicks, timer.ElapsedTicks - prevTicks);
+                prevTicks = timer.ElapsedTicks;
+                Console.WriteLine(format, args);
+                timer.Start();
+            }
         }
 
         [Conditional("DEBUG")]
         public static void Stop()
         {
+            profiling = false;
             timer.Stop();
         }
     }