9 #include <boost/test/data/test_case.hpp>
10 #include <boost/test/unit_test.hpp>
27 BOOST_AUTO_TEST_SUITE(benchmark_tools)
32 BOOST_CHECK_EQUAL(x, 42);
38 BOOST_CHECK_EQUAL(x, 4.2
f);
42 BOOST_CHECK_EQUAL(y,
"LOL");
48 std::complex
c(1.2, 3.4);
50 BOOST_CHECK_EQUAL(c, std::complex(1.2, 3.4));
57 std::chrono::microseconds(420), std::chrono::microseconds(21),
58 std::chrono::milliseconds(4), std::chrono::microseconds(84),
59 std::chrono::microseconds(294), std::chrono::microseconds(378),
60 std::chrono::microseconds(126), std::chrono::milliseconds(42)};
65 BOOST_CHECK_EQUAL(sorted.size(), res.
run_timings.size());
66 BOOST_CHECK_EQUAL(sorted[0].
count(), 21
'000.);
67 BOOST_CHECK_EQUAL(sorted[1].count(), 84'000.);
68 BOOST_CHECK_EQUAL(sorted[2].
count(), 126
'000.);
69 BOOST_CHECK_EQUAL(sorted[3].count(), 294'000.);
70 BOOST_CHECK_EQUAL(sorted[4].
count(), 378
'000.);
71 BOOST_CHECK_EQUAL(sorted[5].count(), 420'000.);
72 BOOST_CHECK_EQUAL(sorted[6].
count(), 4
'000'000.);
73 BOOST_CHECK_EQUAL(sorted[7].
count(), 42
'000'000.);
89 1.2533 * robustRTStddev.count() / std::sqrt(res.
run_timings.size()),
98 std::ostringstream
os;
100 BOOST_CHECK_EQUAL(os.str(),
101 "8 runs of 42 iteration(s), 47.3ms total, "
102 "336.0000+/-1355.2296µs per run, "
103 "8000.000+/-209116.462ns per iteration");
108 microBenchmark([&] { ++counter; }, 15, 7, std::chrono::milliseconds(0));
109 BOOST_CHECK_EQUAL(counter, 15 * 7);
117 17, 11, std::chrono::milliseconds(0));
118 BOOST_CHECK_EQUAL(counter, 17 * 11);
122 std::vector<int> ints{1, 2, 4, 8, 16, 32, 64};
126 BOOST_CHECK_EQUAL(previous, 64);
129 BOOST_CHECK_EQUAL(input, previous * 2);
134 ints, 123, std::chrono::milliseconds(3));
135 BOOST_CHECK_EQUAL(counter, 127);
139 std::vector<char> chars{-1, 3, -9, 27, -81};
143 BOOST_CHECK_EQUAL(previous, -81);
146 BOOST_CHECK_EQUAL(input, -previous * 3);
152 chars, 456, std::chrono::milliseconds(8));
153 BOOST_CHECK_EQUAL(counter, -61);
156 BOOST_AUTO_TEST_SUITE_END()
166 BOOST_AUTO_TEST_SUITE(benchmark_timings, *boost::unit_test::disabled())
171 using namespace std::literals::chrono_literals;
180 std::cout <<
"nop (10x iters): " << nop_x10 << std::endl;
182 std::cout <<
"nop (100x iters): " << nop_x100 << std::endl;
183 const double nop_x10_iter_ns = nop_x10.iterTimeAverage().count();
184 const double nop_x100_iter_ns = nop_x100.iterTimeAverage().count();
192 BOOST_CHECK_LT(nop_x100_iter_ns, 1.0);
196 BOOST_CHECK_LT(nop_x100.iterTimeError().count(), 0.1);
203 const double x = 1.2,
y = 3.4,
z = 5.6;
205 [&] {
return std::sqrt(
x *
y) + std::sqrt(y *
z) + std::sqrt(z *
x); },
207 std::cout <<
"sqrt (correct): " << sqrt << std::endl;
208 BOOST_CHECK_GT(sqrt.iterTimeAverage().count(), 10. * nop_x100_iter_ns);
214 return std::sqrt(1.2 * 3.4) + std::sqrt(3.4 * 5.6) +
215 std::sqrt(5.6 * 1.2);
218 std::cout <<
"sqrt (constprop'd): " << sqrt_constprop << std::endl;
219 BOOST_CHECK_LT(sqrt_constprop.iterTimeAverage().count(),
220 sqrt.iterTimeAverage().count() / 5.);
229 [&] { (void)(std::sqrt(
x *
y) + std::sqrt(y *
z) + std::sqrt(z *
x)); },
231 std::cout <<
"sqrt (deadcode'd): " << sqrt_deadcode << std::endl;
232 BOOST_CHECK_LT(sqrt_deadcode.iterTimeAverage().count(),
233 sqrt.iterTimeAverage().count() / 3.);
248 const double x = 1.2,
y = 3.4,
z = 5.6;
252 std::sqrt(x *
y), std::complex(std::sqrt(y *
z), std::sqrt(z * x)));
255 std::cout <<
"tuple return: " << tuple_return << std::endl;
259 assumeRead(std::complex(std::sqrt(y *
z), std::sqrt(z * x)));
262 std::cout <<
"assumeRead: " << assumeread << std::endl;
263 const double tuple_return_iter_ns = tuple_return.
iterTimeAverage().count();
264 const double assumeRead_iter_ns = assumeread.iterTimeAverage().count();
277 double x = 1.2, y = 3.4,
z = 5.6;
279 [&] {
return std::sqrt(x * y) + std::sqrt(y *
z) + std::sqrt(
z * x); },
281 std::cout <<
"sqrt sum: " << sqrt_sum << std::endl;
284 double tmp = std::sqrt(x * y) + std::sqrt(y *
z) + std::sqrt(
z * x);
288 return tmp + std::sqrt(x * y) + std::sqrt(y *
z) + std::sqrt(
z * x);
291 std::cout <<
"2x(sqrt sum): " << sqrt_2sums << std::endl;
293 const double sqrt_2sums_iter_ns = sqrt_2sums.iterTimeAverage().count();
297 BOOST_AUTO_TEST_SUITE_END()