Advertisement
Guest User

Profiler.cs

a guest
Jun 6th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. namespace Project
  5. {
  6.     public class Profiler
  7.     {
  8.         public struct CallTreeNode
  9.         {
  10.             public int ParentIndex;
  11.             public string Name;
  12.             public TimeSpan StartDelta;
  13.             public TimeSpan StopDelta;
  14.         }
  15.  
  16.         public Profiler(TracerCollection tracers)
  17.         {
  18.             tracer = tracers["PROFILE"];
  19.             timer = new Stopwatch();
  20.  
  21.             callTree = new CallTreeNode[128];
  22.  
  23.             callTree[0].Name = "Frame";
  24.             callTree[0].ParentIndex = -1;
  25.             callTree[0].StartDelta = TimeSpan.Zero;
  26.  
  27.             currentNode = 0;
  28.             nextNode = 1;
  29.         }
  30.  
  31.         TraceSource tracer;
  32.         Stopwatch timer;
  33.  
  34.         CallTreeNode[] callTree;
  35.         int currentNode, nextNode;
  36.  
  37.         public void BeginFrame()
  38.         {
  39.             nextNode = 1;
  40.  
  41.             tracer.TraceEvent(TraceEventType.Start, 0, "Frame t=0");
  42.         }
  43.  
  44.         public void BeginOperation(string message, params object[] args)
  45.         {
  46.             string caption;
  47.             if (args.Length == 0)
  48.                 caption = message;
  49.             else
  50.                 caption = string.Format(message, args);
  51.  
  52.             callTree[nextNode].Name = caption;
  53.             callTree[nextNode].ParentIndex = currentNode;
  54.  
  55.             currentNode = nextNode;
  56.             nextNode += 1;
  57.  
  58.             TimeSpan timestamp = timer.Elapsed;
  59.             tracer.TraceEvent(TraceEventType.Start, currentNode, message + " t=" + timestamp.Ticks);
  60.             callTree[currentNode].StartDelta = timestamp;
  61.         }
  62.  
  63.         public void EndOperation()
  64.         {
  65.             TimeSpan timestamp = timer.Elapsed;
  66.  
  67.             tracer.TraceEvent(TraceEventType.Stop, currentNode, callTree[currentNode].Name + " t=" + timestamp.Ticks);
  68.             callTree[currentNode].StopDelta = timestamp;
  69.  
  70.             currentNode = callTree[currentNode].ParentIndex;
  71.         }
  72.  
  73.         public void EndFrame()
  74.         {
  75.             timer.Stop();
  76.  
  77.             if (currentNode != 0)
  78.                 throw new InvalidOperationException("All logical operations have to end before the next frame. Please make sure all calls to BeginOperation are matched with a call to EndOperation.");
  79.  
  80.             tracer.TraceEvent(TraceEventType.Stop, 0, "Frame t=" + timer.Elapsed.Ticks);
  81.             callTree[0].StopDelta = timer.Elapsed;
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement