-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_perf.cpp
More file actions
93 lines (78 loc) · 2.12 KB
/
tests_perf.cpp
File metadata and controls
93 lines (78 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <vector>
#include <fstream>
#include <streambuf>
#include <memory>
#include <variant>
#include <catch2/catch.hpp>
#include "anpa/anpa.h"
#include "time_measure.h"
struct action {
std::string_view name;
std::string_view com;
};
struct info {
std::string_view name;
std::string_view com;
};
struct separator {};
struct space {};
struct syntax_error {
std::string_view description;
};
using entry = std::variant<
action,
info,
separator,
space,
syntax_error
>;
/**
* Performance test for a simple syntax intended for a application launcher/information dashboard:
*
* Each row can be either of the following:
*
* Com:LABEL=COMMAND
* Info:LABEL=COMMAND
* Separator
* Space
* # COMMENT
*
*
* This parser uses an external state and is invoked on each line of input.
*
*/
void test()
{
using namespace anpa;
constexpr auto add_to_state = [](auto& s, auto&& arg) {
s.emplace_back(std::forward<decltype(arg)>(arg));
};
constexpr auto parse_name = until_item('=');
constexpr auto parse_cmd = not_empty(rest());
constexpr auto parse_action = seq("Com:") >> lift_value<action>(parse_name, parse_cmd);
constexpr auto parse_info = seq("Info:") >> lift_value<info>(parse_name, parse_cmd);
constexpr auto parse_separator = seq("Separator") >> mreturn_emplace<separator>();
constexpr auto parse_space = seq("Space") >> mreturn_emplace<space>();
constexpr auto parse_error = lift_value<syntax_error>(rest());
constexpr auto ignore = empty() || (item('#') >> rest());
constexpr auto entry_parser = ignore || lift_or_state(add_to_state, parse_action, parse_info, parse_separator, parse_space, parse_error);
std::ifstream t("hub");
std::vector<entry> state;
state.reserve(1000000);
std::vector<std::string> lines;
lines.reserve(1000000);
std::string line;
while (std::getline(t, line)) {
lines.push_back(line);
}
TICK;
for (auto& l : lines) {
entry_parser.parse_with_state(l, state);
}
std::cout << "Size: " << state.size() << std::endl;
TOCK("hub");
}
TEST_CASE("performance") {
test();
}