You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

46 lines
1.1 KiB

/**
* Copyright (c) 2018 Niklas Rosenstein
* MIT licensed.
*/
#include <iostream>
#include <sstream>
#include <benchmark/benchmark.h>
#include <NiklasRosenstein/csv.hpp>
namespace nr = niklasrosenstein;
static void generate_csv(std::ostream& data, int lines) {
srand(1);
for (int i = 0; i < lines; ++i) {
data << rand() << ",\"" << rand() % 100 << "\"," << rand() % 3 << "\n";
}
}
static void BM_Csv_ParseFile(benchmark::State& state) {
int const lines = state.range(0);
std::stringstream indata;
generate_csv(indata, lines);
while (state.KeepRunning()) {
bool error = false;
int have_lines = 0;
nr::csv_parse(indata, [&](nr::csv_row const& row) -> bool {
if (row.size() != 3) {
error = true;
state.SkipWithError("a row did not have 3 columns");
return false;
}
++have_lines;
return true;
});
if (have_lines != lines) {
state.SkipWithError("could not read as many lines as generated");
break;
}
indata.clear();
indata.seekg(0);
}
}
BENCHMARK(BM_Csv_ParseFile)->Range(10, 10<<10);