mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
Added Raw C++ benchmarks (#6924)
This commit is contained in:
@@ -3,20 +3,48 @@
|
||||
|
||||
#include "benchmarks/cpp/bench.h"
|
||||
#include "benchmarks/cpp/flatbuffers/fb_bench.h"
|
||||
#include "benchmarks/cpp/raw/raw_bench.h"
|
||||
|
||||
static inline void Encode(benchmark::State &state,
|
||||
std::unique_ptr<Bench> &bench, uint8_t *buffer) {
|
||||
int64_t length;
|
||||
for (auto _ : state) {
|
||||
bench->Encode(buffer, length);
|
||||
benchmark::DoNotOptimize(length);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void Decode(benchmark::State &state,
|
||||
std::unique_ptr<Bench> &bench, uint8_t *buffer) {
|
||||
int64_t length;
|
||||
uint8_t *encoded = bench->Encode(buffer, length);
|
||||
|
||||
for (auto _ : state) {
|
||||
void *decoded = bench->Decode(encoded, length);
|
||||
benchmark::DoNotOptimize(decoded);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void Use(benchmark::State &state, std::unique_ptr<Bench> &bench,
|
||||
uint8_t *buffer, int64_t check_sum) {
|
||||
int64_t length;
|
||||
uint8_t *encoded = bench->Encode(buffer, length);
|
||||
void *decoded = bench->Decode(encoded, length);
|
||||
|
||||
int64_t sum = 0;
|
||||
|
||||
for (auto _ : state) { sum = bench->Use(decoded); }
|
||||
|
||||
EXPECT_EQ(sum, check_sum);
|
||||
}
|
||||
|
||||
static void BM_Flatbuffers_Encode(benchmark::State &state) {
|
||||
const int64_t kBufferLength = 1024;
|
||||
uint8_t buffer[kBufferLength];
|
||||
|
||||
int64_t length;
|
||||
|
||||
StaticAllocator allocator(&buffer[0]);
|
||||
std::unique_ptr<Bench> bench = NewFlatBuffersBench(kBufferLength, &allocator);
|
||||
|
||||
for (auto _ : state) {
|
||||
bench->Encode(buffer, length);
|
||||
benchmark::DoNotOptimize(length);
|
||||
}
|
||||
Encode(state, bench, buffer);
|
||||
}
|
||||
BENCHMARK(BM_Flatbuffers_Encode);
|
||||
|
||||
@@ -24,17 +52,9 @@ static void BM_Flatbuffers_Decode(benchmark::State &state) {
|
||||
const int64_t kBufferLength = 1024;
|
||||
uint8_t buffer[kBufferLength];
|
||||
|
||||
int64_t length;
|
||||
|
||||
StaticAllocator allocator(&buffer[0]);
|
||||
std::unique_ptr<Bench> bench = NewFlatBuffersBench(kBufferLength, &allocator);
|
||||
|
||||
uint8_t* encoded = bench->Encode(buffer, length);
|
||||
|
||||
for (auto _ : state) {
|
||||
void* decoded = bench->Decode(encoded, length);
|
||||
benchmark::DoNotOptimize(decoded);
|
||||
}
|
||||
Decode(state, bench, buffer);
|
||||
}
|
||||
BENCHMARK(BM_Flatbuffers_Decode);
|
||||
|
||||
@@ -42,20 +62,35 @@ static void BM_Flatbuffers_Use(benchmark::State &state) {
|
||||
const int64_t kBufferLength = 1024;
|
||||
uint8_t buffer[kBufferLength];
|
||||
|
||||
int64_t length;
|
||||
|
||||
StaticAllocator allocator(&buffer[0]);
|
||||
std::unique_ptr<Bench> bench = NewFlatBuffersBench(kBufferLength, &allocator);
|
||||
|
||||
uint8_t* encoded = bench->Encode(buffer, length);
|
||||
void* decoded = bench->Decode(encoded, length);
|
||||
|
||||
int64_t sum = 0;
|
||||
|
||||
for (auto _ : state) {
|
||||
sum = bench->Use(decoded);
|
||||
}
|
||||
|
||||
EXPECT_EQ(sum , 218812692406581874);
|
||||
Use(state, bench, buffer, 218812692406581874);
|
||||
}
|
||||
BENCHMARK(BM_Flatbuffers_Use);
|
||||
BENCHMARK(BM_Flatbuffers_Use);
|
||||
|
||||
static void BM_Raw_Encode(benchmark::State &state) {
|
||||
const int64_t kBufferLength = 1024;
|
||||
uint8_t buffer[kBufferLength];
|
||||
|
||||
std::unique_ptr<Bench> bench = NewRawBench();
|
||||
Encode(state, bench, buffer);
|
||||
}
|
||||
BENCHMARK(BM_Raw_Encode);
|
||||
|
||||
static void BM_Raw_Decode(benchmark::State &state) {
|
||||
const int64_t kBufferLength = 1024;
|
||||
uint8_t buffer[kBufferLength];
|
||||
|
||||
std::unique_ptr<Bench> bench = NewRawBench();
|
||||
Decode(state, bench, buffer);
|
||||
}
|
||||
BENCHMARK(BM_Raw_Decode);
|
||||
|
||||
static void BM_Raw_Use(benchmark::State &state) {
|
||||
const int64_t kBufferLength = 1024;
|
||||
uint8_t buffer[kBufferLength];
|
||||
|
||||
std::unique_ptr<Bench> bench = NewRawBench();
|
||||
Use(state, bench, buffer, 218812692406581874);
|
||||
}
|
||||
BENCHMARK(BM_Raw_Use);
|
||||
Reference in New Issue
Block a user