diff options
author | yazevnul <yazevnul@yandex-team.ru> | 2022-02-10 16:46:48 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:48 +0300 |
commit | 9abfb1a53b7f7b791444d1378e645d8fad9b06ed (patch) | |
tree | 49e222ea1c5804306084bb3ae065bb702625360f /library/cpp/testing/benchmark/bench.cpp | |
parent | 8cbc307de0221f84c80c42dcbe07d40727537e2c (diff) | |
download | ydb-9abfb1a53b7f7b791444d1378e645d8fad9b06ed.tar.gz |
Restoring authorship annotation for <yazevnul@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/testing/benchmark/bench.cpp')
-rw-r--r-- | library/cpp/testing/benchmark/bench.cpp | 368 |
1 files changed, 184 insertions, 184 deletions
diff --git a/library/cpp/testing/benchmark/bench.cpp b/library/cpp/testing/benchmark/bench.cpp index dc7f8b7856..08d8708005 100644 --- a/library/cpp/testing/benchmark/bench.cpp +++ b/library/cpp/testing/benchmark/bench.cpp @@ -1,7 +1,7 @@ #include "bench.h" -#include <contrib/libs/re2/re2/re2.h> - +#include <contrib/libs/re2/re2/re2.h> + #include <library/cpp/colorizer/output.h> #include <library/cpp/getopt/small/last_getopt.h> #include <library/cpp/json/json_value.h> @@ -9,7 +9,7 @@ #include <library/cpp/threading/poor_man_openmp/thread_helper.h> #include <util/system/hp_timer.h> -#include <util/system/info.h> +#include <util/system/info.h> #include <util/stream/output.h> #include <util/datetime/base.h> #include <util/random/random.h> @@ -26,7 +26,7 @@ #include <util/system/yield.h> using re2::RE2; - + using namespace NBench; using namespace NColorizer; using namespace NLastGetopt; @@ -47,7 +47,7 @@ namespace { }; struct ITestRunner: public TIntrusiveListItem<ITestRunner> { - virtual ~ITestRunner() = default; + virtual ~ITestRunner() = default; void Register(); virtual TStringBuf Name() const noexcept = 0; @@ -278,115 +278,115 @@ namespace { F(params); }, opts.TimeBudget, *this); } - - enum EOutFormat { - F_CONSOLE = 0 /* "console" */, + + enum EOutFormat { + F_CONSOLE = 0 /* "console" */, F_CSV /* "csv" */, F_JSON /* "json" */ - }; - + }; + TAdaptiveLock STDOUT_LOCK; - + struct IReporter { - virtual void Report(TResult&& result) = 0; - - virtual void Finish() { - } - - virtual ~IReporter() { - } - }; - + virtual void Report(TResult&& result) = 0; + + virtual void Finish() { + } + + virtual ~IReporter() { + } + }; + class TConsoleReporter: public IReporter { - public: + public: ~TConsoleReporter() override { - } - - void Report(TResult&& r) override { - with_lock (STDOUT_LOCK) { - Cout << r; - } - } - }; - + } + + void Report(TResult&& r) override { + with_lock (STDOUT_LOCK) { + Cout << r; + } + } + }; + class TCSVReporter: public IReporter { - public: - TCSVReporter() { + public: + TCSVReporter() { Cout << "Name\tSamples\tIterations\tRun_time\tPer_iteration_sec\tPer_iteration_cycles" << Endl; - } - + } + ~TCSVReporter() override { - } - - void Report(TResult&& r) override { - with_lock (STDOUT_LOCK) { - Cout << r.TestName - << '\t' << r.Samples - << '\t' << r.Iterations - << '\t' << r.RunTime; - - Cout << '\t'; - if (r.CyclesPerIteration) { - Cout << TCycleTimer::FmtTime(*r.CyclesPerIteration); - } else { - Cout << '-'; - } - - Cout << '\t'; - if (r.SecondsPerIteration) { - Cout << DoFmtTime(*r.SecondsPerIteration); - } else { - Cout << '-'; - } - - Cout << Endl; - } - } - }; - + } + + void Report(TResult&& r) override { + with_lock (STDOUT_LOCK) { + Cout << r.TestName + << '\t' << r.Samples + << '\t' << r.Iterations + << '\t' << r.RunTime; + + Cout << '\t'; + if (r.CyclesPerIteration) { + Cout << TCycleTimer::FmtTime(*r.CyclesPerIteration); + } else { + Cout << '-'; + } + + Cout << '\t'; + if (r.SecondsPerIteration) { + Cout << DoFmtTime(*r.SecondsPerIteration); + } else { + Cout << '-'; + } + + Cout << Endl; + } + } + }; + class TJSONReporter: public IReporter { - public: + public: ~TJSONReporter() override { - } - - void Report(TResult&& r) override { - with_lock (ResultsLock_) { - Results_.emplace_back(std::move(r)); - } - } - - void Finish() override { - NJson::TJsonValue report; - auto& bench = report["benchmark"]; - bench.SetType(NJson::JSON_ARRAY); - - NJson::TJsonValue benchReport; - - for (const auto& result : Results_) { - NJson::TJsonValue{}.Swap(benchReport); - benchReport["name"] = result.TestName; - benchReport["samples"] = result.Samples; - benchReport["run_time"] = result.RunTime; - - if (result.CyclesPerIteration) { - benchReport["per_iteration_cycles"] = *result.CyclesPerIteration; - } - - if (result.SecondsPerIteration) { - benchReport["per_iteration_secons"] = *result.SecondsPerIteration; - } - - bench.AppendValue(benchReport); - } - - Cout << report << Endl; - } - - private: - TAdaptiveLock ResultsLock_; + } + + void Report(TResult&& r) override { + with_lock (ResultsLock_) { + Results_.emplace_back(std::move(r)); + } + } + + void Finish() override { + NJson::TJsonValue report; + auto& bench = report["benchmark"]; + bench.SetType(NJson::JSON_ARRAY); + + NJson::TJsonValue benchReport; + + for (const auto& result : Results_) { + NJson::TJsonValue{}.Swap(benchReport); + benchReport["name"] = result.TestName; + benchReport["samples"] = result.Samples; + benchReport["run_time"] = result.RunTime; + + if (result.CyclesPerIteration) { + benchReport["per_iteration_cycles"] = *result.CyclesPerIteration; + } + + if (result.SecondsPerIteration) { + benchReport["per_iteration_secons"] = *result.SecondsPerIteration; + } + + bench.AppendValue(benchReport); + } + + Cout << report << Endl; + } + + private: + TAdaptiveLock ResultsLock_; TVector<TResult> Results_; - }; - + }; + class TOrderedReporter: public IReporter { public: TOrderedReporter(THolder<IReporter> slave) @@ -421,22 +421,22 @@ namespace { }; THolder<IReporter> MakeReporter(const EOutFormat type) { - switch (type) { - case F_CONSOLE: - return MakeHolder<TConsoleReporter>(); - - case F_CSV: - return MakeHolder<TCSVReporter>(); + switch (type) { + case F_CONSOLE: + return MakeHolder<TConsoleReporter>(); + + case F_CSV: + return MakeHolder<TCSVReporter>(); - case F_JSON: - return MakeHolder<TJSONReporter>(); + case F_JSON: + return MakeHolder<TJSONReporter>(); default: break; - } + } return MakeHolder<TConsoleReporter>(); // make compiler happy - } + } THolder<IReporter> MakeOrderedReporter(const EOutFormat type) { return MakeHolder<TOrderedReporter>(MakeReporter(type)); @@ -448,24 +448,24 @@ namespace { } } } - -template <> -EOutFormat FromStringImpl<EOutFormat>(const char* data, size_t len) { - const auto s = TStringBuf{data, len}; + +template <> +EOutFormat FromStringImpl<EOutFormat>(const char* data, size_t len) { + const auto s = TStringBuf{data, len}; if (TStringBuf("console") == s) { - return F_CONSOLE; + return F_CONSOLE; } else if (TStringBuf("csv") == s) { - return F_CSV; + return F_CSV; } else if (TStringBuf("json") == s) { - return F_JSON; - } - - ythrow TFromStringException{} << "failed to convert '" << s << '\''; + return F_JSON; + } + + ythrow TFromStringException{} << "failed to convert '" << s << '\''; } template <> -void Out<TResult>(IOutputStream& out, const TResult& r) { +void Out<TResult>(IOutputStream& out, const TResult& r) { out << "----------- " << LightRed() << r.TestName << Old() << " ---------------" << Endl << " samples: " << White() << r.Samples << Old() << Endl << " iterations: " << White() << r.Iterations << Old() << Endl @@ -482,9 +482,9 @@ void Out<TResult>(IOutputStream& out, const TResult& r) { } NCpu::TRegistar::TRegistar(const char* name, TUserFunc func) { - static_assert(sizeof(TCpuBenchmark) + alignof(TCpuBenchmark) < sizeof(Buf), "fix Buf size"); + static_assert(sizeof(TCpuBenchmark) + alignof(TCpuBenchmark) < sizeof(Buf), "fix Buf size"); - new (AlignUp(Buf, alignof(TCpuBenchmark))) TCpuBenchmark(name, func); + new (AlignUp(Buf, alignof(TCpuBenchmark))) TCpuBenchmark(name, func); } namespace { @@ -496,36 +496,36 @@ namespace { opts.AddLongOption('b', "budget") .StoreResult(&TimeBudget) - .RequiredArgument("SEC") + .RequiredArgument("SEC") .Optional() .Help("overall time budget"); opts.AddLongOption('l', "list") - .NoArgument() - .StoreValue(&ListTests, true) + .NoArgument() + .StoreValue(&ListTests, true) .Help("list all tests"); opts.AddLongOption('t', "threads") .StoreResult(&Threads) - .OptionalValue(ToString((NSystemInfo::CachedNumberOfCpus() + 1) / 2), "JOBS") - .DefaultValue("1") + .OptionalValue(ToString((NSystemInfo::CachedNumberOfCpus() + 1) / 2), "JOBS") + .DefaultValue("1") .Help("run benchmarks in parallel"); - opts.AddLongOption('f', "format") + opts.AddLongOption('f', "format") .AddLongName("benchmark_format") - .StoreResult(&OutFormat) - .RequiredArgument("FORMAT") - .DefaultValue("console") - .Help("output format (console|csv|json)"); - - opts.SetFreeArgDefaultTitle("REGEXP", "RE2 regular expression to filter tests"); - - const TOptsParseResult parseResult{&opts, argc, argv}; - - for (const auto& regexp : parseResult.GetFreeArgs()) { + .StoreResult(&OutFormat) + .RequiredArgument("FORMAT") + .DefaultValue("console") + .Help("output format (console|csv|json)"); + + opts.SetFreeArgDefaultTitle("REGEXP", "RE2 regular expression to filter tests"); + + const TOptsParseResult parseResult{&opts, argc, argv}; + + for (const auto& regexp : parseResult.GetFreeArgs()) { Filters.push_back(MakeHolder<RE2>(regexp.data(), RE2::Quiet)); - Y_ENSURE(Filters.back()->ok(), "incorrect RE2 expression '" << regexp << "'"); - } + Y_ENSURE(Filters.back()->ok(), "incorrect RE2 expression '" << regexp << "'"); + } } bool MatchFilters(const TStringBuf& name) const { @@ -533,72 +533,72 @@ namespace { return true; } - for (auto&& re : Filters) { + for (auto&& re : Filters) { if (RE2::FullMatchN({name.data(), name.size()}, *re, nullptr, 0)) { - return true; - } - } - - return false; - } - + return true; + } + } + + return false; + } + bool ListTests = false; double TimeBudget = -1.0; TVector<THolder<RE2>> Filters; size_t Threads = 0; - EOutFormat OutFormat; + EOutFormat OutFormat; }; } -int NBench::Main(int argc, char** argv) { - const TProgOpts opts(argc, argv); +int NBench::Main(int argc, char** argv) { + const TProgOpts opts(argc, argv); TVector<ITestRunner*> tests; - - for (auto&& it : Tests()) { - if (opts.MatchFilters(it.Name())) { - tests.push_back(&it); + + for (auto&& it : Tests()) { + if (opts.MatchFilters(it.Name())) { + tests.push_back(&it); } - } + } EnumerateTests(tests); - if (opts.ListTests) { - for (const auto* const it : tests) { - Cout << it->Name() << Endl; + if (opts.ListTests) { + for (const auto* const it : tests) { + Cout << it->Name() << Endl; } - return 0; - } + return 0; + } - if (!tests) { - return 0; - } + if (!tests) { + return 0; + } - double timeBudget = opts.TimeBudget; + double timeBudget = opts.TimeBudget; - if (timeBudget < 0) { + if (timeBudget < 0) { timeBudget = 5.0 * tests.size(); - } + } const TOptions testOpts = {timeBudget / tests.size()}; const auto reporter = MakeOrderedReporter(opts.OutFormat); std::function<void(ITestRunner**)> func = [&](ITestRunner** it) { - auto&& res = (*it)->Run(testOpts); + auto&& res = (*it)->Run(testOpts); - reporter->Report(std::move(res)); - }; - - if (opts.Threads > 1) { - NYmp::SetThreadCount(opts.Threads); + reporter->Report(std::move(res)); + }; + + if (opts.Threads > 1) { + NYmp::SetThreadCount(opts.Threads); NYmp::ParallelForStaticChunk(tests.data(), tests.data() + tests.size(), 1, func); - } else { - for (auto it : tests) { - func(&it); - } + } else { + for (auto it : tests) { + func(&it); + } } - - reporter->Finish(); - - return 0; + + reporter->Finish(); + + return 0; } |