duration_histogram.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma once
  2. #include <util/datetime/base.h>
  3. #include <util/generic/bitops.h>
  4. #include <util/generic/string.h>
  5. #include <array>
  6. struct TDurationHistogram {
  7. static const unsigned Buckets = 20;
  8. std::array<ui64, Buckets> Times;
  9. static const unsigned SecondBoundary = 11;
  10. TDurationHistogram() {
  11. Times.fill(0);
  12. }
  13. static unsigned BucketFor(TDuration d) {
  14. ui64 units = d.MicroSeconds() * (1 << SecondBoundary) / 1000000;
  15. if (units == 0) {
  16. return 0;
  17. }
  18. unsigned bucket = GetValueBitCount(units) - 1;
  19. if (bucket >= Buckets) {
  20. bucket = Buckets - 1;
  21. }
  22. return bucket;
  23. }
  24. void AddTime(TDuration d) {
  25. Times[BucketFor(d)] += 1;
  26. }
  27. TDurationHistogram& operator+=(const TDurationHistogram& that) {
  28. for (unsigned i = 0; i < Times.size(); ++i) {
  29. Times[i] += that.Times[i];
  30. }
  31. return *this;
  32. }
  33. static TString LabelBefore(unsigned i);
  34. TString PrintToString() const;
  35. };