Beispiel #1
0
 public Profiler(string rootNodeName)
 {
     RootNode = new ProfilingNode()
     {
         SegmentName = rootNodeName
     };
 }
Beispiel #2
0
        public static ProfilingNode StartNewAt(string name, long ticks)
        {
            var n = new ProfilingNode();

            if (name.IndexOf("[drop]", StringComparison.OrdinalIgnoreCase) > -1)
            {
                n.Drop = true;
                name   = name.Replace("[drop]", "");
            }
            if (name.IndexOf("[isolate]", StringComparison.OrdinalIgnoreCase) > -1)
            {
                n.Isolate = true;
                name      = name.Replace("[isolate]", "");
            }
            n.SegmentName = name.Trim();
            if (ticks > 0)
            {
                n.StartTicks = ticks;
            }
            else
            {
                n.Start();
            }
            return(n);
        }
Beispiel #3
0
 public void AddChild(ProfilingNode n)
 {
     if (n == this)
     {
         throw new InvalidOperationException("You cannot add a parent as a child of itself.");
     }
     CompletedChildren = CompletedChildren ?? new List <ProfilingNode>();
     CompletedChildren.Add(n);
 }
Beispiel #4
0
        public virtual void Start(string segmentName, bool allowRecursion = false)
        {
            if (!allowRecursion && IsRunning(segmentName))
            {
                throw new InvalidOperationException(string.Format("The given profiling segment {0} has already been started, and allowRecursion=false", segmentName));
            }

            callstack.Push(ProfilingNode.StartNew(segmentName));
        }
Beispiel #5
0
        public virtual void StopAt(long ticks, string segmentName, bool assertStarted = true, bool stopChildren = false)
        {
            var normalized_name = ProfilingNode.NormalizeNodeName(segmentName);

            if (stopChildren)
            {
                var topmost = VisibleCallstack.First((n) => n.SegmentName == normalized_name);
                if (topmost != null)
                {
                    var children = VisibleCallstack.TakeWhile((n) => n.SegmentName != normalized_name).ToArray();
                    children.Select((n) => { StopAt(ticks, n.SegmentName, true, false); return(n); });
                    StopAt(ticks, segmentName, assertStarted, false);
                }
                else if (assertStarted)
                {
                    throw new InvalidOperationException(string.Format("The given profiling segment {0} is not running anywhere in the callstack; it cannot be stopped.", normalized_name));
                }
            }
            else
            {
                if (callstack.Peek().SegmentName == normalized_name)
                {
                    var n = callstack.Pop();
                    n.StopAt(ticks);
                    if (n.Drop)
                    {
                        return;
                    }
                    if (callstack.Count > 0)
                    {
                        callstack.Peek().AddChild(n);
                    }
                    else if (RootNode.SegmentName == n.SegmentName && !RootNode.HasChildren)
                    {
                        RootNode = n; //Replace the root node on the very first call, *if* the segment name matches.
                    }
                    else
                    {
                        RootNode.AddChild(n);
                    }
                }
                else if (assertStarted)
                {
                    throw new InvalidOperationException(string.Format("The given profiling segment {0} is not running at the top of the callstack; it cannot be stopped.", normalized_name));
                }
            }
        }
Beispiel #6
0
        public virtual void LogStart(long ticks, string segmentName, bool allowRecursion = false)
        {
            if (!allowRecursion && IsRunning(segmentName))
            {
                throw new InvalidOperationException(string.Format("The given profiling segment {0} has already been started, and allowRecursion=false", segmentName));
            }

            if (VisibleCallstack.Count() > 0)
            {
                var top = VisibleCallstack.First();
                if (top.StartTicks > ticks)
                {
                    throw new InvalidOperationException(string.Format("You cannot log a profiling segment {0} {1} that starts before its parent {1} {2} ", segmentName, ticks, top.SegmentName, top.StartTicks));
                }
            }

            callstack.Push(ProfilingNode.StartNewAt(segmentName, ticks));
        }
Beispiel #7
0
        public bool IsRunning(string segmentName)
        {
            var name = ProfilingNode.NormalizeNodeName(segmentName);

            return(VisibleCallstack.Any((n) => n.SegmentName == name));
        }