saveload.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <benchmark/benchmark.h>
  2. #include <library/cpp/yson/node/node_io.h>
  3. using namespace NYT;
  4. namespace {
  5. static NYT::TNode GenerateList(size_t size)
  6. {
  7. NYT::TNode result = NYT::TNode::CreateList();
  8. for (size_t i = 0; i < size; ++i) {
  9. result.AsList().emplace_back(NYT::TNode("val"));
  10. }
  11. return result;
  12. }
  13. } // namespace
  14. static void BM_SaveLoadGreedy(benchmark::State& state, size_t size)
  15. {
  16. auto list = GenerateList(size);
  17. TString bytes;
  18. TStringOutput outputStream{bytes};
  19. NodeToYsonStream(list, &outputStream, ::NYson::EYsonFormat::Binary);
  20. for (const auto& _ : state) {
  21. TStringInput inputStream{bytes};
  22. NodeFromYsonStream(&inputStream);
  23. }
  24. }
  25. static void BM_SaveLoadNonGreedy(benchmark::State& state, size_t size)
  26. {
  27. auto list = GenerateList(size);
  28. TString bytes;
  29. TStringOutput outputStream{bytes};
  30. NodeToYsonStream(list, &outputStream, ::NYson::EYsonFormat::Binary);
  31. for (const auto& _ : state) {
  32. TStringInput inputStream{bytes};
  33. NodeFromYsonStreamNonGreedy(&inputStream);
  34. }
  35. }
  36. BENCHMARK_CAPTURE(BM_SaveLoadGreedy, greedy_10, 10ul);
  37. BENCHMARK_CAPTURE(BM_SaveLoadNonGreedy, non_greedy_10, 10ul);
  38. BENCHMARK_CAPTURE(BM_SaveLoadGreedy, greedy_100, 100ul);
  39. BENCHMARK_CAPTURE(BM_SaveLoadNonGreedy, non_greedy_100, 100ul);
  40. BENCHMARK_CAPTURE(BM_SaveLoadGreedy, greedy_1000, 1000ul);
  41. BENCHMARK_CAPTURE(BM_SaveLoadNonGreedy, non_greedy_1000, 1000ul);
  42. BENCHMARK_CAPTURE(BM_SaveLoadGreedy, greedy_10000, 10000ul);
  43. BENCHMARK_CAPTURE(BM_SaveLoadNonGreedy, non_greedy_10000, 10000ul);