From e6ea02c2ee7282678e19f0748cb13efbe1442ff6 Mon Sep 17 00:00:00 2001 From: wuchenghui Date: Fri, 1 Sep 2017 16:55:07 +0800 Subject: [PATCH] add a test benchmark example --- mace/core/testing/test_benchmark.cc | 33 +++++++++++++++++++---------- mace/core/testing/test_benchmark.h | 4 ++-- mace/examples/benchmark_example.cc | 18 ++++++++++++++++ 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/mace/core/testing/test_benchmark.cc b/mace/core/testing/test_benchmark.cc index 4d158b25..17f56d0c 100644 --- a/mace/core/testing/test_benchmark.cc +++ b/mace/core/testing/test_benchmark.cc @@ -37,6 +37,18 @@ Benchmark::Benchmark(const char* name, void (*fn)(int, int, int)) Register(); } +Benchmark* Benchmark::Arg(int x) { + CHECK(num_args_ == 1); + args_.push_back(std::make_pair(x, -1)); + return this; +} + +Benchmark* Benchmark::ArgPair(int x, int y) { + CHECK(num_args_ == 2); + args_.push_back(std::make_pair(x, y)); + return this; +} + // Run all benchmarks void Benchmark::Run() { @@ -44,32 +56,31 @@ void Benchmark::Run() { // Compute name width. int width = 10; - string name; + char name[100]; for (auto b : *all_benchmarks) { - name = b->name_; for (auto arg : b->args_) { - name.resize(b->name_.size()); + strcpy(name, b->name_.c_str()); if (arg.first >= 0) { - name += "/" + arg.first; + sprintf(name, "%s/%d", name, arg.first); if (arg.second >= 0) { - name += "/" + arg.second; + sprintf(name, "%s/%d", name, arg.second); } } - width = std::max(width, name.size()); + width = std::max(width, strlen(name)); } } printf("%-*s %10s %10s\n", width, "Benchmark", "Time(ns)", "Iterations"); printf("%s\n", string(width + 22, '-').c_str()); for (auto b : *all_benchmarks) { - name = b->name_; + for (auto arg : b->args_) { - name.resize(b->name_.size()); + strcpy(name, b->name_.c_str()); if (arg.first >= 0) { - name += "/" + arg.first; + sprintf(name, "%s/%d", name, arg.first); if (arg.second >= 0) { - name += "/" + arg.second; + sprintf(name, "%s/%d", name, arg.second); } } @@ -89,7 +100,7 @@ void Benchmark::Run() { (items_processed * 1e-6) / seconds); full_label += buf; } - printf("%-*s %10.0f %10d\t%s\n", width, name.c_str(), + printf("%-*s %10.0f %10d\t%s\n", width, name, seconds * 1e9 / iters, iters, full_label.c_str()); } } diff --git a/mace/core/testing/test_benchmark.h b/mace/core/testing/test_benchmark.h index 89639941..44a352f5 100644 --- a/mace/core/testing/test_benchmark.h +++ b/mace/core/testing/test_benchmark.h @@ -24,6 +24,8 @@ class Benchmark { Benchmark(const char* name, void (*fn)(int)); Benchmark(const char* name, void (*fn)(int, int)); Benchmark(const char* name, void (*fn)(int, int, int)); + Benchmark* Arg(int x); + Benchmark* ArgPair(int x, int y); static void Run(); @@ -40,12 +42,10 @@ class Benchmark { }; void RunBenchmarks(); -void SetLabel(const std::string& label); void BytesProcessed(int64); void ItemsProcessed(int64); void StartTiming(); void StopTiming(); -void UseRealTime(); } // namespace testing } // namespace mace diff --git a/mace/examples/benchmark_example.cc b/mace/examples/benchmark_example.cc index dc3b9c4a..106c6c3c 100644 --- a/mace/examples/benchmark_example.cc +++ b/mace/examples/benchmark_example.cc @@ -25,3 +25,21 @@ static void foo(int iters) { BENCHMARK(foo); +static void bar(int iters, int n) { + const int64 tot = static_cast(iters) * n; + mace::testing::ItemsProcessed(tot); + mace::testing::BytesProcessed(tot * (sizeof(float))); + + float* inp = new float[n]; + float* out = new float[n]; + + while (iters--) { + for (int i=0; i < n; i++) { + out[i] = inp[i] * 2.0; + } + } + delete[] inp; + delete[] out; +} + +BENCHMARK(bar)->Arg(32)->Arg(64)->Arg(128); -- GitLab