PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/System.Xronos/Statistics.cs

https://bitbucket.org/stefanrusek/xronos
C# | 114 lines | 83 code | 8 blank | 23 comment | 2 complexity | 6560ee0b864d5f64e9d594aa1f1f05f3 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) 2008 Stefan Rusek and Benjamin Pollack
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. *
  23. * ***************************************************************************/
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Linq;
  27. using System.Text;
  28. using System.Diagnostics;
  29. using System.Xronos.Language;
  30. using System.Threading;
  31. namespace System.Xronos
  32. {
  33. static class Statistics
  34. {
  35. class ThreadStatistics
  36. {
  37. public Stopwatch StartupTime = new Stopwatch();
  38. public Stopwatch ReaderTime = new Stopwatch();
  39. public Stopwatch CompilerTime = new Stopwatch();
  40. public Stopwatch TypeGenerationTime = new Stopwatch();
  41. public Stopwatch MacroExpandTime = new Stopwatch();
  42. public Stopwatch ExecutionTime = new Stopwatch();
  43. public Stopwatch MethodResolutionTime = new Stopwatch();
  44. public Stopwatch StringInternTime = new Stopwatch();
  45. public int BindMisses = 0;
  46. }
  47. [ThreadStatic]
  48. static ThreadStatistics _localStats;
  49. static List<ThreadStatistics> _allStats = new List<ThreadStatistics>();
  50. private static ThreadStatistics LocalStats
  51. {
  52. get
  53. {
  54. if (_localStats == null)
  55. {
  56. _localStats = new ThreadStatistics();
  57. lock (_allStats)
  58. _allStats.Add(_localStats);
  59. }
  60. return _localStats;
  61. }
  62. }
  63. public static Stopwatch StartupTime { get { return LocalStats.StartupTime; } }
  64. public static Stopwatch ReaderTime { get { return LocalStats.ReaderTime; } }
  65. public static Stopwatch CompilerTime { get { return LocalStats.CompilerTime; } }
  66. public static Stopwatch TypeGenerationTime { get { return LocalStats.TypeGenerationTime; } }
  67. public static Stopwatch MacroExpandTime { get { return LocalStats.MacroExpandTime; } }
  68. public static Stopwatch ExecutionTime { get { return LocalStats.ExecutionTime; } }
  69. public static Stopwatch MethodResolutionTime { get { return LocalStats.MethodResolutionTime; } }
  70. public static Stopwatch StringInternTime { get { return LocalStats.StringInternTime; } }
  71. public static int BindMisses { get { return LocalStats.BindMisses; } }
  72. public static void IncrementBindMisses()
  73. {
  74. Interlocked.Increment(ref LocalStats.BindMisses);
  75. }
  76. public static IPersistentMap GetStatistics()
  77. {
  78. TimeSpan startup = TimeSpan.Zero, reader = TimeSpan.Zero, compiler = TimeSpan.Zero, typegen = TimeSpan.Zero, execution = TimeSpan.Zero, macros = TimeSpan.Zero, methodres = TimeSpan.Zero, interns = TimeSpan.Zero;
  79. int misses = 0;
  80. lock (_allStats)
  81. foreach (var item in _allStats)
  82. {
  83. startup += item.StartupTime.Elapsed;
  84. reader += item.ReaderTime.Elapsed;
  85. compiler += item.CompilerTime.Elapsed;
  86. typegen += item.TypeGenerationTime.Elapsed;
  87. macros += item.MacroExpandTime.Elapsed;
  88. execution += item.ExecutionTime.Elapsed;
  89. methodres += item.MethodResolutionTime.Elapsed;
  90. interns += item.StringInternTime.Elapsed;
  91. misses += item.BindMisses;
  92. }
  93. return PersistentHashMap.Create(new object[] {
  94. "Startup", startup,
  95. "Reader", reader,
  96. "Compiler", compiler,
  97. "Type Gen", typegen,
  98. "Macroexpand", macros,
  99. "Execution", execution,
  100. "Method Resolution", methodres,
  101. "String Interns", interns,
  102. "Bind Misses", misses,
  103. });
  104. }
  105. }
  106. }