diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/messagebus/duration_histogram.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/messagebus/duration_histogram.h')
-rw-r--r-- | library/cpp/messagebus/duration_histogram.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/library/cpp/messagebus/duration_histogram.h b/library/cpp/messagebus/duration_histogram.h new file mode 100644 index 0000000000..ed060b0101 --- /dev/null +++ b/library/cpp/messagebus/duration_histogram.h @@ -0,0 +1,45 @@ +#pragma once + +#include <util/datetime/base.h> +#include <util/generic/bitops.h> +#include <util/generic/string.h> + +#include <array> + +struct TDurationHistogram { + static const unsigned Buckets = 20; + std::array<ui64, Buckets> Times; + + static const unsigned SecondBoundary = 11; + + TDurationHistogram() { + Times.fill(0); + } + + static unsigned BucketFor(TDuration d) { + ui64 units = d.MicroSeconds() * (1 << SecondBoundary) / 1000000; + if (units == 0) { + return 0; + } + unsigned bucket = GetValueBitCount(units) - 1; + if (bucket >= Buckets) { + bucket = Buckets - 1; + } + return bucket; + } + + void AddTime(TDuration d) { + Times[BucketFor(d)] += 1; + } + + TDurationHistogram& operator+=(const TDurationHistogram& that) { + for (unsigned i = 0; i < Times.size(); ++i) { + Times[i] += that.Times[i]; + } + return *this; + } + + static TString LabelBefore(unsigned i); + + TString PrintToString() const; +}; |