Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- namespace Project
- {
- public class Profiler
- {
- public struct CallTreeNode
- {
- public int ParentIndex;
- public string Name;
- public TimeSpan StartDelta;
- public TimeSpan StopDelta;
- }
- public Profiler(TracerCollection tracers)
- {
- tracer = tracers["PROFILE"];
- timer = new Stopwatch();
- callTree = new CallTreeNode[128];
- callTree[0].Name = "Frame";
- callTree[0].ParentIndex = -1;
- callTree[0].StartDelta = TimeSpan.Zero;
- currentNode = 0;
- nextNode = 1;
- }
- TraceSource tracer;
- Stopwatch timer;
- CallTreeNode[] callTree;
- int currentNode, nextNode;
- public void BeginFrame()
- {
- nextNode = 1;
- tracer.TraceEvent(TraceEventType.Start, 0, "Frame t=0");
- }
- public void BeginOperation(string message, params object[] args)
- {
- string caption;
- if (args.Length == 0)
- caption = message;
- else
- caption = string.Format(message, args);
- callTree[nextNode].Name = caption;
- callTree[nextNode].ParentIndex = currentNode;
- currentNode = nextNode;
- nextNode += 1;
- TimeSpan timestamp = timer.Elapsed;
- tracer.TraceEvent(TraceEventType.Start, currentNode, message + " t=" + timestamp.Ticks);
- callTree[currentNode].StartDelta = timestamp;
- }
- public void EndOperation()
- {
- TimeSpan timestamp = timer.Elapsed;
- tracer.TraceEvent(TraceEventType.Stop, currentNode, callTree[currentNode].Name + " t=" + timestamp.Ticks);
- callTree[currentNode].StopDelta = timestamp;
- currentNode = callTree[currentNode].ParentIndex;
- }
- public void EndFrame()
- {
- timer.Stop();
- if (currentNode != 0)
- 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.");
- tracer.TraceEvent(TraceEventType.Stop, 0, "Frame t=" + timer.Elapsed.Ticks);
- callTree[0].StopDelta = timer.Elapsed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement