//--------------------------------------------------------------- #endregion //--------------------------------------------------------------- //--------------------------------------------------------------- #region Methods //--------------------------------------------------------------- /// <summary> /// Resets the gathered profiling data. /// </summary> public void Reset() { max = new ProfilingSample("Root"); current = new ProfilingSample("Root"); min = new ProfilingSample("Root"); total = new ProfilingSample("Root"); }
/// <summary> /// Update the profiler data. /// </summary> /// <param name="sample"></param> public void UpdateProfilerData(ProfilingSample sample) { total.DoAdd(sample); min.DoMin(sample); max.DoMax(sample); current.DoAdd(sample); }
//--------------------------------------------------------------- #endregion //--------------------------------------------------------------- //--------------------------------------------------------------- #region Initialisation //--------------------------------------------------------------- /// <summary> /// Creates a new instance of a <see cref="ProfilingSample"/>. /// </summary> /// <param name="name">Name of profiler sample</param> internal ProfilingSample(string name) { Name = name; Reset(); Parent = null; Children = new ArrayList(); }
private void WriteSamples(StringBuilder builder, ProfilingSample total, ProfilingSample current, ProfilingSample min, ProfilingSample max, string indent, bool currentData) { string formatString = ""; if (!currentData) { formatString = "{8}{0," + (-sizeName + indent.Length) + "}{1," + sizeCalls + "}{2," + sizeColumn + "}({3," + sizePercent + ":##0.0}){4," + sizeColumn + "}({5," + sizePercent + ":##0.0}){6," + sizeColumn + "}{7," + sizeColumn + "}"; } else { formatString = "{10}{0," + (-sizeName + indent.Length) + "}{1," + sizeCalls + "}{2," + sizeColumn + "}({3," + sizePercent + ":##0.0}){4," + sizeColumn + "}({5," + sizePercent + ":##0.0}){6," + sizeColumn + "}({7," + sizePercent + ":##0.0}){8," + sizeColumn + "}{9," + sizeColumn + "}"; } if (total.NumberOfCalls == 0) { total.NumberOfCalls = 1; } float percentageTotal = total.TotalTime * 100.0f / total.Root.TotalTime; float percentage = total.TotalTime * 100.0f / (total.Root.TotalTime * total.NumberOfCalls); float percentageCurrent = current.TotalTime * 100.0f / (current.Root.TotalTime); if (!currentData) { builder.AppendFormat(formatString, total.Name, total.NumberOfCalls, Counter.CalcTime(total.TotalTime), percentageTotal, Counter.CalcTime(total.TotalTime / total.NumberOfCalls), percentage, Counter.CalcTime(min.TotalTime), Counter.CalcTime(max.TotalTime), indent); builder.Append(System.Environment.NewLine); } else { builder.AppendFormat(formatString, total.Name, total.NumberOfCalls, Counter.CalcTime(total.TotalTime), percentageTotal, Counter.CalcTime(current.TotalTime), percentageCurrent, Counter.CalcTime(total.TotalTime / total.NumberOfCalls), percentage, Counter.CalcTime(min.TotalTime), Counter.CalcTime(max.TotalTime), indent); builder.Append(System.Environment.NewLine); } for (int i = 0; i < total.Children.Count; i++) { WriteSamples(builder, (ProfilingSample)total.Children[i], (ProfilingSample)current.Children[i], (ProfilingSample)min.Children[i], (ProfilingSample)max.Children[i], indent + " ", currentData); (current.Children[i] as ProfilingSample).Reset(); } }
/// <summary> /// creates a clone of the profilerSample (without children) /// </summary> /// <returns></returns> public ProfilingSample Clone() { ProfilingSample ret = new ProfilingSample(this.Name); ret.StartTime = StartTime; ret.TotalTime = TotalTime; ret.OpenCounter = OpenCounter; ret.NumberOfCalls = NumberOfCalls; ret.Parent = null; return(ret); }
public void End(string name) { System.Diagnostics.Debug.Assert(current.Name == name, "Profiler End (" + name + ") != Begin (" + current.Name + ")"); System.Diagnostics.Debug.Assert(current.OpenCounter == 1, "End has to be called as often as Begin!"); // update ProfilerSample current.OpenCounter--; ulong time = Counter.GetElapsed(current.StartTime, Counter.Instance.Count); current.TotalTime += time; current = current.Parent; }
/// <summary> /// adds the values of the sample to the current sample /// </summary> /// <param name="sample">sample to add</param> public void DoAdd(ProfilingSample sample) { this.TotalTime += sample.TotalTime; this.NumberOfCalls += sample.NumberOfCalls; foreach (ProfilingSample s in sample.Children) { if (!this.Contains(s.Name)) { this.Add(new ProfilingSample(s.Name)); } this[s.Name].DoAdd(s); } }
/// <summary> /// tests if new sample is new min /// </summary> /// <param name="sample">sample to test for new min</param> public void DoMin(ProfilingSample sample) { if (this.TotalTime > sample.TotalTime) { this.TotalTime = sample.TotalTime; this.NumberOfCalls = sample.NumberOfCalls; } foreach (ProfilingSample s in sample.Children) { if (!this.Contains(s.Name)) { this.Add(new ProfilingSample(s.Name)); this[s.Name].TotalTime = 0xFFFFFFFF; } this[s.Name].DoMin(s); } }
/// <summary> /// Ends recording a new frame. /// </summary> /// <remarks> /// Usually called for every frame -> fires event containing data. /// Mustn't be called between Begin and End. /// </remarks> /// <returns> /// Returns the time within the frame. /// </returns> public int EndFrame() { System.Diagnostics.Debug.Assert(current.Name == "Root", "Flush mustn't be called between Begin and End!"); current.TotalTime = Counter.GetElapsed(current.StartTime, Counter.Instance.Count); int ret = (int)current.TotalTime; // just create new objects if the PROFILE is enabled #if PROFILE // fire event if (ProfilerCallback != null) { ProfilerCallback(current); } // reset current = new ProfilingSample("Root"); #endif return(ret); }
public void Begin(string name) { ProfilingSample sample = null; if (current.Contains(name)) { sample = current[name]; System.Diagnostics.Debug.Assert(sample.OpenCounter == 0, "Begin has to be called as often as End!"); } else { sample = new ProfilingSample(name); // initialize sample and link current.Children.Add(sample); sample.Parent = current; } sample.OpenCounter++; sample.NumberOfCalls++; sample.StartTime = Counter.Instance.Count; current = sample; }
/// <summary> /// adds a profilerSample to the child list /// </summary> /// <param name="sample"></param> public void Add(ProfilingSample sample) { Children.Add(sample); sample.Parent = this; }