2018-05-02 15:54:28 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <benchmark/benchmark.h>
|
|
|
|
|
2019-03-15 22:58:16 +08:00
|
|
|
#include "query/distributed/frontend/semantic/symbol_serialization.hpp"
|
2019-05-02 20:24:11 +08:00
|
|
|
#include "query/frontend/semantic/symbol.hpp"
|
|
|
|
#include "slk/serialization.hpp"
|
2018-10-31 17:22:51 +08:00
|
|
|
|
2018-05-02 15:54:28 +08:00
|
|
|
class SymbolVectorFixture : public benchmark::Fixture {
|
|
|
|
protected:
|
|
|
|
std::vector<query::Symbol> symbols_;
|
|
|
|
|
|
|
|
void SetUp(const benchmark::State &state) override {
|
|
|
|
using Type = ::query::Symbol::Type;
|
2018-11-22 16:51:58 +08:00
|
|
|
std::vector<Type> types{Type::ANY, Type::VERTEX, Type::EDGE,
|
|
|
|
Type::PATH, Type::NUMBER, Type::EDGE_LIST};
|
2018-05-02 15:54:28 +08:00
|
|
|
symbols_.reserve(state.range(0));
|
|
|
|
for (int i = 0; i < state.range(0); ++i) {
|
|
|
|
std::string name = "Symbol " + std::to_string(i);
|
|
|
|
bool user_declared = i % 2;
|
|
|
|
auto type = types[i % types.size()];
|
|
|
|
symbols_.emplace_back(name, i, user_declared, type, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown(const benchmark::State &) override { symbols_.clear(); }
|
|
|
|
};
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
void SymbolVectorToSlk(const std::vector<query::Symbol> &symbols,
|
|
|
|
slk::Builder *builder) {
|
2018-10-31 17:22:51 +08:00
|
|
|
slk::Save(symbols.size(), builder);
|
|
|
|
for (int i = 0; i < symbols.size(); ++i) {
|
|
|
|
slk::Save(symbols[i], builder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
void SlkToSymbolVector(std::vector<query::Symbol> *symbols,
|
|
|
|
slk::Reader *reader) {
|
2018-10-31 17:22:51 +08:00
|
|
|
uint64_t size = 0;
|
|
|
|
slk::Load(&size, reader);
|
|
|
|
symbols->resize(size);
|
|
|
|
for (uint64_t i = 0; i < size; ++i) {
|
|
|
|
slk::Load(&(*symbols)[i], reader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
BENCHMARK_DEFINE_F(SymbolVectorFixture, SlkSerial)(benchmark::State &state) {
|
2018-10-31 17:22:51 +08:00
|
|
|
while (state.KeepRunning()) {
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Builder builder([](const uint8_t *, size_t, bool) {});
|
|
|
|
SymbolVectorToSlk(symbols_, &builder);
|
|
|
|
builder.Finalize();
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
2018-11-12 17:47:46 +08:00
|
|
|
state.SetItemsProcessed(state.iterations());
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
BENCHMARK_DEFINE_F(SymbolVectorFixture, SlkDeserial)
|
2018-10-31 17:22:51 +08:00
|
|
|
(benchmark::State &state) {
|
2019-05-02 20:24:11 +08:00
|
|
|
std::vector<uint8_t> encoded;
|
|
|
|
slk::Builder builder(
|
|
|
|
[&encoded](const uint8_t *data, size_t size, bool have_more) {
|
|
|
|
for (size_t i = 0; i < size; ++i) encoded.push_back(data[i]);
|
|
|
|
});
|
|
|
|
SymbolVectorToSlk(symbols_, &builder);
|
|
|
|
builder.Finalize();
|
|
|
|
|
2018-10-31 17:22:51 +08:00
|
|
|
while (state.KeepRunning()) {
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Reader reader(encoded.data(), encoded.size());
|
2018-10-31 17:22:51 +08:00
|
|
|
std::vector<query::Symbol> symbols;
|
2019-05-02 20:24:11 +08:00
|
|
|
SlkToSymbolVector(&symbols, &reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
2018-11-12 17:47:46 +08:00
|
|
|
state.SetItemsProcessed(state.iterations());
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
BENCHMARK_REGISTER_F(SymbolVectorFixture, SlkSerial)
|
2018-10-31 17:22:51 +08:00
|
|
|
->RangeMultiplier(4)
|
|
|
|
->Range(4, 1 << 12)
|
|
|
|
->Unit(benchmark::kNanosecond);
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
BENCHMARK_REGISTER_F(SymbolVectorFixture, SlkDeserial)
|
2018-10-31 17:22:51 +08:00
|
|
|
->RangeMultiplier(4)
|
|
|
|
->Range(4, 1 << 12)
|
|
|
|
->Unit(benchmark::kNanosecond);
|
|
|
|
|
2018-05-02 15:54:28 +08:00
|
|
|
BENCHMARK_MAIN();
|