9 #include <boost/test/data/test_case.hpp>
10 #include <boost/test/unit_test.hpp>
26 BOOST_AUTO_TEST_SUITE(benchmark_tools)
31 BOOST_CHECK_EQUAL(x, 42);
37 BOOST_CHECK_EQUAL(x, 4.2
f);
39 const std::string
y =
"LOL";
41 BOOST_CHECK_EQUAL(y,
"LOL");
47 std::complex
c(1.2, 3.4);
49 BOOST_CHECK_EQUAL(c, std::complex(1.2, 3.4));
56 std::chrono::microseconds(420), std::chrono::microseconds(21),
57 std::chrono::milliseconds(4), std::chrono::microseconds(84),
58 std::chrono::microseconds(294), std::chrono::microseconds(378),
59 std::chrono::microseconds(126), std::chrono::milliseconds(42)};
64 BOOST_CHECK_EQUAL(sorted.size(), res.
run_timings.size());
65 BOOST_CHECK_EQUAL(sorted[0].count(), 21
'000.);
66 BOOST_CHECK_EQUAL(sorted[1].count(), 84'000.);
67 BOOST_CHECK_EQUAL(sorted[2].count(), 126
'000.);
68 BOOST_CHECK_EQUAL(sorted[3].count(), 294'000.);
69 BOOST_CHECK_EQUAL(sorted[4].count(), 378
'000.);
70 BOOST_CHECK_EQUAL(sorted[5].count(), 420'000.);
71 BOOST_CHECK_EQUAL(sorted[6].count(), 4
'000'000.);
72 BOOST_CHECK_EQUAL(sorted[7].count(), 42
'000'000.);
82 (thirdq - firstq).count() / 1.349, 1
e-3);
91 std::ostringstream os;
93 BOOST_CHECK_EQUAL(os.str(),
94 "8 runs of 42 iteration(s), 47.3ms total, "
95 "336.0000+/-1560.4388µs per run, "
96 "8000.000+/-240780.940ns per iteration");
101 microBenchmark([&] { ++counter; }, 15, 7, std::chrono::milliseconds(0));
102 BOOST_CHECK_EQUAL(counter, 15 * 7);
110 17, 11, std::chrono::milliseconds(0));
111 BOOST_CHECK_EQUAL(counter, 17 * 11);
115 std::vector<int> ints{1, 2, 4, 8, 16, 32, 64};
119 BOOST_CHECK_EQUAL(previous, 64);
122 BOOST_CHECK_EQUAL(input, previous * 2);
127 ints, 123, std::chrono::milliseconds(3));
128 BOOST_CHECK_EQUAL(counter, 127);
132 std::vector<char> chars{-1, 3, -9, 27, -81};
136 BOOST_CHECK_EQUAL(previous, -81);
139 BOOST_CHECK_EQUAL(input, -previous * 3);
145 chars, 456, std::chrono::milliseconds(8));
146 BOOST_CHECK_EQUAL(counter, -61);
149 BOOST_AUTO_TEST_SUITE_END()
159 BOOST_AUTO_TEST_SUITE(benchmark_timings, *boost::unit_test::disabled())
164 using namespace std::literals::chrono_literals;
172 std::cout <<
"nop (10x iters): " << nop_x10 << std::endl;
174 std::cout <<
"nop (100x iters): " << nop_x100 << std::endl;
176 const double nop_x100_iter_ns = nop_x100.iterTimeAverage().count();
184 BOOST_CHECK_LT(nop_x100_iter_ns, 1.0);
188 BOOST_CHECK_LT(nop_x100.iterTimeError().count(), 0.1);
195 const double x = 1.2,
y = 3.4,
z = 5.6;
197 [&] {
return std::sqrt(
x *
y) + std::sqrt(y *
z) + std::sqrt(z *
x); },
199 std::cout <<
"sqrt (correct): " << sqrt << std::endl;
200 BOOST_CHECK_GT(sqrt.iterTimeAverage().count(), 10. * nop_x100_iter_ns);
206 return std::sqrt(1.2 * 3.4) + std::sqrt(3.4 * 5.6) +
207 std::sqrt(5.6 * 1.2);
210 std::cout <<
"sqrt (constprop'd): " << sqrt_constprop << std::endl;
211 BOOST_CHECK_LT(sqrt_constprop.iterTimeAverage().count(),
212 sqrt.iterTimeAverage().count() / 5.);
221 [&] { (
void)(std::sqrt(
x *
y) + std::sqrt(y *
z) + std::sqrt(z *
x)); },
223 std::cout <<
"sqrt (deadcode'd): " << sqrt_deadcode << std::endl;
224 BOOST_CHECK_LT(sqrt_deadcode.iterTimeAverage().count(),
225 sqrt.iterTimeAverage().count() / 3.);
240 const double x = 1.2,
y = 3.4,
z = 5.6;
243 return std::make_tuple(
244 std::sqrt(x *
y), std::complex(std::sqrt(y *
z), std::sqrt(z * x)));
247 std::cout <<
"tuple return: " << tuple_return << std::endl;
251 assumeRead(std::complex(std::sqrt(y *
z), std::sqrt(z * x)));
254 std::cout <<
"assumeRead: " << assumeread << std::endl;
255 const double tuple_return_iter_ns = tuple_return.
iterTimeAverage().count();
256 const double assumeRead_iter_ns = assumeread.iterTimeAverage().count();
257 BOOST_CHECK_LT(
std::abs(tuple_return_iter_ns - assumeRead_iter_ns),
258 5. * tuple_return.iterTimeError().count());
270 double x = 1.2, y = 3.4,
z = 5.6;
272 [&] {
return std::sqrt(x * y) + std::sqrt(y *
z) + std::sqrt(
z * x); },
274 std::cout <<
"sqrt sum: " << sqrt_sum << std::endl;
277 double tmp = std::sqrt(x * y) + std::sqrt(y *
z) + std::sqrt(
z * x);
281 return tmp + std::sqrt(x * y) + std::sqrt(y *
z) + std::sqrt(
z * x);
284 std::cout <<
"2x(sqrt sum): " << sqrt_2sums << std::endl;
286 const double sqrt_2sums_iter_ns = sqrt_2sums.iterTimeAverage().count();
287 BOOST_CHECK_LT(
std::abs(2. * sqrt_sum_iter_ns - sqrt_2sums_iter_ns),
288 5. * sqrt_sum.iterTimeError().count());
291 BOOST_AUTO_TEST_SUITE_END()