public ProfiledSegment(ProfiledSegment parent, string name) { this.parent = parent; this.name = name; stopwatch = new Stopwatch(); children = new ProfiledSegmentCollection(); }
private static void Append(StringBuilder sb, int indent, ProfiledSegment segmentParent, TimeSpan threshold) { var totalElapsed = TimeSpan.Zero; foreach (var segment in segmentParent.children.OrderByDescending(s => s.stopwatch.Elapsed)) { if (segment.stopwatch.Elapsed < threshold) { continue; } totalElapsed += segment.stopwatch.Elapsed; var elapsedProportion = segmentParent.parent == null ? 1 : segment.stopwatch.Elapsed.TotalMilliseconds / segmentParent.stopwatch.Elapsed.TotalMilliseconds; var color = levelColors[indent % levelColors.Length]; sb.AppendLineFormat ( "<color=#{0}>{1}<b>{2}:</b> {3:0.0}ms ({4:0}%) ({5}x, ~{6:0.000}ms/x)</color>", color.ToHexString(), new string(' ', indent * 4), segment.name, segment.stopwatch.Elapsed.TotalMilliseconds, elapsedProportion * 100, segment.calls, segment.stopwatch.Elapsed.TotalMilliseconds / segment.calls ); indent++; Append(sb, indent, segment, threshold); indent--; } if (segmentParent.children.Count > 0) { var remainingElapsed = (segmentParent.stopwatch.Elapsed - totalElapsed); if (remainingElapsed > threshold) { var remainingElapsedProportion = remainingElapsed.TotalMilliseconds / segmentParent.stopwatch.Elapsed.TotalMilliseconds; var color = levelColors[indent % levelColors.Length]; sb.AppendLineFormat ( "<color=#{4}>{0}<b>{1}:</b> {2:0.0}ms ({3:0}%)</color>", new string(' ', indent * 4), "Remaining", remainingElapsed.TotalMilliseconds, remainingElapsedProportion * 100, color.ToHexString() ); } } }
public static void EndSample() { currentSegment.stopwatch.Stop(); if (currentSegment.parent != null) { currentSegment = currentSegment.parent; } if (UnityThread.allowsAPI) { Profiler.EndSample(); } }
public static void BeginSample(string name) { if (!currentSegment.children.Contains(name)) { currentSegment.children.Add(new ProfiledSegment(currentSegment, name)); } currentSegment = currentSegment.children[name]; currentSegment.calls++; currentSegment.stopwatch.Start(); if (UnityThread.allowsAPI) { Profiler.BeginSample(name); } }