Files
scylladb/utils/histogram.hh
Amnon Heiman 2b584ec2ec Adding the histogram object
The histogram object is equivalent to the Histogram used in Origin. It
collect multiple values about the data:
Count, Min, Max, Sum, variance and the sum of square that are used for
std calculation.

It also contain a sample of the last n elements, that are stored in a
circular buffer.

The histogram is used by the API to report histogram statistics.

As the API does not support unsigned integer, the count is signed.

Typically the base type of the histogram is int64_t, so ihistogram was
defined as such.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-07-26 10:55:14 +03:00

51 lines
1.0 KiB
C++

/*
* Copyright 2015 Cloudius Systems
*/
#pragma once
#include <boost/circular_buffer.hpp>
namespace utils {
template<typename T>
class histogram {
public:
int64_t count;
T min;
T max;
T sum;
double mean;
double variance;
boost::circular_buffer<T> sample;
histogram(size_t size = 1024)
: count(0), min(0), max(0), sum(0), mean(0), variance(0), sample(
size) {
}
void mark(T value) {
if (count == 0 || value < min) {
min = value;
}
if (count == 0 || value > max) {
max = value;
}
if (count == 0) {
mean = value;
variance = 0;
} else {
double old_m = mean;
double old_s = variance;
mean = old_m + ((value - old_m) / (count + 1));
variance = old_s + ((value - old_m) * (value - mean));
}
sum += value;
count++;
sample.push_back(value);
}
};
using ihistogram = histogram<int64_t>;
}