mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-04 04:33:23 +00:00
Added Google benchmarks (and gtests) (#6920)
* Added Google benchmarks (and gtests) * Default building benchmarks to OFF as it requires c++11 * Separate benchmark CMakeLists.txt to its own file * Move output directory to target just flatbenchmark
This commit is contained in:
61
benchmarks/cpp/benchmark_main.cpp
Normal file
61
benchmarks/cpp/benchmark_main.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <benchmark/benchmark.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "benchmarks/cpp/bench.h"
|
||||
#include "benchmarks/cpp/flatbuffers/fb_bench.h"
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_Flatbuffers_Encode);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_Flatbuffers_Decode);
|
||||
|
||||
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);
|
||||
}
|
||||
BENCHMARK(BM_Flatbuffers_Use);
|
||||
Reference in New Issue
Block a user