Skip to content

Commit a40d39a

Browse files
authored
Merge d4a5af5 into af441fc
2 parents af441fc + d4a5af5 commit a40d39a

File tree

6 files changed

+163
-6
lines changed

6 files changed

+163
-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: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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)->Repetitions(2);
28+
ADD_CASES(
29+
TC_ConsoleOut,
30+
{
31+
{"^BM_Counters_Thousands/repeats:2 %console_report "
32+
"t0_1000000DefaultBase=1000k "
33+
"t1_1000000Base1000=1000k t2_1000000Base1024=976.56[23]k "
34+
"t3_1048576Base1000=1048.58k t4_1048576Base1024=1024k$"},
35+
{"^BM_Counters_Thousands/repeats:2 %console_report "
36+
"t0_1000000DefaultBase=1000k "
37+
"t1_1000000Base1000=1000k t2_1000000Base1024=976.56[23]k "
38+
"t3_1048576Base1000=1048.58k t4_1048576Base1024=1024k$"},
39+
{"^BM_Counters_Thousands/repeats:2_mean %console_report "
40+
"t0_1000000DefaultBase=1000k t1_1000000Base1000=1000k "
41+
"t2_1000000Base1024=976.56[23]k t3_1048576Base1000=1048.58k "
42+
"t4_1048576Base1024=1024k$"},
43+
{"^BM_Counters_Thousands/repeats:2_median %console_report "
44+
"t0_1000000DefaultBase=1000k t1_1000000Base1000=1000k "
45+
"t2_1000000Base1024=976.56[23]k t3_1048576Base1000=1048.58k "
46+
"t4_1048576Base1024=1024k$"},
47+
{"^BM_Counters_Thousands/repeats:2_stddev %console_report "
48+
"t0_1000000DefaultBase=0 t1_1000000Base1000=0 t2_1000000Base1024=0 "
49+
"t3_1048576Base1000=0 t4_1048576Base1024=0$"},
50+
});
51+
ADD_CASES(TC_JSONOut,
52+
{{"\"name\": \"BM_Counters_Thousands/repeats:2\",$"},
53+
{"\"iterations\": %int,$", MR_Next},
54+
{"\"real_time\": %float,$", MR_Next},
55+
{"\"cpu_time\": %float,$", MR_Next},
56+
{"\"time_unit\": \"ns\",$", MR_Next},
57+
{"\"t0_1000000DefaultBase\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
58+
{"\"t1_1000000Base1000\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
59+
{"\"t2_1000000Base1024\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
60+
{"\"t3_1048576Base1000\": 1\\.048576(0)*e\\+(0)*6,$", MR_Next},
61+
{"\"t4_1048576Base1024\": 1\\.048576(0)*e\\+(0)*6$", MR_Next},
62+
{"}", MR_Next}});
63+
ADD_CASES(TC_JSONOut,
64+
{{"\"name\": \"BM_Counters_Thousands/repeats:2\",$"},
65+
{"\"iterations\": %int,$", MR_Next},
66+
{"\"real_time\": %float,$", MR_Next},
67+
{"\"cpu_time\": %float,$", MR_Next},
68+
{"\"time_unit\": \"ns\",$", MR_Next},
69+
{"\"t0_1000000DefaultBase\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
70+
{"\"t1_1000000Base1000\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
71+
{"\"t2_1000000Base1024\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
72+
{"\"t3_1048576Base1000\": 1\\.048576(0)*e\\+(0)*6,$", MR_Next},
73+
{"\"t4_1048576Base1024\": 1\\.048576(0)*e\\+(0)*6$", MR_Next},
74+
{"}", MR_Next}});
75+
ADD_CASES(TC_JSONOut,
76+
{{"\"name\": \"BM_Counters_Thousands/repeats:2_mean\",$"},
77+
{"\"iterations\": %int,$", MR_Next},
78+
{"\"real_time\": %float,$", MR_Next},
79+
{"\"cpu_time\": %float,$", MR_Next},
80+
{"\"time_unit\": \"ns\",$", MR_Next},
81+
{"\"t0_1000000DefaultBase\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
82+
{"\"t1_1000000Base1000\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
83+
{"\"t2_1000000Base1024\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
84+
{"\"t3_1048576Base1000\": 1\\.048576(0)*e\\+(0)*6,$", MR_Next},
85+
{"\"t4_1048576Base1024\": 1\\.048576(0)*e\\+(0)*6$", MR_Next},
86+
{"}", MR_Next}});
87+
ADD_CASES(TC_JSONOut,
88+
{{"\"name\": \"BM_Counters_Thousands/repeats:2_median\",$"},
89+
{"\"iterations\": %int,$", MR_Next},
90+
{"\"real_time\": %float,$", MR_Next},
91+
{"\"cpu_time\": %float,$", MR_Next},
92+
{"\"time_unit\": \"ns\",$", MR_Next},
93+
{"\"t0_1000000DefaultBase\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
94+
{"\"t1_1000000Base1000\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
95+
{"\"t2_1000000Base1024\": 1\\.(0)*e\\+(0)*6,$", MR_Next},
96+
{"\"t3_1048576Base1000\": 1\\.048576(0)*e\\+(0)*6,$", MR_Next},
97+
{"\"t4_1048576Base1024\": 1\\.048576(0)*e\\+(0)*6$", MR_Next},
98+
{"}", MR_Next}});
99+
ADD_CASES(TC_JSONOut,
100+
{{"\"name\": \"BM_Counters_Thousands/repeats:2_stddev\",$"},
101+
{"\"iterations\": %int,$", MR_Next},
102+
{"\"real_time\": %float,$", MR_Next},
103+
{"\"cpu_time\": %float,$", MR_Next},
104+
{"\"time_unit\": \"ns\",$", MR_Next},
105+
{"\"t0_1000000DefaultBase\": 0\\.(0)*e\\+(0)*,$", MR_Next},
106+
{"\"t1_1000000Base1000\": 0\\.(0)*e\\+(0)*,$", MR_Next},
107+
{"\"t2_1000000Base1024\": 0\\.(0)*e\\+(0)*,$", MR_Next},
108+
{"\"t3_1048576Base1000\": 0\\.(0)*e\\+(0)*,$", MR_Next},
109+
{"\"t4_1048576Base1024\": 0\\.(0)*e\\+(0)*$", MR_Next},
110+
{"}", MR_Next}});
111+
112+
ADD_CASES(
113+
TC_CSVOut,
114+
{{"^\"BM_Counters_Thousands/"
115+
"repeats:2\",%csv_report,1e\\+(0)*6,1e\\+(0)*6,1e\\+(0)*6,1\\.04858e\\+("
116+
"0)*6,1\\.04858e\\+(0)*6$"},
117+
{"^\"BM_Counters_Thousands/"
118+
"repeats:2\",%csv_report,1e\\+(0)*6,1e\\+(0)*6,1e\\+(0)*6,1\\.04858e\\+("
119+
"0)*6,1\\.04858e\\+(0)*6$"},
120+
{"^\"BM_Counters_Thousands/"
121+
"repeats:2_mean\",%csv_report,1e\\+(0)*6,1e\\+(0)*6,1e\\+(0)*6,1\\."
122+
"04858e\\+(0)*6,1\\.04858e\\+(0)*6$"},
123+
{"^\"BM_Counters_Thousands/"
124+
"repeats:2_median\",%csv_report,1e\\+(0)*6,1e\\+(0)*6,1e\\+(0)*6,1\\."
125+
"04858e\\+(0)*6,1\\.04858e\\+(0)*6$"},
126+
{"^\"BM_Counters_Thousands/repeats:2_stddev\",%csv_report,0,0,0,0,0$"}});
127+
// VS2013 does not allow this function to be passed as a lambda argument
128+
// to CHECK_BENCHMARK_RESULTS()
129+
void CheckThousands(Results const& e) {
130+
if (e.name != "BM_Counters_Thousands/repeats:2")
131+
return; // Do not check the aggregates!
132+
133+
// check that the values are within 0.01% of the expected values
134+
CHECK_FLOAT_COUNTER_VALUE(e, "t0_1000000DefaultBase", EQ, 1000 * 1000,
135+
0.0001);
136+
CHECK_FLOAT_COUNTER_VALUE(e, "t1_1000000Base1000", EQ, 1000 * 1000, 0.0001);
137+
CHECK_FLOAT_COUNTER_VALUE(e, "t2_1000000Base1024", EQ, 1000 * 1000, 0.0001);
138+
CHECK_FLOAT_COUNTER_VALUE(e, "t3_1048576Base1000", EQ, 1024 * 1024, 0.0001);
139+
CHECK_FLOAT_COUNTER_VALUE(e, "t4_1048576Base1024", EQ, 1024 * 1024, 0.0001);
140+
}
141+
CHECK_BENCHMARK_RESULTS("BM_Counters_Thousands", &CheckThousands);
142+
143+
// ========================================================================= //
144+
// --------------------------- TEST CASES END ------------------------------ //
145+
// ========================================================================= //
146+
147+
int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }

0 commit comments

Comments
 (0)