Repository navigation
Expand file tree
/
Copy pathbench.h
More file actions
114 lines (98 loc) · 3.91 KB
/
Copy pathbench.h
File metadata and controls
114 lines (98 loc) · 3.91 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
// ---------------------------------------------------------------------------
// Shared benchmark core. One logical "scenario" is fed to every backend in
// exactly the same way: the same N key/value pairs (identical key names and
// identical values), optionally one binary attachment of a given size.
// ---------------------------------------------------------------------------
using Clock = std::chrono::high_resolution_clock;
struct Scenario {
const char* name;
size_t params_count;
bool with_attachment;
size_t attachment_bytes;
size_t iterations;
};
struct KV {
enum class Type { Double, Int, Bool, Str } type;
std::string device; // used by MessageFrame two-part addressing
std::string param;
std::string key; // flat key used by protobuf/json/msgpack ("device.param")
double d = 0.0;
int64_t i = 0;
bool b = false;
std::string s;
};
// Deterministic key/value pool, built once before timing.
// Values depend only on the pool index, never on the iteration counter,
// so wire sizes stay constant across runs (no msgpack varint drift).
inline std::vector<KV> make_kvs(size_t n) {
std::vector<KV> out;
out.reserve(n);
for (size_t k = 0; k < n; ++k) {
KV kv;
kv.device = "d" + std::to_string(k % 8);
kv.param = "p" + std::to_string(k);
kv.key = kv.device + "." + kv.param;
switch (k % 4) {
case 0: kv.type = KV::Type::Double; kv.d = 12.5 + double(k) * 0.001; break;
case 1: kv.type = KV::Type::Int; kv.i = 1000 + int64_t(k); break;
case 2: kv.type = KV::Type::Bool; kv.b = (k % 2) == 0; break;
case 3: kv.type = KV::Type::Str; kv.s = "v1.2.3"; break;
}
out.push_back(std::move(kv));
}
return out;
}
class Backend {
public:
virtual ~Backend() = default;
virtual const char* name() const = 0;
virtual void reset() = 0;
virtual void populate(size_t params_count, bool with_attachment, size_t attachment_bytes) = 0;
virtual void serialize(std::vector<uint8_t>& out) = 0;
virtual void deserialize(const uint8_t* data, size_t size) = 0;
};
struct PhaseResult {
double add_us = 0.0;
double serialize_us = 0.0;
double deserialize_us = 0.0;
double total_us = 0.0;
size_t wire_bytes = 0;
};
inline PhaseResult run_backend(Backend& b, const Scenario& sc) {
PhaseResult r;
std::vector<uint8_t> buf;
buf.reserve(sc.attachment_bytes + 65536);
for (size_t it = 0; it < sc.iterations; ++it) {
b.reset();
auto t0 = Clock::now();
b.populate(sc.params_count, sc.with_attachment, sc.attachment_bytes);
auto t1 = Clock::now();
buf.clear();
b.serialize(buf);
auto t2 = Clock::now();
r.wire_bytes = buf.size();
b.deserialize(buf.data(), buf.size());
auto t3 = Clock::now();
r.add_us += std::chrono::duration<double, std::micro>(t1 - t0).count();
r.serialize_us += std::chrono::duration<double, std::micro>(t2 - t1).count();
r.deserialize_us += std::chrono::duration<double, std::micro>(t3 - t2).count();
}
r.add_us /= double(sc.iterations);
r.serialize_us /= double(sc.iterations);
r.deserialize_us /= double(sc.iterations);
r.total_us = r.add_us + r.serialize_us + r.deserialize_us;
return r;
}
inline void print_result(const char* backend, const char* scenario, const PhaseResult& r, size_t iterations) {
double msgs_per_sec = 1e6 / r.total_us;
double mb_per_sec = (double(r.wire_bytes) / (1024.0 * 1024.0)) / (r.total_us / 1e6);
std::printf("%-12s | %-24s | %9.2f | %8.2f | %9.2f | %9.2f | %10.0f | %8.2f | %8zu\n",
backend, scenario, r.add_us, r.serialize_us, r.deserialize_us, r.total_us,
msgs_per_sec, mb_per_sec, r.wire_bytes);
}