-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metric.h
60 lines (57 loc) · 1.21 KB
/
Metric.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
template <class T>
class Metric
{
uint32_t _count;
const char* _name;
T _min;
T _max;
T _sum;
uint32_t _printInterval;
public:
Metric(const char* name) {
_name=name;
_printInterval=1000000;
};
Metric(const char* name,uint32_t printInterval) {
_name=name;
_printInterval=printInterval;
};
void update(T value) {
if ( _count==0 ) {
_min=_max=value;
_sum=value;
_count=1;
return;
}
if ( _min > value ) _min=value;
if ( _max < value ) _max = value;
_sum+=value;
_count++;
if ( _printInterval == _count ) {
print();
reset();
}
};
uint32_t count() {
return _count;
}
void reset() {
_count=0;
}
void print() {
INFO(" %s - min : %d , max : %d , avg : %d , count : %d ",_name, _min,_max,_sum/_count,_count);
}
};
class Timer : public Metric<uint32_t>
{
uint64_t _startTime;
public :
Timer(const char* name,uint32_t counter) : Metric(name,counter) {
}
void start() {
_startTime=micros();
}
void stop() {
update(micros()-_startTime);
}
};