Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PerformanceReport.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Collections.ObjectModel;
6 using System.Diagnostics;
7 using System.Linq;
8 using System.Text;
9 
10 namespace SiliconStudio.Core.Diagnostics
11 {
12  public class PerformanceCheckBlock : IDisposable
13  {
14  private readonly PerformanceReport report;
15 
16  public PerformanceCheckBlock(string text, PerformanceReport report)
17  {
18  if (string.IsNullOrWhiteSpace(text))
19  throw new ArgumentException("Invalid 'text' argument");
20  if (report == null)
21  throw new ArgumentNullException("report");
22 
23  this.report = report;
24  this.report.BeginMeasure(text);
25  }
26 
27  public void Dispose()
28  {
29  report.EndMeasure();
30  }
31  }
32 
33  public class PerformanceReport
34  {
35  public struct PerformanceReportInfo
36  {
37  public string Text { get; set; }
38  public long Milliseconds { get; set; }
39  public long Ticks { get; set; }
40  }
41 
42  public IEnumerable<PerformanceReportInfo> Measures { get; private set; }
43 
44  private readonly List<PerformanceReportInfo> measures = new List<PerformanceReportInfo>();
45 
46  private readonly Stopwatch stopwatch = new Stopwatch();
47  private string currentMeasureText;
48 
50  {
51  Measures = new ReadOnlyCollection<PerformanceReportInfo>(measures);
52  }
53 
54  [Conditional("DEBUG")]
55  public void BeginMeasure(string text)
56  {
57  if (currentMeasureText != null)
58  EndMeasure();
59 
60  currentMeasureText = text;
61  stopwatch.Reset();
62  stopwatch.Start();
63  }
64 
65  [Conditional("DEBUG")]
66  public void EndMeasure()
67  {
68  stopwatch.Stop();
69 
70  var ticks = stopwatch.ElapsedTicks;
71  var ms = stopwatch.ElapsedMilliseconds;
72 
73  measures.Add(new PerformanceReportInfo { Text = currentMeasureText, Milliseconds = ms, Ticks = ticks });
74  currentMeasureText = null;
75  }
76 
77  public void Reset()
78  {
79  measures.Clear();
80  }
81 
82  public override string ToString()
83  {
84  var sb = new StringBuilder();
85 
86  var totalTicks = measures.Sum(info => info.Ticks);
87 
88  foreach (var info in measures)
89  {
90  sb.AppendLine(string.Format("{0}: {1} ms, {2} ticks ({3:F2}%)",
91  info.Text, info.Milliseconds, info.Ticks, ((double)info.Ticks * 100.0 / (double)totalTicks)));
92  }
93 
94  return sb.ToString();
95  }
96  }
97 }
PerformanceCheckBlock(string text, PerformanceReport report)