Skip to content

Commit 9dd521f

Browse files
authored
Merge 3d325d6 into af441fc
2 parents af441fc + 3d325d6 commit 9dd521f

File tree

6 files changed

+149
-6
lines changed

6 files changed

+149
-6
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,10 @@ In multithreaded benchmarks, each counter is set on the calling thread only.
657657
When the benchmark finishes, the counters from each thread will be summed;
658658
the resulting sum is the value which will be shown for the benchmark.
659659
660-
The `Counter` constructor accepts two parameters: the value as a `double`
661-
and a bit flag which allows you to show counters as rates and/or as
662-
per-thread averages:
660+
The `Counter` constructor accepts three parameters: the value as a `double`
661+
; a bit flag which allows you to show counters as rates, and/or as per-thread
662+
iteration, and/or as per-thread averages, and/or iteration invariants;
663+
and a 'thousand' multiplier - i.e. what should be counted as 1k - 1000 (default) or 1024?
663664
664665
```c++
665666
// sets a simple counter
@@ -675,6 +676,9 @@ per-thread averages:
675676
676677
// There's also a combined flag:
677678
state.counters["FooAvgRate"] = Counter(numFoos,benchmark::Counter::kAvgThreadsRate);
679+
680+
// This says that we process with the rate of state.range(0) bytes every iteration:
681+
state.counters["BytesProcessed"] = Counter(state.range(0), benchmark::Counter::kIsIterationInvariantRate, 1024);
678682
```
679683

680684
When you're compiling in C++11 mode or later you can use `insert()` with

include/benchmark/benchmark.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,11 @@ class Counter {
375375

376376
double value;
377377
Flags flags;
378+
int thousand;
378379

379380
BENCHMARK_ALWAYS_INLINE
380-
Counter(double v = 0., Flags f = kDefaults) : value(v), flags(f) {}
381+
Counter(double v = 0., Flags f = kDefaults, int t = 1000)
382+
: value(v), flags(f), thousand(t) {}
381383

382384
BENCHMARK_ALWAYS_INLINE operator double const&() const { return value; }
383385
BENCHMARK_ALWAYS_INLINE operator double&() { return value; }

src/console_reporter.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void ConsoleReporter::PrintRunData(const Run& result) {
150150
for (auto& c : result.counters) {
151151
const std::size_t cNameLen = std::max(std::string::size_type(10),
152152
c.first.length());
153-
auto const& s = HumanReadableNumber(c.second.value, 1000);
153+
auto const& s = HumanReadableNumber(c.second.value, c.second.thousand);
154154
if (output_options_ & OO_Tabular) {
155155
if (c.second.flags & Counter::kIsRate) {
156156
printer(Out, COLOR_DEFAULT, " %*s/s", cNameLen - 2, s.c_str());

src/statistics.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ std::vector<BenchmarkReporter::Run> ComputeStats(
164164
// user counters
165165
for (auto const& kv : counter_stats) {
166166
const auto uc_stat = Stat.compute_(kv.second.s);
167-
auto c = Counter(uc_stat, counter_stats[kv.first].c.flags);
167+
auto c = Counter(uc_stat, counter_stats[kv.first].c.flags,
168+
counter_stats[kv.first].c.thousand);
168169
data.counters[kv.first] = c;
169170
}
170171

test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ add_test(user_counters_test user_counters_test --benchmark_min_time=0.01)
128128
compile_output_test(user_counters_tabular_test)
129129
add_test(user_counters_tabular_test user_counters_tabular_test --benchmark_counters_tabular=true --benchmark_min_time=0.01)
130130

131+
compile_output_test(user_counters_thousands_test)
132+
add_test(user_counters_thousands_test user_counters_thousands_test --benchmark_min_time=0.01)
133+
131134
check_cxx_compiler_flag(-std=c++03 BENCHMARK_HAS_CXX03_FLAG)
132135
if (BENCHMARK_HAS_CXX03_FLAG)
133136
compile_benchmark_test(cxx03_test)
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
2+
#undef NDEBUG
3+
4+
#include "benchmark/benchmark.h"
5+
#include "output_test.h"
6+
7+
// ========================================================================= //
8+
// ------------------------ Thousands Customisation ------------------------ //
9+
// ========================================================================= //
10+
11+
void BM_Counters_Thousands(benchmark::State& state) {
12+
for (auto _ : state) {
13+
}
14+
namespace bm = benchmark;
15+
state.counters.insert(
16+
{{"t0_1000000DefaultBase",
17+
bm::Counter(1000 * 1000, bm::Counter::kDefaults)},
18+
{"t1_1000000Base1000",
19+
bm::Counter(1000 * 1000, bm::Counter::kDefaults, 1000)},
20+
{"t2_1000000Base1024",
21+
bm::Counter(1000 * 1000, bm::Counter::kDefaults, 1024)},
22+
{"t3_1048576Base1000",
23+
bm::Counter(1024 * 1024, bm::Counter::kDefaults, 1000)},
24+
{"t4_1048576Base1024",
25+
bm::Counter(1024 * 1024, bm::Counter::kDefaults, 1024)}});
26+
}
27+
BENCHMARK(BM_Counters_Thousands);
28+
ADD_CASES(
29+
TC_ConsoleOut,
30+
{{"^BM_Counters_Thousands %console_report t0_1000000DefaultBase=1000k "
31+
"t1_1000000Base1000=1000k t2_1000000Base1024=976.562k "
32+
"t3_1048576Base1000=1048.58k t4_1048576Base1024=1024k$"}});
33+
ADD_CASES(TC_JSONOut,
34+
{{"\"name\": \"BM_Counters_Thousands\",$"},
35+
{"\"iterations\": %int,$", MR_Next},
36+
{"\"real_time\": %float,$", MR_Next},
37+
{"\"cpu_time\": %float,$", MR_Next},
38+
{"\"time_unit\": \"ns\",$", MR_Next},
39+
{"\"t0_1000000DefaultBase\": 1\\.0000000000000000e\\+06,$", MR_Next},
40+
{"\"t1_1000000Base1000\": 1\\.0000000000000000e\\+06,$", MR_Next},
41+
{"\"t2_1000000Base1024\": 1\\.0000000000000000e\\+06,$", MR_Next},
42+
{"\"t3_1048576Base1000\": 1\\.0485760000000000e\\+06,$", MR_Next},
43+
{"\"t4_1048576Base1024\": 1\\.0485760000000000e\\+06$", MR_Next},
44+
{"}", MR_Next}});
45+
ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_Thousands\",%csv_report,1e\\+06,1e\\+06,"
46+
"1e\\+06,1\\.04858e\\+06,1\\.04858e\\+06$"}});
47+
// VS2013 does not allow this function to be passed as a lambda argument
48+
// to CHECK_BENCHMARK_RESULTS()
49+
void CheckThousands(Results const& e) {
50+
// check that the values are within 0.01% of the expected values
51+
CHECK_FLOAT_COUNTER_VALUE(e, "t0_1000000DefaultBase", EQ, 1000 * 1000,
52+
0.0001);
53+
CHECK_FLOAT_COUNTER_VALUE(e, "t1_1000000Base1000", EQ, 1000 * 1000, 0.0001);
54+
CHECK_FLOAT_COUNTER_VALUE(e, "t2_1000000Base1024", EQ, 1000 * 1000, 0.0001);
55+
CHECK_FLOAT_COUNTER_VALUE(e, "t3_1048576Base1000", EQ, 1024 * 1024, 0.0001);
56+
CHECK_FLOAT_COUNTER_VALUE(e, "t4_1048576Base1024", EQ, 1024 * 1024, 0.0001);
57+
}
58+
CHECK_BENCHMARK_RESULTS("BM_Counters_Thousands", &CheckThousands);
59+
60+
// ========================================================================= //
61+
// ----------------------- Aggregates Customisation ------------------------ //
62+
// ========================================================================= //
63+
64+
void BM_Counters_Aggregates(benchmark::State& state) {
65+
BM_Counters_Thousands(state);
66+
}
67+
68+
BENCHMARK(BM_Counters_Aggregates)->Repetitions(3)->ReportAggregatesOnly();
69+
ADD_CASES(TC_ConsoleOut,
70+
{{"^BM_Counters_Aggregates/repeats:3_mean %console_report "
71+
"t0_1000000DefaultBase=1000k t1_1000000Base1000=1000k "
72+
"t2_1000000Base1024=976.562k t3_1048576Base1000=1048.58k "
73+
"t4_1048576Base1024=1024k$"},
74+
{"^BM_Counters_Aggregates/repeats:3_median %console_report "
75+
"t0_1000000DefaultBase=1000k t1_1000000Base1000=1000k "
76+
"t2_1000000Base1024=976.562k t3_1048576Base1000=1048.58k "
77+
"t4_1048576Base1024=1024k$"},
78+
{"^BM_Counters_Aggregates/repeats:3_stddev %console_report "
79+
"t0_1000000DefaultBase=0 t1_1000000Base1000=0 t2_1000000Base1024=0 "
80+
"t3_1048576Base1000=0 t4_1048576Base1024=0$"}});
81+
ADD_CASES(TC_JSONOut,
82+
{{"\"name\": \"BM_Counters_Aggregates/repeats:3_mean\",$"},
83+
{"\"iterations\": %int,$", MR_Next},
84+
{"\"real_time\": %float,$", MR_Next},
85+
{"\"cpu_time\": %float,$", MR_Next},
86+
{"\"time_unit\": \"ns\",$", MR_Next},
87+
{"\"t0_1000000DefaultBase\": 1\\.0000000000000000e\\+06,$", MR_Next},
88+
{"\"t1_1000000Base1000\": 1\\.0000000000000000e\\+06,$", MR_Next},
89+
{"\"t2_1000000Base1024\": 1\\.0000000000000000e\\+06,$", MR_Next},
90+
{"\"t3_1048576Base1000\": 1\\.0485760000000000e\\+06,$", MR_Next},
91+
{"\"t4_1048576Base1024\": 1\\.0485760000000000e\\+06$", MR_Next},
92+
{"}", MR_Next}});
93+
ADD_CASES(TC_JSONOut,
94+
{{"\"name\": \"BM_Counters_Aggregates/repeats:3_median\",$"},
95+
{"\"iterations\": %int,$", MR_Next},
96+
{"\"real_time\": %float,$", MR_Next},
97+
{"\"cpu_time\": %float,$", MR_Next},
98+
{"\"time_unit\": \"ns\",$", MR_Next},
99+
{"\"t0_1000000DefaultBase\": 1\\.0000000000000000e\\+06,$", MR_Next},
100+
{"\"t1_1000000Base1000\": 1\\.0000000000000000e\\+06,$", MR_Next},
101+
{"\"t2_1000000Base1024\": 1\\.0000000000000000e\\+06,$", MR_Next},
102+
{"\"t3_1048576Base1000\": 1\\.0485760000000000e\\+06,$", MR_Next},
103+
{"\"t4_1048576Base1024\": 1\\.0485760000000000e\\+06$", MR_Next},
104+
{"}", MR_Next}});
105+
ADD_CASES(TC_JSONOut,
106+
{{"\"name\": \"BM_Counters_Aggregates/repeats:3_stddev\",$"},
107+
{"\"iterations\": %int,$", MR_Next},
108+
{"\"real_time\": %float,$", MR_Next},
109+
{"\"cpu_time\": %float,$", MR_Next},
110+
{"\"time_unit\": \"ns\",$", MR_Next},
111+
{"\"t0_1000000DefaultBase\": 0\\.0000000000000000e\\+00,$", MR_Next},
112+
{"\"t1_1000000Base1000\": 0\\.0000000000000000e\\+00,$", MR_Next},
113+
{"\"t2_1000000Base1024\": 0\\.0000000000000000e\\+00,$", MR_Next},
114+
{"\"t3_1048576Base1000\": 0\\.0000000000000000e\\+00,$", MR_Next},
115+
{"\"t4_1048576Base1024\": 0\\.0000000000000000e\\+00$", MR_Next},
116+
{"}", MR_Next}});
117+
ADD_CASES(
118+
TC_CSVOut,
119+
{
120+
{"^\"BM_Counters_Aggregates/"
121+
"repeats:3_mean\",%csv_report,1e\\+06,1e\\+06,1e\\+06,1\\.04858e\\+06,"
122+
"1\\.04858e\\+06$"},
123+
{"^\"BM_Counters_Aggregates/"
124+
"repeats:3_median\",%csv_report,1e\\+06,1e\\+06,1e\\+06,1\\.04858e\\+"
125+
"06,1\\.04858e\\+06$"},
126+
{"^\"BM_Counters_Aggregates/repeats:3_stddev\",%csv_report,0,0,0,0,0$"},
127+
});
128+
129+
// ========================================================================= //
130+
// --------------------------- TEST CASES END ------------------------------ //
131+
// ========================================================================= //
132+
133+
int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }

0 commit comments

Comments
 (0)