benchmark/test/time_unit_gtest.cc
Bátor Tallér d08e7b6056
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
2022-03-04 11:07:01 +00:00

38 lines
880 B
C++

#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