PageRenderTime 30ms CodeModel.GetById 20ms app.highlight 7ms 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 * ***************************************************************************/
 24using System;
 25using System.Collections.Generic;
 26using System.Linq;
 27using System.Text;
 28using System.Diagnostics;
 29using System.Xronos.Language;
 30using System.Threading;
 31
 32namespace System.Xronos
 33{
 34    static class Statistics
 35    {
 36        class ThreadStatistics
 37        {
 38            public Stopwatch StartupTime = new Stopwatch();
 39            public Stopwatch ReaderTime = new Stopwatch();
 40            public Stopwatch CompilerTime = new Stopwatch();
 41            public Stopwatch TypeGenerationTime = new Stopwatch();
 42            public Stopwatch MacroExpandTime = new Stopwatch();
 43            public Stopwatch ExecutionTime = new Stopwatch();
 44            public Stopwatch MethodResolutionTime = new Stopwatch();
 45            public Stopwatch StringInternTime = new Stopwatch();
 46            public int BindMisses = 0;
 47        }
 48
 49        [ThreadStatic]
 50        static ThreadStatistics _localStats;
 51        static List<ThreadStatistics> _allStats = new List<ThreadStatistics>();
 52
 53        private static ThreadStatistics LocalStats
 54        {
 55            get
 56            {
 57                if (_localStats == null)
 58                {
 59                    _localStats = new ThreadStatistics();
 60                    lock (_allStats)
 61                        _allStats.Add(_localStats);
 62                }
 63                return _localStats;
 64            }
 65        }
 66
 67        public static Stopwatch StartupTime { get { return LocalStats.StartupTime; } }
 68        public static Stopwatch ReaderTime { get { return LocalStats.ReaderTime; } }
 69        public static Stopwatch CompilerTime { get { return LocalStats.CompilerTime; } }
 70        public static Stopwatch TypeGenerationTime { get { return LocalStats.TypeGenerationTime; } }
 71        public static Stopwatch MacroExpandTime { get { return LocalStats.MacroExpandTime; } }
 72        public static Stopwatch ExecutionTime { get { return LocalStats.ExecutionTime; } }
 73        public static Stopwatch MethodResolutionTime { get { return LocalStats.MethodResolutionTime; } }
 74        public static Stopwatch StringInternTime { get { return LocalStats.StringInternTime; } }
 75        public static int BindMisses { get { return LocalStats.BindMisses; } }
 76
 77        public static void IncrementBindMisses()
 78        {
 79            Interlocked.Increment(ref LocalStats.BindMisses);
 80        }
 81
 82        public static IPersistentMap GetStatistics()
 83        {
 84            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;
 85            int misses = 0;
 86
 87            lock (_allStats)
 88                foreach (var item in _allStats)
 89                {
 90                    startup += item.StartupTime.Elapsed;
 91                    reader += item.ReaderTime.Elapsed;
 92                    compiler += item.CompilerTime.Elapsed;
 93                    typegen += item.TypeGenerationTime.Elapsed;
 94                    macros += item.MacroExpandTime.Elapsed;
 95                    execution += item.ExecutionTime.Elapsed;
 96                    methodres += item.MethodResolutionTime.Elapsed;
 97                    interns += item.StringInternTime.Elapsed;
 98                    misses += item.BindMisses;
 99                }
100
101            return PersistentHashMap.Create(new object[] {
102                "Startup",  startup,
103                "Reader",  reader,
104                "Compiler",  compiler,
105                "Type Gen",  typegen,
106                "Macroexpand",  macros,
107                "Execution",  execution,
108                "Method Resolution",  methodres,
109                "String Interns", interns,
110                "Bind Misses", misses,
111            });
112        }
113    }
114}