main.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <library/cpp/testing/gbenchmark/benchmark.h>
  2. #include <util/datetime/base.h>
  3. #include <util/stream/str.h>
  4. class TTimestampGenerator {
  5. public:
  6. TInstant operator()() {
  7. TInstant result = TInstant::MicroSeconds(Base_ + Current_);
  8. Current_ = (Current_ + Step_) % Range_;
  9. return result;
  10. }
  11. private:
  12. static constexpr ui64 Step_ = TDuration::MicroSeconds(1234567891011).MicroSeconds();
  13. static constexpr ui64 Range_ = 1ull << 45; // ~ year
  14. static constexpr ui64 Base_ = TInstant::Seconds(1605320321).MicroSeconds();
  15. ui64 Current_ = 0;
  16. };
  17. Y_FORCE_INLINE static void BenchFormatStream(auto&& formatFn, benchmark::State& state) {
  18. TTimestampGenerator gen;
  19. TStringStream ss;
  20. for (auto _ : state) {
  21. ss << formatFn(gen());
  22. Y_DO_NOT_OPTIMIZE_AWAY(ss.Str());
  23. ss.clear();
  24. }
  25. }
  26. Y_FORCE_INLINE static void BenchToString(auto&& toStringFn, benchmark::State& state) {
  27. TTimestampGenerator gen;
  28. TString s;
  29. for (auto _ : state) {
  30. s = toStringFn(gen());
  31. Y_DO_NOT_OPTIMIZE_AWAY(s);
  32. }
  33. }
  34. void BM_FormatIsoLocal(benchmark::State& state) {
  35. BenchFormatStream(FormatIsoLocal, state);
  36. }
  37. void BM_FormatLocal(benchmark::State& state) {
  38. BenchFormatStream(FormatLocal, state);
  39. }
  40. void BM_ToStringIsoLocal(benchmark::State& state) {
  41. BenchToString(std::mem_fn(&TInstant::ToIsoStringLocal), state);
  42. }
  43. void BM_ToStringLocal(benchmark::State& state) {
  44. BenchToString(std::mem_fn(&TInstant::ToIsoStringLocal), state);
  45. }
  46. BENCHMARK(BM_FormatIsoLocal);
  47. BENCHMARK(BM_FormatLocal);
  48. BENCHMARK(BM_ToStringIsoLocal);
  49. BENCHMARK(BM_ToStringLocal);