Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PerformanceLogger.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.Diagnostics;
6 using System.IO;
7 using System.Linq;
8 
9 namespace SiliconStudio.Paradox.Shaders.Parser.Performance
10 {
11  public static class PerformanceLogger
12  {
13  private static int globalCount;
14  private static int loadingCount;
15  private static int typeAnalysisCount;
16  private static int semanticAnalysisCount;
17  private static int mixCount;
18  private static int deepCloneCount;
19  private static int astParsingCount;
20 
21  private static readonly List<long> GlobalTimes = new List<long>();
22  private static readonly List<long> LoadingTimes = new List<long>();
23  private static readonly List<long> TypeAnalysisTimes = new List<long>();
24  private static readonly List<long> SemanticAnalysisTimes = new List<long>();
25  private static readonly List<long> MixTimes = new List<long>();
26  private static readonly List<long> DeepcloneTimes = new List<long>();
27  private static readonly List<long> AstParsingTimes = new List<long>();
28 
29  private static Stopwatch globalWatch = new Stopwatch();
30  private static Stopwatch loadingWatch = new Stopwatch();
31  private static Stopwatch typeAnalysisWatch = new Stopwatch();
32  private static Stopwatch semanticAnalysisWatch = new Stopwatch();
33  private static Stopwatch mixWatch = new Stopwatch();
34  private static Stopwatch deepCloneWatch = new Stopwatch();
35  private static Stopwatch astParsingWatch = new Stopwatch();
36 
37  public static void Start(PerformanceStage stage)
38  {
39  switch (stage)
40  {
41  case PerformanceStage.Global:
42  globalWatch.Start();
43  break;
44  case PerformanceStage.Loading:
45  loadingWatch.Start();
46  break;
47  case PerformanceStage.TypeAnalysis:
48  typeAnalysisWatch.Start();
49  break;
50  case PerformanceStage.SemanticAnalysis:
51  semanticAnalysisWatch.Start();
52  break;
53  case PerformanceStage.Mix:
54  mixWatch.Start();
55  break;
56  case PerformanceStage.DeepClone:
57  deepCloneWatch.Start();
58  break;
59  case PerformanceStage.AstParsing:
60  astParsingWatch.Start();
61  break;
62  }
63  }
64 
65  public static void Pause(PerformanceStage stage)
66  {
67  switch (stage)
68  {
69  case PerformanceStage.Global:
70  globalWatch.Stop();
71  break;
72  case PerformanceStage.Loading:
73  loadingWatch.Stop();
74  break;
75  case PerformanceStage.TypeAnalysis:
76  typeAnalysisWatch.Stop();
77  break;
78  case PerformanceStage.SemanticAnalysis:
79  semanticAnalysisWatch.Stop();
80  break;
81  case PerformanceStage.Mix:
82  mixWatch.Stop();
83  break;
84  case PerformanceStage.DeepClone:
85  deepCloneWatch.Stop();
86  break;
87  case PerformanceStage.AstParsing:
88  astParsingWatch.Stop();
89  break;
90  }
91  }
92 
93  public static void Stop(PerformanceStage stage)
94  {
95  switch (stage)
96  {
97  case PerformanceStage.Global:
98  globalWatch.Stop();
99  GlobalTimes.Add(globalWatch.ElapsedMilliseconds);
100  ++globalCount;
101  break;
102  case PerformanceStage.Loading:
103  loadingWatch.Stop();
104  LoadingTimes.Add(loadingWatch.ElapsedMilliseconds);
105  ++loadingCount;
106  break;
107  case PerformanceStage.TypeAnalysis:
108  typeAnalysisWatch.Stop();
109  TypeAnalysisTimes.Add(typeAnalysisWatch.ElapsedMilliseconds);
110  ++typeAnalysisCount;
111  break;
112  case PerformanceStage.SemanticAnalysis:
113  semanticAnalysisWatch.Stop();
114  SemanticAnalysisTimes.Add(semanticAnalysisWatch.ElapsedMilliseconds);
115  ++semanticAnalysisCount;
116  break;
117  case PerformanceStage.Mix:
118  mixWatch.Stop();
119  MixTimes.Add(mixWatch.ElapsedMilliseconds);
120  ++mixCount;
121  break;
122  case PerformanceStage.DeepClone:
123  deepCloneWatch.Stop();
124  DeepcloneTimes.Add(deepCloneWatch.ElapsedMilliseconds);
125  ++deepCloneCount;
126  break;
127  case PerformanceStage.AstParsing:
128  astParsingWatch.Stop();
129  AstParsingTimes.Add(astParsingWatch.ElapsedMilliseconds);
130  ++astParsingCount;
131  break;
132  }
133  }
134 
135  public static void Reset()
136  {
137  globalWatch.Reset();
138  loadingWatch.Reset();
139  typeAnalysisWatch.Reset();
140  semanticAnalysisWatch.Reset();
141  mixWatch.Reset();
142  deepCloneWatch.Reset();
143  astParsingWatch.Reset();
144  }
145 
146  public static void PrintResult()
147  {
148  Console.WriteLine();
149  Console.WriteLine(@"--------------------------TOTAL PERFORMANCE ANALYZER---------------------------");
150  Console.WriteLine(@"Loading took {0} ms for {1} shader(s)", LoadingTimes.Aggregate((long)0, (agg, next) => agg + next), loadingCount);
151  Console.WriteLine(@"Type analysis took {0} ms for {1} shader(s)", TypeAnalysisTimes.Aggregate((long)0, (agg, next) => agg + next), typeAnalysisCount);
152  Console.WriteLine(@"Semantic analysis took {0} ms for {1} shader(s)", SemanticAnalysisTimes.Aggregate((long)0, (agg, next) => agg + next), semanticAnalysisCount);
153  Console.WriteLine(@"Mix took {0} ms for {1} shader(s)", MixTimes.Aggregate((long)0, (agg, next) => agg + next), mixCount);
154  Console.WriteLine(@"DeepClone took {0} ms for {1} shader(s)", DeepcloneTimes.Aggregate((long)0, (agg, next) => agg + next), deepCloneCount);
155  Console.WriteLine(@"Ast parsing took {0} ms for {1} shader(s)", AstParsingTimes.Aggregate((long)0, (agg, next) => agg + next), astParsingCount);
156  Console.WriteLine(@"-------------------------------------------------------------------------------");
157  Console.WriteLine();
158  }
159  public static void PrintLastResult()
160  {
161  Console.WriteLine();
162  Console.WriteLine(@"--------------------------LAST PERFORMANCE ANALYZER---------------------------");
163  Console.WriteLine(@"Process took {0} ms", globalWatch.ElapsedMilliseconds);
164  Console.WriteLine(@"Loading took {0} ms", loadingWatch.ElapsedMilliseconds);
165  Console.WriteLine(@"Type analysis took {0} ms", typeAnalysisWatch.ElapsedMilliseconds);
166  Console.WriteLine(@"Semantic analysis took {0} ms", semanticAnalysisWatch.ElapsedMilliseconds);
167  Console.WriteLine(@"Mix took {0} ms", mixWatch.ElapsedMilliseconds);
168  Console.WriteLine(@"DeepClone took {0} ms", deepCloneWatch.ElapsedMilliseconds);
169  Console.WriteLine(@"Ast parsing took {0} ms", astParsingWatch.ElapsedMilliseconds);
170  Console.WriteLine(@"------------------------------------------------------------------------------");
171  Console.WriteLine();
172  }
173 
174 
175  public static void WriteOut(int limit)
176  {
177  if (globalCount == limit)
178  {
179  PrintResult();
180  TextWriter tw = new StreamWriter("performance.csv");
181  tw.WriteLine("loading,type,semantic,mix,deepclone,global");
182 
183  for (var i = 0; i < limit; ++i)
184  {
185  tw.WriteLine("{0},{1},{2},{3},{4},{5}", LoadingTimes[i], TypeAnalysisTimes[i], SemanticAnalysisTimes[i], MixTimes[i], DeepcloneTimes[i], GlobalTimes[i]);
186  }
187  tw.Close();
188  }
189  }
190  }
191 
192  public enum PerformanceStage
193  {
194  Global,
195  Loading,
196  TypeAnalysis,
198  Mix,
199  DeepClone,
200  AstParsing
201  }
202 }