-
Notifications
You must be signed in to change notification settings - Fork 2
/
freq_counter.hh
125 lines (117 loc) · 3.15 KB
/
freq_counter.hh
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (c) 2016 Zhao DAI <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see accompanying file LICENSE.txt
* or <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Events counting and frequency calculation.
* @author Zhao DAI
*/
#ifndef DOZERG_FREQ_COUNTER_H_20170109
#define DOZERG_FREQ_COUNTER_H_20170109
#include "tools/time.hh"
#include "to_string.hh"
NS_SERVER_BEGIN
template<size_t kDuration, size_t kGranularity = 1>
class CFreqCounter
{
static const size_t kSize = kDuration + 1;
public:
CFreqCounter()
: up_(tools::MonoTime(NULL))
, last_(up_)
, lasti_(0)
{
::memset(cnt_, 0, sizeof cnt_);
}
static size_t granularity(){return kGranularity;}
static size_t duration(){return kDuration;}
void occur(size_t times = 1){
update();
cnt_[lasti_ % kSize] += times;
}
size_t frequency(){
update();
size_t dur = kSize;
const size_t s = sumTo(&dur);
return (s / dur);
}
double frequencyDouble(){
update();
size_t dur = kSize;
const double s = sumTo(&dur);
return (s / dur);
}
std::string toString() const{
CToString oss;
oss<<"{up_="<<up_
<<", last_="<<last_
<<", lasti_="<<lasti_
<<", cnt_={"<<cnt_[0];
for(size_t i = 1;i < kSize;++i)
oss<<", "<<cnt_[i];
oss<<"}}";
return oss.str();
}
private:
size_t sumTo(size_t * dur) const{
size_t s = 0, d = 0;
if(lasti_ < 2){
s = cnt_[0];
d = 1;
}else if( lasti_ < kSize){
for(d = 1;d < lasti_;++d)
s += cnt_[d];
d = lasti_ - 1;
}else{
size_t cur = lasti_ % kSize;
for(size_t i = 0;i < kSize;++i)
if(i != cur)
s += cnt_[i];
d = kSize - 1;
}
if(dur)
*dur = d;
return s;
}
void update(){
time_t t = tools::MonoTime(NULL);
if(t > last_){
size_t e = idx(t);
if(e < lasti_ + kSize){
for(;++lasti_ <= e;)
cnt_[lasti_ % kSize] = 0;
}else
::memset(cnt_, 0, sizeof cnt_);
last_ = t;
lasti_ = e;
}
}
size_t idx(time_t cur) const{
int t = cur - up_;
if(t < 0)
t = 0;
if(kGranularity > 1)
t /= kGranularity;
return t;
}
const time_t up_;
time_t last_;
size_t lasti_;
size_t cnt_[kSize];
};
NS_SERVER_END
#endif