Repository navigation
Expand file tree
/
Copy pathbackend_pb.cpp
More file actions
50 lines (43 loc) · 1.51 KB
/
Copy pathbackend_pb.cpp
File metadata and controls
50 lines (43 loc) · 1.51 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
#include "bench.h"
#include "telemetry.pb.h"
#include <google/protobuf/message.h>
class PBBackend : public Backend {
public:
const char* name() const override { return "protobuf"; }
void reset() override { m_msg.Clear(); }
void populate(size_t params_count, bool with_attachment, size_t attachment_bytes) override {
if (m_pool.size() != params_count)
m_pool = make_kvs(params_count);
auto* params = m_msg.mutable_params();
for (const KV& kv : m_pool) {
bench::TelemetryValue& v = (*params)[kv.key];
switch (kv.type) {
case KV::Type::Double: v.set_d(kv.d); break;
case KV::Type::Int: v.set_i(kv.i); break;
case KV::Type::Bool: v.set_b(kv.b); break;
case KV::Type::Str: v.set_s(kv.s); break;
}
}
if (with_attachment) {
m_blob.assign(attachment_bytes, '\xAB');
m_msg.set_attachment(m_blob.data(), m_blob.size());
}
}
void serialize(std::vector<uint8_t>& out) override {
m_buf.clear();
m_msg.SerializeToString(&m_buf);
out.assign(m_buf.begin(), m_buf.end());
}
void deserialize(const uint8_t* data, size_t size) override {
m_recv.ParseFromArray(data, static_cast<int>(size));
}
private:
bench::TelemetryFrame m_msg;
bench::TelemetryFrame m_recv;
std::string m_buf;
std::string m_blob;
std::vector<KV> m_pool;
};
Backend* make_pb_backend() {
return new PBBackend();
}