|
| 1 | +// Copyright (c) 2020-now by the Zeek Project. See LICENSE for details. |
| 2 | + |
| 3 | +#include <benchmark/benchmark.h> |
| 4 | + |
| 5 | +#include <hilti/rt/init.h> |
| 6 | +#include <hilti/rt/types/reference.h> |
| 7 | +#include <hilti/rt/types/stream.h> |
| 8 | + |
| 9 | +#include <spicy/rt/init.h> |
| 10 | +#include <spicy/rt/parsed-unit.h> |
| 11 | +#include <spicy/rt/parser.h> |
| 12 | + |
| 13 | +static std::string big_endian(std::uint64_t number) { |
| 14 | + char buffer[8]; |
| 15 | + for ( int i = 0; i < 8; ++i ) { |
| 16 | + buffer[i] = static_cast<char>((number >> (56 - 8 * i)) & 0xFF); |
| 17 | + } |
| 18 | + return std::string(buffer, sizeof(buffer)); |
| 19 | +} |
| 20 | + |
| 21 | +static std::string make_input(std::uint64_t input_size) { |
| 22 | + std::string number = big_endian(input_size); |
| 23 | + std::string repeated(input_size, 'A'); |
| 24 | + return number + repeated + 'B' + 'B'; |
| 25 | +} |
| 26 | + |
| 27 | +template<class... Args> |
| 28 | +static void benchmark_parser(benchmark::State& state, Args&&... args) { |
| 29 | + auto args_tuple = std::make_tuple(std::move(args)...); |
| 30 | + auto parser_name = std::get<0>(args_tuple); |
| 31 | + |
| 32 | + hilti::rt::init(); |
| 33 | + spicy::rt::init(); |
| 34 | + |
| 35 | + static const spicy::rt::Parser* parser = nullptr; |
| 36 | + for ( auto* p : spicy::rt::parsers() ) { |
| 37 | + if ( p->name == parser_name ) { |
| 38 | + parser = p; |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + assert(parser); |
| 44 | + |
| 45 | + for ( auto _ : state ) { |
| 46 | + (void)_; |
| 47 | + state.PauseTiming(); |
| 48 | + auto in = make_input(state.range(0)); |
| 49 | + auto stream = hilti::rt::reference::make_value<hilti::rt::Stream>(in); |
| 50 | + stream->freeze(); |
| 51 | + state.ResumeTiming(); |
| 52 | + parser->parse1(stream, {}, {}); |
| 53 | + } |
| 54 | + |
| 55 | + hilti::rt::done(); |
| 56 | +} |
| 57 | + |
| 58 | +static const int64_t min_input = 100; |
| 59 | +static const int64_t max_input = 100000; |
| 60 | +static const int64_t mult = 10; |
| 61 | + |
| 62 | +BENCHMARK_CAPTURE(benchmark_parser, Benchmark::WithUnit, std::string("Benchmark::WithUnit")) |
| 63 | + ->RangeMultiplier(mult) |
| 64 | + ->Range(min_input, max_input); |
| 65 | + |
| 66 | +BENCHMARK_CAPTURE(benchmark_parser, Benchmark::Regex, std::string("Benchmark::Regex")) |
| 67 | + ->RangeMultiplier(mult) |
| 68 | + ->Range(min_input, max_input); |
| 69 | + |
| 70 | +BENCHMARK_MAIN(); |
0 commit comments