aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBátor Tallér <55082209+batortaller@users.noreply.github.com>2022-03-04 12:07:01 +0100
committerGitHub <noreply@github.com>2022-03-04 11:07:01 +0000
commitd08e7b60564549aeaaa23d7bb075b6b9abf01d49 (patch)
tree307a4f0d196360d5be5f943c7d77dd09a2fcceef
parente33986a000987387c92ad29590a88fefe12e746b (diff)
downloadgoogle-benchmark-d08e7b60564549aeaaa23d7bb075b6b9abf01d49.tar.gz
Allow setting the default time unit globally (#1337)
* Add option to set the default time unit globally This commit introduces the `--benchmark_time_unit={ns|us|ms|s}` command line argument. The argument only affects benchmarks where the time unit is not set explicitly. * Update AUTHORS and CONTRIBUTORS * Test `SetDefaultTimeUnit` * clang format * Use `GetDefaultTimeUnit()` for initializing `TimeUnit` variables * Review fixes * Export functions * Add comment
-rw-r--r--AUTHORS1
-rw-r--r--CONTRIBUTORS1
-rw-r--r--docs/user_guide.md4
-rw-r--r--include/benchmark/benchmark.h21
-rw-r--r--src/benchmark.cc31
-rw-r--r--src/benchmark_api_internal.cc2
-rw-r--r--src/benchmark_register.cc8
-rw-r--r--test/CMakeLists.txt1
-rw-r--r--test/time_unit_gtest.cc37
9 files changed, 99 insertions, 7 deletions
diff --git a/AUTHORS b/AUTHORS
index 2b8072e..5a39872 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -52,6 +52,7 @@ Paul Redmond <paul.redmond@gmail.com>
Radoslav Yovchev <radoslav.tm@gmail.com>
Roman Lebedev <lebedev.ri@gmail.com>
Sayan Bhattacharjee <aero.sayan@gmail.com>
+Shapr3D <google-contributors@shapr3d.com>
Shuo Chen <chenshuo@chenshuo.com>
Staffan Tjernstrom <staffantj@gmail.com>
Steinar H. Gunderson <sgunderson@bigfoot.com>
diff --git a/CONTRIBUTORS b/CONTRIBUTORS
index 651fbea..35a4cc6 100644
--- a/CONTRIBUTORS
+++ b/CONTRIBUTORS
@@ -27,6 +27,7 @@ Albert Pretorius <pretoalb@gmail.com>
Alex Steele <steelal123@gmail.com>
Andriy Berestovskyy <berestovskyy@gmail.com>
Arne Beer <arne@twobeer.de>
+Bátor Tallér <bator.taller@shapr3d.com>
Billy Robert O'Neal III <billy.oneal@gmail.com> <bion@microsoft.com>
Chris Kennelly <ckennelly@google.com> <ckennelly@ckennelly.com>
Christian Wassermann <christian_wassermann@web.de>
diff --git a/docs/user_guide.md b/docs/user_guide.md
index 0fe5a4f..7c2ca31 100644
--- a/docs/user_guide.md
+++ b/docs/user_guide.md
@@ -927,6 +927,10 @@ order to manually set the time unit, you can specify it manually:
BENCHMARK(BM_test)->Unit(benchmark::kMillisecond);
```
+Additionally the default time unit can be set globally with the
+`--benchmark_time_unit={ns|us|ms|s}` command line argument. The argument only
+affects benchmarks where the time unit is not set explicitly.
+
<a name="preventing-optimization" />
## Preventing Optimization
diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h
index 4d6ce4c..7e146db 100644
--- a/include/benchmark/benchmark.h
+++ b/include/benchmark/benchmark.h
@@ -337,6 +337,16 @@ BENCHMARK_EXPORT size_t
RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
BenchmarkReporter* file_reporter, std::string spec);
+// TimeUnit is passed to a benchmark in order to specify the order of magnitude
+// for the measured time.
+enum TimeUnit { kNanosecond, kMicrosecond, kMillisecond, kSecond };
+
+BENCHMARK_EXPORT TimeUnit GetDefaultTimeUnit();
+
+// Sets the default time unit the benchmarks use
+// Has to be called before the benchmark loop to take effect
+BENCHMARK_EXPORT void SetDefaultTimeUnit(TimeUnit unit);
+
// If a MemoryManager is registered (via RegisterMemoryManager()),
// it can be used to collect and report allocation metrics for a run of the
// benchmark.
@@ -524,10 +534,6 @@ Counter::Flags inline operator|(const Counter::Flags& LHS,
// This is the container for the user-defined counters.
typedef std::map<std::string, Counter> UserCounters;
-// TimeUnit is passed to a benchmark in order to specify the order of magnitude
-// for the measured time.
-enum TimeUnit { kNanosecond, kMicrosecond, kMillisecond, kSecond };
-
// BigO is passed to a benchmark in order to specify the asymptotic
// computational
// complexity for the benchmark. In case oAuto is selected, complexity will be
@@ -1108,6 +1114,8 @@ class BENCHMARK_EXPORT Benchmark {
virtual void Run(State& state) = 0;
+ TimeUnit GetTimeUnit() const;
+
protected:
explicit Benchmark(const char* name);
void SetName(const char* name);
@@ -1122,7 +1130,10 @@ class BENCHMARK_EXPORT Benchmark {
AggregationReportMode aggregation_report_mode_;
std::vector<std::string> arg_names_; // Args for all benchmark runs
std::vector<std::vector<int64_t> > args_; // Args for all benchmark runs
+
TimeUnit time_unit_;
+ bool use_default_time_unit_;
+
int range_multiplier_;
double min_time_;
IterationCount iterations_;
@@ -1555,7 +1566,7 @@ class BENCHMARK_EXPORT BenchmarkReporter {
error_occurred(false),
iterations(1),
threads(1),
- time_unit(kNanosecond),
+ time_unit(GetDefaultTimeUnit()),
real_accumulated_time(0),
cpu_accumulated_time(0),
max_heapbytes_used(0),
diff --git a/src/benchmark.cc b/src/benchmark.cc
index d915843..57958b1 100644
--- a/src/benchmark.cc
+++ b/src/benchmark.cc
@@ -121,6 +121,10 @@ BM_DEFINE_string(benchmark_perf_counters, "");
// pairs. Kept internal as it's only used for parsing from env/command line.
BM_DEFINE_kvpairs(benchmark_context, {});
+// Set the default time unit to use for reports
+// Valid values are 'ns', 'us', 'ms' or 's'
+BM_DEFINE_string(benchmark_time_unit, "");
+
// The level of verbose logging to output
BM_DEFINE_int32(v, 0);
@@ -520,6 +524,15 @@ size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
return benchmarks.size();
}
+namespace {
+// stores the time unit benchmarks use by default
+TimeUnit default_time_unit = kNanosecond;
+} // namespace
+
+TimeUnit GetDefaultTimeUnit() { return default_time_unit; }
+
+void SetDefaultTimeUnit(TimeUnit unit) { default_time_unit = unit; }
+
std::string GetBenchmarkFilter() { return FLAGS_benchmark_filter; }
void RegisterMemoryManager(MemoryManager* manager) {
@@ -559,11 +572,26 @@ void PrintUsageAndExit() {
" [--benchmark_color={auto|true|false}]\n"
" [--benchmark_counters_tabular={true|false}]\n"
" [--benchmark_context=<key>=<value>,...]\n"
+ " [--benchmark_time_unit={ns|us|ms|s}]\n"
" [--v=<verbosity>]\n");
}
exit(0);
}
+void SetDefaultTimeUnitFromFlag(const std::string& time_unit_flag) {
+ if (time_unit_flag == "s") {
+ return SetDefaultTimeUnit(kSecond);
+ } else if (time_unit_flag == "ms") {
+ return SetDefaultTimeUnit(kMillisecond);
+ } else if (time_unit_flag == "us") {
+ return SetDefaultTimeUnit(kMicrosecond);
+ } else if (time_unit_flag == "ns") {
+ return SetDefaultTimeUnit(kNanosecond);
+ } else if (!time_unit_flag.empty()) {
+ PrintUsageAndExit();
+ }
+}
+
void ParseCommandLineFlags(int* argc, char** argv) {
using namespace benchmark;
BenchmarkReporter::Context::executable_name =
@@ -593,6 +621,8 @@ void ParseCommandLineFlags(int* argc, char** argv) {
&FLAGS_benchmark_perf_counters) ||
ParseKeyValueFlag(argv[i], "benchmark_context",
&FLAGS_benchmark_context) ||
+ ParseStringFlag(argv[i], "benchmark_time_unit",
+ &FLAGS_benchmark_time_unit) ||
ParseInt32Flag(argv[i], "v", &FLAGS_v)) {
for (int j = i; j != *argc - 1; ++j) argv[j] = argv[j + 1];
@@ -608,6 +638,7 @@ void ParseCommandLineFlags(int* argc, char** argv) {
PrintUsageAndExit();
}
}
+ SetDefaultTimeUnitFromFlag(FLAGS_benchmark_time_unit);
if (FLAGS_benchmark_color.empty()) {
PrintUsageAndExit();
}
diff --git a/src/benchmark_api_internal.cc b/src/benchmark_api_internal.cc
index 4de36e3..6b90cfa 100644
--- a/src/benchmark_api_internal.cc
+++ b/src/benchmark_api_internal.cc
@@ -16,7 +16,7 @@ BenchmarkInstance::BenchmarkInstance(Benchmark* benchmark, int family_idx,
per_family_instance_index_(per_family_instance_idx),
aggregation_report_mode_(benchmark_.aggregation_report_mode_),
args_(args),
- time_unit_(benchmark_.time_unit_),
+ time_unit_(benchmark_.GetTimeUnit()),
measure_process_cpu_time_(benchmark_.measure_process_cpu_time_),
use_real_time_(benchmark_.use_real_time_),
use_manual_time_(benchmark_.use_manual_time_),
diff --git a/src/benchmark_register.cc b/src/benchmark_register.cc
index 61a0c26..36b3a0b 100644
--- a/src/benchmark_register.cc
+++ b/src/benchmark_register.cc
@@ -202,7 +202,8 @@ bool FindBenchmarksInternal(const std::string& re,
Benchmark::Benchmark(const char* name)
: name_(name),
aggregation_report_mode_(ARM_Unspecified),
- time_unit_(kNanosecond),
+ time_unit_(GetDefaultTimeUnit()),
+ use_default_time_unit_(true),
range_multiplier_(kRangeMultiplier),
min_time_(0),
iterations_(0),
@@ -235,6 +236,7 @@ Benchmark* Benchmark::Arg(int64_t x) {
Benchmark* Benchmark::Unit(TimeUnit unit) {
time_unit_ = unit;
+ use_default_time_unit_ = false;
return this;
}
@@ -462,6 +464,10 @@ int Benchmark::ArgsCnt() const {
return static_cast<int>(args_.front().size());
}
+TimeUnit Benchmark::GetTimeUnit() const {
+ return use_default_time_unit_ ? GetDefaultTimeUnit() : time_unit_;
+}
+
//=============================================================================//
// FunctionBenchmark
//=============================================================================//
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 205be91..d5eb25d 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -219,6 +219,7 @@ if (BENCHMARK_ENABLE_GTEST_TESTS)
add_gtest(statistics_gtest)
add_gtest(string_util_gtest)
add_gtest(perf_counters_gtest)
+ add_gtest(time_unit_gtest)
endif(BENCHMARK_ENABLE_GTEST_TESTS)
###############################################################################
diff --git a/test/time_unit_gtest.cc b/test/time_unit_gtest.cc
new file mode 100644
index 0000000..ae53743
--- /dev/null
+++ b/test/time_unit_gtest.cc
@@ -0,0 +1,37 @@
+#include "../include/benchmark/benchmark.h"
+#include "gtest/gtest.h"
+
+namespace benchmark {
+namespace internal {
+
+namespace {
+
+class DummyBenchmark : public Benchmark {
+ public:
+ DummyBenchmark() : Benchmark("dummy") {}
+ virtual void Run(State&) override {}
+};
+
+TEST(DefaultTimeUnitTest, TimeUnitIsNotSet) {
+ DummyBenchmark benchmark;
+ EXPECT_EQ(benchmark.GetTimeUnit(), kNanosecond);
+}
+
+TEST(DefaultTimeUnitTest, DefaultIsSet) {
+ DummyBenchmark benchmark;
+ EXPECT_EQ(benchmark.GetTimeUnit(), kNanosecond);
+ SetDefaultTimeUnit(kMillisecond);
+ EXPECT_EQ(benchmark.GetTimeUnit(), kMillisecond);
+}
+
+TEST(DefaultTimeUnitTest, DefaultAndExplicitUnitIsSet) {
+ DummyBenchmark benchmark;
+ benchmark.Unit(kMillisecond);
+ SetDefaultTimeUnit(kMicrosecond);
+
+ EXPECT_EQ(benchmark.GetTimeUnit(), kMillisecond);
+}
+
+} // namespace
+} // namespace internal
+} // namespace benchmark