#include "tile/base/buffer.h"
#include "tile/testing/internal/random_string.h"

#include "benchmark/benchmark.h"

namespace tile {
namespace {
PolymorphicBuffer
MakeNativeBuffer(Slice s)
{
    auto buffer = MakeNativeBufferBlock();
    memcpy(buffer->mutable_data(), s.data(), s.size());
    return PolymorphicBuffer(buffer, 0, s.size());
}

void
Benchmark_CreateBufferSlow(benchmark::State &state)
{
    std::string data;
    while (state.KeepRunning()) {
        tile::testing::RandomString(&data, 4080, 4080);
        auto nb = CreateBufferSlow(data);
        for (int i = 0; i < 100; i++) {
            tile::testing::RandomString(&data, 4080, 4080);
            nb.Append(MakeNativeBuffer(data));
        }
        benchmark::DoNotOptimize(nb);
    }
}

void
Benchmark_NoncontinueBufferBuilder(benchmark::State &state)
{
    std::string data;
    while (state.KeepRunning()) {
        tile::NoncontiguousBufferBuilder nbb;
        tile::testing::RandomString(&data);
        for (int i = 0; i < 100; i++) { nbb.Append(data); }
        benchmark::DoNotOptimize(nbb.DestructiveGet());
    }
}

BENCHMARK(Benchmark_CreateBufferSlow);
BENCHMARK(Benchmark_NoncontinueBufferBuilder);

}// namespace
}// namespace tile
