forked from dterei/mutilate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionStats.h
203 lines (166 loc) · 5.69 KB
/
ConnectionStats.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* -*- c++ -*- */
#ifndef CONNECTIONSTATS_H
#define CONNECTIONSTATS_H
#include <algorithm>
#include <inttypes.h>
#include <vector>
#ifdef USE_ADAPTIVE_SAMPLER
#include "AdaptiveSampler.h"
#elif defined(USE_HISTOGRAM_SAMPLER)
#include "HistogramSampler.h"
#else
#include "LogHistogramSampler.h"
#endif
#include "AgentStats.h"
#include "Operation.h"
using namespace std;
class ConnectionStats {
public:
ConnectionStats(bool _sampling = true) :
#ifdef USE_ADAPTIVE_SAMPLER
get_sampler(100000), set_sampler(100000), op_sampler(100000),
#elif defined(USE_HISTOGRAM_SAMPLER)
get_sampler(10000,1), set_sampler(10000,1), op_sampler(1000,1),
#else
get_sampler(200), set_sampler(200), op_sampler(100),
#endif
rx_bytes(0), tx_bytes(0), gets(0), sets(0),
get_misses(0), skips(0), sampling(_sampling) {}
#ifdef USE_ADAPTIVE_SAMPLER
AdaptiveSampler<Operation> get_sampler;
AdaptiveSampler<Operation> set_sampler;
AdaptiveSampler<double> op_sampler;
#elif defined(USE_HISTOGRAM_SAMPLER)
HistogramSampler get_sampler;
HistogramSampler set_sampler;
HistogramSampler op_sampler;
#else
LogHistogramSampler get_sampler;
LogHistogramSampler set_sampler;
LogHistogramSampler op_sampler;
#endif
uint64_t rx_bytes, tx_bytes;
uint64_t gets, sets, get_misses;
uint64_t skips;
double start, stop;
bool sampling;
void log_get(Operation& op) { if (sampling) get_sampler.sample(op); gets++; }
void log_set(Operation& op) { if (sampling) set_sampler.sample(op); sets++; }
void log_op (double op) { if (sampling) op_sampler.sample(op); }
double get_qps() {
return (gets + sets) / (stop - start);
}
#ifdef USE_ADAPTIVE_SAMPLER
double get_nth(double nth) {
vector<double> samples;
if (samples.size() == 0) return 0;
for (auto s: get_sampler.samples)
samples.push_back(s.time());
for (auto s: set_sampler.samples)
samples.push_back(s.time());
sort(samples.begin(), samples.end());
int l = samples.size();
int i = (int)((nth * l) / 100);
assert(i < l);
return samples[i];
}
#else
double get_nth(double nth) {
// FIXME: nth across gets & sets?
return get_sampler.get_nth(nth);
}
#endif
void accumulate(const ConnectionStats &cs) {
#ifdef USE_ADAPTIVE_SAMPLER
for (auto i: cs.get_sampler.samples) get_sampler.sample(i);
for (auto i: cs.set_sampler.samples) set_sampler.sample(i);
for (auto i: cs.op_sampler.samples) op_sampler.sample(i);
#else
get_sampler.accumulate(cs.get_sampler);
set_sampler.accumulate(cs.set_sampler);
op_sampler.accumulate(cs.op_sampler);
#endif
rx_bytes += cs.rx_bytes;
tx_bytes += cs.tx_bytes;
gets += cs.gets;
sets += cs.sets;
get_misses += cs.get_misses;
skips += cs.skips;
start = cs.start;
stop = cs.stop;
}
void accumulate(const AgentStats &as) {
rx_bytes += as.rx_bytes;
tx_bytes += as.tx_bytes;
gets += as.gets;
sets += as.sets;
get_misses += as.get_misses;
skips += as.skips;
start = as.start;
stop = as.stop;
}
static void print_header(FILE* out) {
fprintf(out, "%-7s %7s %7s %7s %7s %7s %7s %7s %7s %7s\n",
"#type", "avg", "std", "min", "5th", "10th",
"90th", "95th", "99th", "max");
}
#ifdef USE_ADAPTIVE_SAMPLER
void print_stats(FILE* out, const char *tag,
AdaptiveSampler<Operation> &sampler, bool newline = true) {
vector<double> copy;
for (auto i: sampler.samples) copy.push_back(i.time());
size_t l = copy.size();
if (l == 0) {
fprintf(out, "%-7s %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f",
tag, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
if (newline) fprintf(out, "\n");
return;
}
sort(copy.begin(), copy.end());
fprintf(out, "%-7s %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f",
tag, std::accumulate(copy.begin(), copy.end(), 0.0) / l,
copy[0], copy[(l*1) / 100], copy[(l*5) / 100], copy[(l*10) / 100],
copy[(l*90) / 100], copy[(l*95) / 100], copy[(l*99) / 100]
);
if (newline) fprintf(out, "\n");
}
void print_stats(FILE* out, const char *tag,
AdaptiveSampler<double> &sampler, bool newline = true) {
vector<double> copy;
for (auto i: sampler.samples) copy.push_back(i);
size_t l = copy.size();
if (l == 0) {
fprintf(out, "%-7s 0", tag);
if (newline) fprintf(out, "\n");
return;
}
sort(copy.begin(), copy.end());
fprintf(out, "%-7s %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f",
tag, std::accumulate(copy.begin(), copy.end(), 0.0) / l,
copy[0], copy[(l*1) / 100], copy[(l*5) / 100], copy[(l*10) / 100],
copy[(l*90) / 100], copy[(l*95) / 100], copy[(l*99) / 100], 0.0);
if (newline) fprintf(out, "\n");
}
#elif defined(USE_HISTOGRAM_SAMPLER)
void print_stats(FILE* out, const char *tag, HistogramSampler &sampler,
bool newline = true) {
#else /* LOGHISTOGRAMSAMPLER */
void print_stats(FILE* out, const char *tag, LogHistogramSampler &sampler,
bool newline = true) {
#endif
if (sampler.total() == 0) {
fprintf(out, "%-7s %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f",
tag, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
if (newline) fprintf(out, "\n");
return;
}
fprintf(out, "%-7s %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f",
tag, sampler.average(), sampler.stddev(),
sampler.minimum(), sampler.get_nth(5),
sampler.get_nth(10), sampler.get_nth(90),
sampler.get_nth(95), sampler.get_nth(99),
sampler.maximum());
if (newline) fprintf(out, "\n");
}
};
#endif // CONNECTIONSTATS_H