Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
75 lines (64 loc) · 2.68 KB
/
Copy pathmain.cpp
File metadata and controls
75 lines (64 loc) · 2.68 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
#include "bench.h"
#include <memory>
#include <vector>
Backend* make_mf_backend(size_t reserve_hint);
Backend* make_pb_backend();
Backend* make_json_backend();
Backend* make_msgpack_backend();
struct ScenarioResult {
std::string backend;
std::string scenario;
PhaseResult phase;
};
int main() {
const Scenario scenarios[] = {
{"4 params", 4, false, 0, 200000},
{"32 params", 32, false, 0, 100000},
{"127 params (vector)",127, false, 0, 50000},
{"150 params (map)", 150, false, 0, 50000},
{"1024 params", 1024, false, 0, 10000},
{"4 params + 1MB blob",4, true, 1024u * 1024u, 2000},
};
struct Runner {
std::string name;
Backend* (*make)();
};
std::vector<std::unique_ptr<Backend>> backends;
std::vector<Runner> runners = {
{"MessageFrame", []() { return make_mf_backend(0); }},
{"MessageFrame+R", []() { return make_mf_backend(1024); }},
{"protobuf", []() { return make_pb_backend(); }},
{"nlohmann/json", []() { return make_json_backend(); }},
{"msgpack-cxx", []() { return make_msgpack_backend(); }},
};
std::printf("%-12s | %-24s | %10s | %10s | %11s | %10s | %12s | %10s | %9s\n",
"backend", "scenario", "add(us)", "serialize", "deserialize",
"total(us)", "msgs/sec", "MB/sec", "bytes");
for (const Scenario& sc : scenarios) {
for (const Runner& r : runners) {
// The sizing-hint variant only makes sense once a message would
// cross the vector->map threshold anyway (>= 150 params here).
if (sc.params_count < 150 &&
(std::string(r.name) == "MessageFrame+R"))
continue;
std::unique_ptr<Backend> b(r.make());
// min-of-3 passes to reduce timer noise
PhaseResult best;
bool first = true;
for (int pass = 0; pass < 3; ++pass) {
PhaseResult pr = run_backend(*b, sc);
if (first || pr.total_us < best.total_us) {
best = pr;
first = false;
}
}
std::printf("%-12s | %-24s | %10.2f | %10.2f | %11.2f | %10.2f | %12.0f | %10.2f | %9zu\n",
b->name(), sc.name,
best.add_us, best.serialize_us, best.deserialize_us, best.total_us,
1e6 / best.total_us,
(double(best.wire_bytes) / (1024.0 * 1024.0)) / (best.total_us / 1e6),
best.wire_bytes);
}
}
return 0;
}