1 /** 2 System management module. 3 4 Copyright: © 2015-2016 Claude Merle 5 Authors: Claude Merle 6 License: This file is part of EntitySysD. 7 8 EntitySysD is free software: you can redistribute it and/or modify it 9 under the terms of the Lesser GNU General Public License as published 10 by the Free Software Foundation, either version 3 of the License, or 11 (at your option) any later version. 12 13 EntitySysD is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 Lesser GNU General Public License for more details. 17 18 You should have received a copy of the Lesser GNU General Public License 19 along with EntitySysD. If not, see $(LINK http://www.gnu.org/licenses/). 20 */ 21 22 module entitysysd.stat; 23 24 public import core.time; 25 26 27 /** 28 * Structure used in the system-manager to do some basic profiling. 29 */ 30 struct Stat 31 { 32 package: 33 void start() 34 { 35 mLastTime = MonoTime.currTime; 36 if (mFirstTime == MonoTime.zero) 37 { 38 mFirstTime = mLastTime; 39 mMin = mMax = seconds(0); 40 mRunCount = 0; 41 } 42 } 43 44 void stop() 45 { 46 assert(mLastTime != MonoTime.zero); 47 auto now = MonoTime.currTime; 48 Duration dur = now - mLastTime; 49 if (mMin == seconds(0) || dur < mMin) 50 mMin = dur; 51 if (mMax < dur) 52 mMax = dur; 53 54 mSum += dur; 55 mRunCount++; 56 } 57 58 void update() 59 { 60 if (mRunCount != 0) 61 mAvg = mSum / mRunCount; 62 else 63 mAvg = seconds(0); 64 mSum = seconds(0); 65 mFirstTime = MonoTime.zero; 66 } 67 68 void clear() 69 { 70 mMin = mMax = mSum = mAvg = seconds(0); 71 mFirstTime = mLastTime = MonoTime.zero; 72 mRunCount = 0; 73 } 74 75 public: 76 /** 77 * Elapsed time since the last update (defined by rate parameter in the 78 * statistic enabling function of the system-manager). 79 */ 80 Duration elapsedTime() @property const 81 { 82 return mFirstTime == MonoTime.zero 83 ? seconds(0) 84 : MonoTime.currTime - mFirstTime; 85 } 86 87 deprecated("Please, use `average` instead.") 88 alias averageDuration = average; 89 deprecated("Please, use `min` instead.") 90 alias minDuration = min; 91 deprecated("Please, use `max` instead.") 92 alias maxDuration = max; 93 94 /** 95 * Average duration of the profiled function (during the time defined by the 96 * rate parameter in the statistic enabling function of the system-manager). 97 */ 98 Duration average() @property const 99 { 100 return mAvg; 101 } 102 103 /** 104 * Minimum measured duration of the profiled function (during the time 105 * defined by the rate parameter in the statistic enabling function of 106 * the system-manager). 107 */ 108 Duration min() @property const 109 { 110 return mMin; 111 } 112 113 /** 114 * Maximum measured duration of the profiled function (during the time 115 * defined by the rate parameter in the statistic enabling function of 116 * the system-manager). 117 */ 118 Duration max() @property const 119 { 120 return mMax; 121 } 122 123 /** 124 * Number of times the profiled function was called (during the time 125 * defined by the rate parameter in the statistic enabling function of 126 * the system-manager). 127 */ 128 ulong runCount() @property const 129 { 130 return mRunCount; 131 } 132 133 private: 134 Duration mMin; 135 Duration mMax; 136 Duration mSum; 137 Duration mAvg; 138 ulong mRunCount; 139 MonoTime mLastTime; 140 MonoTime mFirstTime; 141 }