|
| 1 | +#include <benchmark/benchmark.h> |
| 2 | +#include <csv/csv.h> |
| 3 | +#include <core/schema.h> |
| 4 | + |
| 5 | +#include <sstream> |
| 6 | + |
| 7 | +using namespace columnar; // NOLINT |
| 8 | + |
| 9 | +std::string GenerateCSV(std::size_t rows) { |
| 10 | + std::ostringstream ss; |
| 11 | + for (std::size_t i = 0; i < rows; ++i) { |
| 12 | + ss << i << ",value" << i << ",0.123\n"; |
| 13 | + } |
| 14 | + return ss.str(); |
| 15 | +} |
| 16 | + |
| 17 | +void BenchCSVRowReader(benchmark::State& state) { |
| 18 | + std::string data = GenerateCSV(state.range(0)); |
| 19 | + for (auto s : state) { |
| 20 | + std::istringstream in(data); |
| 21 | + csv::CSVRowReader reader(in); |
| 22 | + while (reader.ReadRow()) { |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + state.SetItemsProcessed(state.iterations() * state.range(0)); |
| 27 | +} |
| 28 | +BENCHMARK(BenchCSVRowReader)->Range(100, 10000); |
| 29 | + |
| 30 | +void BenchCSVBatchReader(benchmark::State& state) { |
| 31 | + core::Schema schema({core::Field("id", core::DataType::Int64), |
| 32 | + core::Field("name", core::DataType::String), |
| 33 | + core::Field("value", core::DataType::Double)}); |
| 34 | + |
| 35 | + std::string data = GenerateCSV(state.range(0)); |
| 36 | + for (auto s : state) { |
| 37 | + std::istringstream in(data); |
| 38 | + csv::CSVBatchReader reader(in, schema, {}); |
| 39 | + while (reader.ReadNext()) { |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + state.SetItemsProcessed(state.iterations() * state.range(0)); |
| 44 | +} |
| 45 | +BENCHMARK(BenchCSVBatchReader)->Range(100, 10000); |
| 46 | + |
| 47 | +void BenchCSVBatchWriter(benchmark::State& state) { |
| 48 | + core::Schema schema( |
| 49 | + {core::Field("id", core::DataType::Int64), core::Field("name", core::DataType::String)}); |
| 50 | + |
| 51 | + core::Batch batch(schema); |
| 52 | + for (int i = 0; i < state.range(0); ++i) { |
| 53 | + batch.ColumnAt(0).AppendFromString(std::to_string(i)); |
| 54 | + batch.ColumnAt(1).AppendFromString("hello mir"); |
| 55 | + } |
| 56 | + |
| 57 | + for (auto s : state) { |
| 58 | + std::ostringstream out; |
| 59 | + csv::CSVBatchWriter writer(out, {}); |
| 60 | + writer.Write(batch); |
| 61 | + writer.Flush(); |
| 62 | + benchmark::DoNotOptimize(out.str()); |
| 63 | + } |
| 64 | + |
| 65 | + state.SetItemsProcessed(state.iterations() * state.range(0)); |
| 66 | +} |
| 67 | +BENCHMARK(BenchCSVBatchWriter)->Range(100, 10000); |
| 68 | + |
| 69 | +BENCHMARK_MAIN(); |
0 commit comments