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             mRunCount = 0;
40         }
41     }
42 
43     void stop()
44     {
45         assert(mLastTime != MonoTime.zero);
46         auto now = MonoTime.currTime;
47         Duration dur = now - mLastTime;
48         if (mMin == seconds(0) || dur < mMin)
49             mMin = dur;
50         if (mMax < dur)
51             mMax = dur;
52 
53         mSum += dur;
54         mRunCount++;
55     }
56 
57     void update()
58     {
59         if (mRunCount != 0)
60             mAvg = mSum / mRunCount;
61         else
62             mAvg = seconds(0);
63         mSum = seconds(0);
64         mFirstTime = MonoTime.zero;
65     }
66 
67     void clear()
68     {
69         mMin = mMax = mSum = mAvg = seconds(0);
70         mFirstTime = mLastTime = MonoTime.zero;
71         mRunCount = 0;
72     }
73 
74 public:
75     /**
76      * Elapsed time since the last update (defined by rate parameter in the
77      * statistic enabling function of the system-manager).
78      */
79     Duration elapsedTime() @property const
80     {
81         return mFirstTime == MonoTime.zero
82              ? seconds(0)
83              : MonoTime.currTime - mFirstTime;
84     }
85 
86     /**
87      * Average duration of the profiled function (during the time defined by the
88      * rate parameter in the statistic enabling function of the system-manager).
89      */
90     Duration averageDuration() @property const
91     {
92         return mAvg;
93     }
94 
95     /**
96      * Minimum measured duration of the profiled function (during the time
97      * defined by the rate parameter in the statistic enabling function of
98      * the system-manager).
99      */
100     Duration minDuration() @property const
101     {
102         return mMin;
103     }
104 
105     /**
106      * Maximum measured duration of the profiled function (during the time
107      * defined by the rate parameter in the statistic enabling function of
108      * the system-manager).
109      */
110     Duration maxDuration() @property const
111     {
112         return mMax;
113     }
114 
115     /**
116      * Number of times the profiled function was called (during the time
117      * defined by the rate parameter in the statistic enabling function of
118      * the system-manager).
119      */
120     ulong runCount() @property const
121     {
122         return mRunCount;
123     }
124 
125 private:
126     Duration        mMin;
127     Duration        mMax;
128     Duration        mSum;
129     Duration        mAvg;
130     ulong           mRunCount;
131     MonoTime        mLastTime;
132     MonoTime        mFirstTime;
133 }