forked from leverich/mutilate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdaptiveSampler.h
99 lines (78 loc) · 2.55 KB
/
AdaptiveSampler.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
/* -*- c++ -*- */
#ifndef ADAPTIVESAMPLER_H
#define ADAPTIVESAMPLER_H
// Simple exponential-backoff adaptive time series sampler. Will
// record at most max_samples samples out of however many samples are
// thrown at it. Makes a vague effort to do this evenly over the
// samples given to it. The sampling is time invariant (i.e. if you
// start inserting samples at a slower rate, they will be
// under-represented).
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include "log.h"
template <class T> class AdaptiveSampler {
public:
std::vector<T> samples;
unsigned int sample_rate;
unsigned int max_samples;
unsigned int total_samples;
AdaptiveSampler() = delete;
AdaptiveSampler(int max) :
sample_rate(1), max_samples(max), total_samples(0) {
}
void sample(T s) {
total_samples++;
if (drand48() < (1/(double) sample_rate))
samples.push_back(s);
// Throw out half of the samples, double sample_rate.
if (samples.size() >= max_samples) {
sample_rate *= 2;
std::vector<T> half_samples;
for (unsigned int i = 0; i < samples.size(); i++) {
if (drand48() > .5) half_samples.push_back(samples[i]);
}
samples = half_samples;
}
}
void save_samples(const char* type, const char* filename) {
FILE *file;
if ((file = fopen(filename, "a")) == NULL) {
W("fopen() failed: %s", strerror(errno));
return;
}
for (size_t i = 0; i < samples.size(); i++) {
fprintf(file, "%s %" PRIu64 " %f\n", type, i, samples[i]);
}
}
double average() {
double result = 0.0;
size_t length = samples.size();
for (size_t i = 0; i < length; i++) result += samples[i];
return result/length;
}
void print_header() {
printf("#%-6s %6s %8s %8s %8s %8s %8s %8s\n", "type", "size",
"min", "max", "avg", "90th", "95th", "99th");
}
void print_stats(const char *type, const char *size) {
std::vector<double> samples_copy = samples;
size_t l = samples_copy.size();
if (l == 0) {
printf("%-7s %6s %8.1f %8.1f %8.1f %8.1f %8.1f %8.1f\n", type, size,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
return;
}
sort(samples_copy.begin(), samples_copy.end());
printf("%-7s %6s %8.1f %8.1f %8.1f %8.1f %8.1f %8.1f\n", type, size,
samples_copy[0], samples_copy[l-1], average(),
samples_copy[(l*90)/100], samples_copy[(l*95)/100],
samples_copy[(l*99)/100]);
}
};
#endif // ADAPTIVESAMPLER_H