27 const Vector2 poly[] = {{0.4, 0.25}, {0.6, 0.25}, {0.8, 0.75}, {0.2, 0.75}};
31 cov << 0.2, 0.02, 0.15, 0.02;
37 std::uniform_real_distribution<double> axis(0, 1);
38 auto random_point = [&]() ->
Vector2 {
39 return Vector2(axis(rng), axis(rng));
45 const Vector2 edge_inside(0.401, 0.251);
47 const Vector2 edge_outside(0.399, 0.249);
49 const Vector2 far_away(-1000., -1000.);
54 constexpr
int NTESTS = 5
'000;
56 // Some checks are much slower, so we tune down benchmark iterations
57 constexpr int NTESTS_SLOW = NTESTS / 10;
59 // Conversely, no-op tests are so fast that we need to tune up iterations
60 constexpr int NTESTS_NOOP = NTESTS * 10;
62 // We use this to switch between iteration counts
63 enum class Mode { NoCheck, FastOutside, SlowOutside };
65 // Benchmark output display
66 auto print_bench_header = [](const std::string& check_name) {
67 std::cout << check_name << ":" << std::endl;
69 auto print_bench_result = [](const std::string& bench_name,
70 const Acts::Test::MicroBenchmarkResult& res) {
71 std::cout << "- " << bench_name << ": " << res << std::endl;
75 auto run_bench = [&](auto&& iteration, int num_iters,
76 const std::string& bench_name) {
77 auto bench_result = Acts::Test::microBenchmark(iteration, num_iters);
78 print_bench_result(bench_name, bench_result);
80 auto run_bench_with_inputs = [&](auto&& iterationWithArg, auto&& inputs,
81 const std::string& bench_name) {
82 auto bench_result = Acts::Test::microBenchmark(iterationWithArg, inputs);
83 print_bench_result(bench_name, bench_result);
85 auto run_all_benches = [&](const BoundaryCheck& check,
86 const std::string& check_name, const Mode mode) {
87 // Announce a set of benchmarks
88 print_bench_header(check_name);
90 // Pre-determined "interesting" test points
91 int num_inside_points = 0;
92 int num_outside_points = 0;
95 num_inside_points = NTESTS_NOOP;
96 num_outside_points = NTESTS_NOOP;
98 case Mode::FastOutside:
99 num_inside_points = NTESTS;
100 num_outside_points = NTESTS;
102 case Mode::SlowOutside:
103 num_inside_points = NTESTS;
104 num_outside_points = NTESTS_SLOW;
106 run_bench([&] { return check.isInside(center, poly); }, num_inside_points,
108 run_bench([&] { return check.isInside(edge_inside, poly); },
109 num_inside_points, "Inside edge");
110 run_bench([&] { return check.isInside(edge_outside, poly); },
111 num_outside_points, "Outside edge");
112 run_bench([&] { return check.isInside(far_away, poly); },
113 num_outside_points, "Far away");
115 // Pre-rolled random points
116 std::vector<Vector2> points(num_outside_points);
117 std::generate(points.begin(), points.end(), random_point);
118 run_bench_with_inputs(
119 [&](const auto& point) { return check.isInside(point, poly); }, points,
123 // Benchmark scenarios
124 run_all_benches(BoundaryCheck(false), "No check", Mode::NoCheck);
125 run_all_benches(BoundaryCheck(true), "No tolerance", Mode::FastOutside);
126 run_all_benches(BoundaryCheck(true, true, 0.6, 0.45), "Abs. tolerance",
128 run_all_benches(BoundaryCheck(cov, 3.0), "Cov. tolerance", Mode::SlowOutside);