From 378ed8ff25c01fe55c35cad6ed1eeab02c15d49a Mon Sep 17 00:00:00 2001 From: feserr Date: Mon, 21 Dec 2020 18:15:58 +0100 Subject: Add 'seconds' time unit (#1076) Fixes #1075. * Add an option to report in seconds. * Reduce the time of the test. * Add CSV/JSON tests for new time reports. --- bindings/python/google_benchmark/__init__.py | 2 + bindings/python/google_benchmark/benchmark.cc | 1 + include/benchmark/benchmark.h | 6 +- test/options_test.cc | 1 + test/output_test_helper.cc | 5 ++ test/reporter_output_test.cc | 89 ++++++++++++++++++++++++++- 6 files changed, 102 insertions(+), 2 deletions(-) diff --git a/bindings/python/google_benchmark/__init__.py b/bindings/python/google_benchmark/__init__.py index 787c423..f31285e 100644 --- a/bindings/python/google_benchmark/__init__.py +++ b/bindings/python/google_benchmark/__init__.py @@ -34,6 +34,7 @@ from google_benchmark._benchmark import ( kNanosecond, kMicrosecond, kMillisecond, + kSecond, oNone, o1, oN, @@ -53,6 +54,7 @@ __all__ = [ "kNanosecond", "kMicrosecond", "kMillisecond", + "kSecond", "oNone", "o1", "oN", diff --git a/bindings/python/google_benchmark/benchmark.cc b/bindings/python/google_benchmark/benchmark.cc index a733339..d80816e 100644 --- a/bindings/python/google_benchmark/benchmark.cc +++ b/bindings/python/google_benchmark/benchmark.cc @@ -49,6 +49,7 @@ PYBIND11_MODULE(_benchmark, m) { .value("kNanosecond", TimeUnit::kNanosecond) .value("kMicrosecond", TimeUnit::kMicrosecond) .value("kMillisecond", TimeUnit::kMillisecond) + .value("kSecond", TimeUnit::kSecond) .export_values(); using benchmark::BigO; diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index 834687e..f57e3e7 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -407,7 +407,7 @@ typedef std::map UserCounters; // TimeUnit is passed to a benchmark in order to specify the order of magnitude // for the measured time. -enum TimeUnit { kNanosecond, kMicrosecond, kMillisecond }; +enum TimeUnit { kNanosecond, kMicrosecond, kMillisecond, kSecond }; // BigO is passed to a benchmark in order to specify the asymptotic // computational @@ -1577,6 +1577,8 @@ class MemoryManager { inline const char* GetTimeUnitString(TimeUnit unit) { switch (unit) { + case kSecond: + return "s"; case kMillisecond: return "ms"; case kMicrosecond: @@ -1589,6 +1591,8 @@ inline const char* GetTimeUnitString(TimeUnit unit) { inline double GetTimeUnitMultiplier(TimeUnit unit) { switch (unit) { + case kSecond: + return 1; case kMillisecond: return 1e3; case kMicrosecond: diff --git a/test/options_test.cc b/test/options_test.cc index 7bfc235..9f9a786 100644 --- a/test/options_test.cc +++ b/test/options_test.cc @@ -25,6 +25,7 @@ BENCHMARK(BM_basic)->Arg(42); BENCHMARK(BM_basic_slow)->Arg(10)->Unit(benchmark::kNanosecond); BENCHMARK(BM_basic_slow)->Arg(100)->Unit(benchmark::kMicrosecond); BENCHMARK(BM_basic_slow)->Arg(1000)->Unit(benchmark::kMillisecond); +BENCHMARK(BM_basic_slow)->Arg(1000)->Unit(benchmark::kSecond); BENCHMARK(BM_basic)->Range(1, 8); BENCHMARK(BM_basic)->RangeMultiplier(2)->Range(1, 8); BENCHMARK(BM_basic)->DenseRange(10, 15); diff --git a/test/output_test_helper.cc b/test/output_test_helper.cc index f99b3a8..1aebc55 100644 --- a/test/output_test_helper.cc +++ b/test/output_test_helper.cc @@ -48,6 +48,9 @@ SubMap& GetSubstitutions() { {" %s ", "[ ]+"}, {"%time", "[ ]*" + time_re + "[ ]+ns"}, {"%console_report", "[ ]*" + time_re + "[ ]+ns [ ]*" + time_re + "[ ]+ns [ ]*[0-9]+"}, + {"%console_us_report", "[ ]*" + time_re + "[ ]+us [ ]*" + time_re + "[ ]+us [ ]*[0-9]+"}, + {"%console_ms_report", "[ ]*" + time_re + "[ ]+ms [ ]*" + time_re + "[ ]+ms [ ]*[0-9]+"}, + {"%console_s_report", "[ ]*" + time_re + "[ ]+s [ ]*" + time_re + "[ ]+s [ ]*[0-9]+"}, {"%console_time_only_report", "[ ]*" + time_re + "[ ]+ns [ ]*" + time_re + "[ ]+ns"}, {"%console_us_report", "[ ]*" + time_re + "[ ]+us [ ]*" + time_re + "[ ]+us [ ]*[0-9]+"}, {"%console_us_time_only_report", "[ ]*" + time_re + "[ ]+us [ ]*" + time_re + "[ ]+us"}, @@ -56,6 +59,8 @@ SubMap& GetSubstitutions() { "items_per_second,label,error_occurred,error_message"}, {"%csv_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns,,,,,"}, {"%csv_us_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",us,,,,,"}, + {"%csv_ms_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ms,,,,,"}, + {"%csv_s_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",s,,,,,"}, {"%csv_bytes_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns," + safe_dec_re + ",,,,"}, {"%csv_items_report", diff --git a/test/reporter_output_test.cc b/test/reporter_output_test.cc index bcce007..d24a57d 100644 --- a/test/reporter_output_test.cc +++ b/test/reporter_output_test.cc @@ -168,6 +168,93 @@ ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_label\",$"}, ADD_CASES(TC_CSVOut, {{"^\"BM_label\",%csv_label_report_begin\"some " "label\"%csv_label_report_end$"}}); +// ========================================================================= // +// ------------------------ Testing Time Label Output ---------------------- // +// ========================================================================= // + +void BM_time_label_nanosecond(benchmark::State& state) { + for (auto _ : state) { + } +} +BENCHMARK(BM_time_label_nanosecond)->Unit(benchmark::kNanosecond); + +ADD_CASES(TC_ConsoleOut, {{"^BM_time_label_nanosecond %console_report$"}}); +ADD_CASES(TC_JSONOut, + {{"\"name\": \"BM_time_label_nanosecond\",$"}, + {"\"run_name\": \"BM_time_label_nanosecond\",$", MR_Next}, + {"\"run_type\": \"iteration\",$", MR_Next}, + {"\"repetitions\": 0,$", MR_Next}, + {"\"repetition_index\": 0,$", MR_Next}, + {"\"threads\": 1,$", MR_Next}, + {"\"iterations\": %int,$", MR_Next}, + {"\"real_time\": %float,$", MR_Next}, + {"\"cpu_time\": %float,$", MR_Next}, + {"\"time_unit\": \"ns\"$", MR_Next}, + {"}", MR_Next}}); +ADD_CASES(TC_CSVOut, {{"^\"BM_time_label_nanosecond\",%csv_report$"}}); + +void BM_time_label_microsecond(benchmark::State& state) { + for (auto _ : state) { + } +} +BENCHMARK(BM_time_label_microsecond)->Unit(benchmark::kMicrosecond); + +ADD_CASES(TC_ConsoleOut, {{"^BM_time_label_microsecond %console_us_report$"}}); +ADD_CASES(TC_JSONOut, + {{"\"name\": \"BM_time_label_microsecond\",$"}, + {"\"run_name\": \"BM_time_label_microsecond\",$", MR_Next}, + {"\"run_type\": \"iteration\",$", MR_Next}, + {"\"repetitions\": 0,$", MR_Next}, + {"\"repetition_index\": 0,$", MR_Next}, + {"\"threads\": 1,$", MR_Next}, + {"\"iterations\": %int,$", MR_Next}, + {"\"real_time\": %float,$", MR_Next}, + {"\"cpu_time\": %float,$", MR_Next}, + {"\"time_unit\": \"us\"$", MR_Next}, + {"}", MR_Next}}); +ADD_CASES(TC_CSVOut, {{"^\"BM_time_label_microsecond\",%csv_us_report$"}}); + +void BM_time_label_millisecond(benchmark::State& state) { + for (auto _ : state) { + } +} +BENCHMARK(BM_time_label_millisecond)->Unit(benchmark::kMillisecond); + +ADD_CASES(TC_ConsoleOut, {{"^BM_time_label_millisecond %console_ms_report$"}}); +ADD_CASES(TC_JSONOut, + {{"\"name\": \"BM_time_label_millisecond\",$"}, + {"\"run_name\": \"BM_time_label_millisecond\",$", MR_Next}, + {"\"run_type\": \"iteration\",$", MR_Next}, + {"\"repetitions\": 0,$", MR_Next}, + {"\"repetition_index\": 0,$", MR_Next}, + {"\"threads\": 1,$", MR_Next}, + {"\"iterations\": %int,$", MR_Next}, + {"\"real_time\": %float,$", MR_Next}, + {"\"cpu_time\": %float,$", MR_Next}, + {"\"time_unit\": \"ms\"$", MR_Next}, + {"}", MR_Next}}); +ADD_CASES(TC_CSVOut, {{"^\"BM_time_label_millisecond\",%csv_ms_report$"}}); + +void BM_time_label_second(benchmark::State& state) { + for (auto _ : state) { + } +} +BENCHMARK(BM_time_label_second)->Unit(benchmark::kSecond); + +ADD_CASES(TC_ConsoleOut, {{"^BM_time_label_second %console_s_report$"}}); +ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_time_label_second\",$"}, + {"\"run_name\": \"BM_time_label_second\",$", MR_Next}, + {"\"run_type\": \"iteration\",$", MR_Next}, + {"\"repetitions\": 0,$", MR_Next}, + {"\"repetition_index\": 0,$", MR_Next}, + {"\"threads\": 1,$", MR_Next}, + {"\"iterations\": %int,$", MR_Next}, + {"\"real_time\": %float,$", MR_Next}, + {"\"cpu_time\": %float,$", MR_Next}, + {"\"time_unit\": \"s\"$", MR_Next}, + {"}", MR_Next}}); +ADD_CASES(TC_CSVOut, {{"^\"BM_time_label_second\",%csv_s_report$"}}); + // ========================================================================= // // ------------------------ Testing Error Output --------------------------- // // ========================================================================= // @@ -712,7 +799,7 @@ ADD_CASES( // ========================================================================= // // ------------------------- Testing StrEscape JSON ------------------------ // // ========================================================================= // -#if 0 // enable when csv testing code correctly handles multi-line fields +#if 0 // enable when csv testing code correctly handles multi-line fields void BM_JSON_Format(benchmark::State& state) { state.SkipWithError("val\b\f\n\r\t\\\"with\"es,capes"); for (auto _ : state) { -- cgit v1.2.3 From d8254bb9eb5f6deeddee639d0b27347e186e0a84 Mon Sep 17 00:00:00 2001 From: Aidan Wolter Date: Tue, 22 Dec 2020 01:54:33 -0800 Subject: Add bazel target for benchmark_release (#1078) Fixes google#1077 Bazel clients currently cannot build the benchmark library in Release mode. This commit adds a new target ":benchmark_release" to enable this. --- BUILD.bazel | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index eb35b62..2d87177 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -10,8 +10,8 @@ config_setting( visibility = [":__subpackages__"], ) -cc_library( - name = "benchmark", +filegroup( + name = "benchmark_srcs", srcs = glob( [ "src/*.cc", @@ -19,7 +19,25 @@ cc_library( ], exclude = ["src/benchmark_main.cc"], ), +) + +cc_library( + name = "benchmark", + srcs = [":benchmark_srcs"], + hdrs = ["include/benchmark/benchmark.h"], + linkopts = select({ + ":windows": ["-DEFAULTLIB:shlwapi.lib"], + "//conditions:default": ["-pthread"], + }), + strip_include_prefix = "include", + visibility = ["//visibility:public"], +) + +cc_library( + name = "benchmark_release", + srcs = [":benchmark_srcs"], hdrs = ["include/benchmark/benchmark.h"], + defines = ["NDEBUG"], linkopts = select({ ":windows": ["-DEFAULTLIB:shlwapi.lib"], "//conditions:default": ["-pthread"], -- cgit v1.2.3 From a6d08aea4b70c5532736924377df8be62ef2067a Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Tue, 22 Dec 2020 11:47:52 +0000 Subject: Create workflow to exercise bazel build (#1079) * Create workflow to exercise bazel build --- .github/workflows/bazel.yml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .github/workflows/bazel.yml diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml new file mode 100644 index 0000000..d6bbe62 --- /dev/null +++ b/.github/workflows/bazel.yml @@ -0,0 +1,33 @@ +name: bazel + +on: + push: {} + pull_request: {} + +jobs: + build-and-test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + + - name: mount bazel cache + uses: actions/cache@v1 + with: + path: "/home/runner/.cache/bazel" + key: bazel + + - name: install bazelisk + run: | + curl -LO "https://github.com/bazelbuild/bazelisk/releases/download/v1.1.0/bazelisk-linux-amd64" + mkdir -p "${GITHUB_WORKSPACE}/bin/" + mv bazelisk-linux-amd64 "${GITHUB_WORKSPACE}/bin/bazel" + chmod +x "${GITHUB_WORKSPACE}/bin/bazel" + + - name: build + run: | + "${GITHUB_WORKSPACE}/bin/bazel" build //... + + - name: test + run: | + "${GITHUB_WORKSPACE}/bin/bazel" test //test/... -- cgit v1.2.3 From 8df87f6c879cbcabd17c5cfcec7b89687df36953 Mon Sep 17 00:00:00 2001 From: Yannic Date: Tue, 5 Jan 2021 10:54:04 +0100 Subject: Revert "Add bazel target for benchmark_release (#1078)" (#1081) This reverts commit d8254bb9eb5f6deeddee639d0b27347e186e0a84. --- BUILD.bazel | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 2d87177..eb35b62 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -10,8 +10,8 @@ config_setting( visibility = [":__subpackages__"], ) -filegroup( - name = "benchmark_srcs", +cc_library( + name = "benchmark", srcs = glob( [ "src/*.cc", @@ -19,25 +19,7 @@ filegroup( ], exclude = ["src/benchmark_main.cc"], ), -) - -cc_library( - name = "benchmark", - srcs = [":benchmark_srcs"], - hdrs = ["include/benchmark/benchmark.h"], - linkopts = select({ - ":windows": ["-DEFAULTLIB:shlwapi.lib"], - "//conditions:default": ["-pthread"], - }), - strip_include_prefix = "include", - visibility = ["//visibility:public"], -) - -cc_library( - name = "benchmark_release", - srcs = [":benchmark_srcs"], hdrs = ["include/benchmark/benchmark.h"], - defines = ["NDEBUG"], linkopts = select({ ":windows": ["-DEFAULTLIB:shlwapi.lib"], "//conditions:default": ["-pthread"], -- cgit v1.2.3