Decouple parser from CommitRequest
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "commit_request.hpp"
|
||||
#include "json_commit_request_parser.hpp"
|
||||
#include "test_data.hpp"
|
||||
|
||||
#include <nanobench.h>
|
||||
@@ -18,28 +19,34 @@ int main() {
|
||||
// Simple JSON parsing
|
||||
bench.run("Simple JSON (3 fields)", [&] {
|
||||
CommitRequest request;
|
||||
JsonCommitRequestParser parser;
|
||||
std::string mutable_json = SIMPLE_JSON;
|
||||
bool result = request.parse_json(mutable_json.data(), mutable_json.size());
|
||||
bool result =
|
||||
parser.parse(request, mutable_json.data(), mutable_json.size());
|
||||
ankerl::nanobench::doNotOptimizeAway(result);
|
||||
ankerl::nanobench::doNotOptimizeAway(request.is_parse_complete());
|
||||
ankerl::nanobench::doNotOptimizeAway(request.leader_id());
|
||||
});
|
||||
|
||||
// Medium complexity JSON parsing
|
||||
bench.run("Medium JSON (2 preconditions, 2 operations)", [&] {
|
||||
CommitRequest request;
|
||||
JsonCommitRequestParser parser;
|
||||
std::string mutable_json = MEDIUM_JSON;
|
||||
bool result = request.parse_json(mutable_json.data(), mutable_json.size());
|
||||
bool result =
|
||||
parser.parse(request, mutable_json.data(), mutable_json.size());
|
||||
ankerl::nanobench::doNotOptimizeAway(result);
|
||||
ankerl::nanobench::doNotOptimizeAway(request.is_parse_complete());
|
||||
ankerl::nanobench::doNotOptimizeAway(request.leader_id());
|
||||
});
|
||||
|
||||
// Complex JSON parsing
|
||||
bench.run("Complex JSON (3 preconditions, 5 operations)", [&] {
|
||||
CommitRequest request;
|
||||
JsonCommitRequestParser parser;
|
||||
std::string mutable_json = COMPLEX_JSON;
|
||||
bool result = request.parse_json(mutable_json.data(), mutable_json.size());
|
||||
bool result =
|
||||
parser.parse(request, mutable_json.data(), mutable_json.size());
|
||||
ankerl::nanobench::doNotOptimizeAway(result);
|
||||
ankerl::nanobench::doNotOptimizeAway(request.is_parse_complete());
|
||||
ankerl::nanobench::doNotOptimizeAway(request.leader_id());
|
||||
});
|
||||
|
||||
// Large batch operations
|
||||
@@ -47,11 +54,12 @@ int main() {
|
||||
std::string large_json = generate_large_json(num_ops);
|
||||
bench.run("Large JSON (" + std::to_string(num_ops) + " operations)", [&] {
|
||||
CommitRequest request;
|
||||
JsonCommitRequestParser parser;
|
||||
std::string mutable_json = large_json;
|
||||
bool result =
|
||||
request.parse_json(mutable_json.data(), mutable_json.size());
|
||||
parser.parse(request, mutable_json.data(), mutable_json.size());
|
||||
ankerl::nanobench::doNotOptimizeAway(result);
|
||||
ankerl::nanobench::doNotOptimizeAway(request.is_parse_complete());
|
||||
ankerl::nanobench::doNotOptimizeAway(request.leader_id());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -67,28 +75,30 @@ int main() {
|
||||
"Streaming Medium JSON (chunk size " + std::to_string(chunk_size) + ")",
|
||||
[&] {
|
||||
CommitRequest request;
|
||||
JsonCommitRequestParser parser;
|
||||
std::string mutable_json = MEDIUM_JSON;
|
||||
|
||||
request.begin_streaming_parse();
|
||||
parser.begin_streaming_parse(request);
|
||||
|
||||
size_t offset = 0;
|
||||
CommitRequest::ParseStatus status =
|
||||
CommitRequest::ParseStatus::Incomplete;
|
||||
CommitRequestParser::ParseStatus status =
|
||||
CommitRequestParser::ParseStatus::Incomplete;
|
||||
|
||||
while (offset < mutable_json.size() &&
|
||||
status == CommitRequest::ParseStatus::Incomplete) {
|
||||
status == CommitRequestParser::ParseStatus::Incomplete) {
|
||||
size_t len = std::min(static_cast<size_t>(chunk_size),
|
||||
mutable_json.size() - offset);
|
||||
status = request.parse_chunk(mutable_json.data() + offset, len);
|
||||
status =
|
||||
parser.parse_chunk(request, mutable_json.data() + offset, len);
|
||||
offset += len;
|
||||
}
|
||||
|
||||
if (status == CommitRequest::ParseStatus::Incomplete) {
|
||||
status = request.finish_streaming_parse();
|
||||
if (status == CommitRequestParser::ParseStatus::Incomplete) {
|
||||
status = parser.finish_streaming_parse(request);
|
||||
}
|
||||
|
||||
ankerl::nanobench::doNotOptimizeAway(status);
|
||||
ankerl::nanobench::doNotOptimizeAway(request.is_parse_complete());
|
||||
ankerl::nanobench::doNotOptimizeAway(request.leader_id());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -100,20 +110,21 @@ int main() {
|
||||
|
||||
reuse_bench.run("Parse -> Reset -> Parse cycle", [&] {
|
||||
static CommitRequest request; // Static to persist across invocations
|
||||
static JsonCommitRequestParser parser;
|
||||
|
||||
std::string mutable_json1 = SIMPLE_JSON;
|
||||
bool result1 =
|
||||
request.parse_json(mutable_json1.data(), mutable_json1.size());
|
||||
parser.parse(request, mutable_json1.data(), mutable_json1.size());
|
||||
|
||||
request.reset();
|
||||
|
||||
std::string mutable_json2 = MEDIUM_JSON;
|
||||
bool result2 =
|
||||
request.parse_json(mutable_json2.data(), mutable_json2.size());
|
||||
parser.parse(request, mutable_json2.data(), mutable_json2.size());
|
||||
|
||||
ankerl::nanobench::doNotOptimizeAway(result1);
|
||||
ankerl::nanobench::doNotOptimizeAway(result2);
|
||||
ankerl::nanobench::doNotOptimizeAway(request.is_parse_complete());
|
||||
ankerl::nanobench::doNotOptimizeAway(request.leader_id());
|
||||
});
|
||||
|
||||
// Base64 decoding performance
|
||||
@@ -145,11 +156,12 @@ int main() {
|
||||
base64_bench.run(
|
||||
"Heavy Base64 JSON (20 operations with long encoded data)", [&] {
|
||||
CommitRequest request;
|
||||
JsonCommitRequestParser parser;
|
||||
std::string mutable_json = base64_heavy_json;
|
||||
bool result =
|
||||
request.parse_json(mutable_json.data(), mutable_json.size());
|
||||
parser.parse(request, mutable_json.data(), mutable_json.size());
|
||||
ankerl::nanobench::doNotOptimizeAway(result);
|
||||
ankerl::nanobench::doNotOptimizeAway(request.is_parse_complete());
|
||||
ankerl::nanobench::doNotOptimizeAway(request.leader_id());
|
||||
});
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user