duration_histogram.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "duration_histogram.h"
  2. #include <util/generic/singleton.h>
  3. #include <util/stream/str.h>
  4. namespace {
  5. ui64 SecondsRound(TDuration d) {
  6. if (d.MilliSeconds() % 1000 >= 500) {
  7. return d.Seconds() + 1;
  8. } else {
  9. return d.Seconds();
  10. }
  11. }
  12. ui64 MilliSecondsRound(TDuration d) {
  13. if (d.MicroSeconds() % 1000 >= 500) {
  14. return d.MilliSeconds() + 1;
  15. } else {
  16. return d.MilliSeconds();
  17. }
  18. }
  19. ui64 MinutesRound(TDuration d) {
  20. if (d.Seconds() % 60 >= 30) {
  21. return d.Minutes() + 1;
  22. } else {
  23. return d.Minutes();
  24. }
  25. }
  26. }
  27. namespace {
  28. struct TMarks {
  29. std::array<TDuration, TDurationHistogram::Buckets> Marks;
  30. TMarks() {
  31. Marks[0] = TDuration::Zero();
  32. for (unsigned i = 1; i < TDurationHistogram::Buckets; ++i) {
  33. if (i >= TDurationHistogram::SecondBoundary) {
  34. Marks[i] = TDuration::Seconds(1) * (1 << (i - TDurationHistogram::SecondBoundary));
  35. } else {
  36. Marks[i] = TDuration::Seconds(1) / (1 << (TDurationHistogram::SecondBoundary - i));
  37. }
  38. }
  39. }
  40. };
  41. }
  42. TString TDurationHistogram::LabelBefore(unsigned i) {
  43. Y_ABORT_UNLESS(i < Buckets);
  44. TDuration d = Singleton<TMarks>()->Marks[i];
  45. TStringStream ss;
  46. if (d == TDuration::Zero()) {
  47. ss << "0";
  48. } else if (d < TDuration::Seconds(1)) {
  49. ss << MilliSecondsRound(d) << "ms";
  50. } else if (d < TDuration::Minutes(1)) {
  51. ss << SecondsRound(d) << "s";
  52. } else {
  53. ss << MinutesRound(d) << "m";
  54. }
  55. return ss.Str();
  56. }
  57. TString TDurationHistogram::PrintToString() const {
  58. TStringStream ss;
  59. for (auto time : Times) {
  60. ss << time << "\n";
  61. }
  62. return ss.Str();
  63. }