|
| 1 | +// Minimal, dependency-free benchmark for step matching. |
| 2 | +// |
| 3 | +// Measures how long it takes to find a matching step_definition for a |
| 4 | +// batch of feature-step strings against a realistic set of registered |
| 5 | +// steps, using the same step_finder::find() path the interpreter uses |
| 6 | +// at runtime. Run it before/after a change to step_finder/step to see |
| 7 | +// its effect on matching throughput. |
| 8 | +// |
| 9 | +// Usage: ./build/bin/step-matching-benchmark [iterations] |
| 10 | + |
| 11 | +#include <cstdlib> |
| 12 | +#include <iomanip> |
| 13 | +#include <iostream> |
| 14 | +#include <stdexcept> |
| 15 | +#include <string> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include "step.hpp" |
| 19 | +#include "step_finder.hpp" |
| 20 | +#include "util.hpp" |
| 21 | + |
| 22 | +namespace |
| 23 | +{ |
| 24 | + |
| 25 | +void noop_callback(const cuke::value_array&, |
| 26 | + const std::vector<cuke::internal::param_info>&, |
| 27 | + const cuke::internal::doc_string&, const cuke::table&) |
| 28 | +{ |
| 29 | +} |
| 30 | + |
| 31 | +std::vector<cuke::internal::step_definition> make_step_definitions() |
| 32 | +{ |
| 33 | + std::vector<cuke::internal::step_definition> steps; |
| 34 | + steps.reserve(50); |
| 35 | + |
| 36 | + steps.emplace_back(noop_callback, "I have {int} cukes in my belly"); |
| 37 | + steps.emplace_back(noop_callback, "I eat {int} cukes"); |
| 38 | + steps.emplace_back(noop_callback, "I place {int} x {string} in it"); |
| 39 | + steps.emplace_back(noop_callback, "the stock count should be {int}"); |
| 40 | + steps.emplace_back(noop_callback, "I see {} in the raw output"); |
| 41 | + steps.emplace_back(noop_callback, "The value is {word}"); |
| 42 | + steps.emplace_back(noop_callback, "It should equal {string}"); |
| 43 | + steps.emplace_back(noop_callback, "the shipping label should equal {string}"); |
| 44 | + steps.emplace_back(noop_callback, |
| 45 | + "The box gets shipped at {int}-{int}-{int}"); |
| 46 | + steps.emplace_back(noop_callback, "I have a doc string:"); |
| 47 | + |
| 48 | + // Pad with additional, syntactically distinct steps so a realistic |
| 49 | + // step_finder::find() has to walk a non-trivial registry, similar to |
| 50 | + // a medium-sized real project. |
| 51 | + for (int i = 0; i < 40; ++i) |
| 52 | + { |
| 53 | + steps.emplace_back( |
| 54 | + noop_callback, |
| 55 | + std::format( |
| 56 | + "this is generated step number {} with {{int}} and {{string}}", i)); |
| 57 | + } |
| 58 | + |
| 59 | + return steps; |
| 60 | +} |
| 61 | + |
| 62 | +std::vector<std::string> make_feature_steps() |
| 63 | +{ |
| 64 | + return { |
| 65 | + "I have 5 cukes in my belly", |
| 66 | + "I eat 3 cukes", |
| 67 | + "I place 2 x \"widget\" in it", |
| 68 | + "the stock count should be 42", |
| 69 | + "I see {status} in the raw output", |
| 70 | + "The value is foo/bar", |
| 71 | + "It should equal \"foo/bar\"", |
| 72 | + "the shipping label should equal \"Ship to: Tokyo, Japan\"", |
| 73 | + "The box gets shipped at 2026-08-27", |
| 74 | + "I have a doc string:", |
| 75 | + "this step definitely does not match anything", |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace |
| 80 | + |
| 81 | +int main(int argc, char** argv) |
| 82 | +{ |
| 83 | + std::size_t iterations = 100000; |
| 84 | + if (argc > 1) |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + const unsigned long parsed = std::stoul(argv[1]); |
| 89 | + if (parsed == 0) |
| 90 | + { |
| 91 | + throw std::invalid_argument("iterations must be positive"); |
| 92 | + } |
| 93 | + iterations = parsed; |
| 94 | + } |
| 95 | + catch (const std::exception&) |
| 96 | + { |
| 97 | + std::cerr << "usage: " << argv[0] |
| 98 | + << " [iterations] (must be a positive integer)\n"; |
| 99 | + return 1; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + const std::vector<cuke::internal::step_definition> steps = |
| 104 | + make_step_definitions(); |
| 105 | + const std::vector<std::string> feature_steps = make_feature_steps(); |
| 106 | + |
| 107 | + std::size_t matched = 0; |
| 108 | + std::size_t unmatched = 0; |
| 109 | + |
| 110 | + const double seconds = cuke::internal::execute_and_count_time( |
| 111 | + [&]() |
| 112 | + { |
| 113 | + for (std::size_t i = 0; i < iterations; ++i) |
| 114 | + { |
| 115 | + const std::string& feature_step = |
| 116 | + feature_steps[i % feature_steps.size()]; |
| 117 | + cuke::internal::step_finder finder(feature_step); |
| 118 | + auto it = finder.find(steps.begin(), steps.end()); |
| 119 | + if (it != steps.end()) |
| 120 | + { |
| 121 | + ++matched; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + ++unmatched; |
| 126 | + } |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + const double per_match_us = |
| 131 | + (seconds * 1'000'000.0) / static_cast<double>(iterations); |
| 132 | + |
| 133 | + std::cout << std::fixed << std::setprecision(3); |
| 134 | + std::cout << "step definitions registered : " << steps.size() << '\n'; |
| 135 | + std::cout << "feature step lookups : " << iterations << '\n'; |
| 136 | + std::cout << "matched : " << matched << '\n'; |
| 137 | + std::cout << "unmatched : " << unmatched << '\n'; |
| 138 | + std::cout << "total time : " << seconds << " s\n"; |
| 139 | + std::cout << "avg time per lookup : " << per_match_us << " us\n"; |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
0 commit comments