mapnik/benchmark/test_getline.cpp

126 lines
3.3 KiB
C++
Raw Normal View History

2015-10-01 23:47:55 +02:00
#include "bench_framework.hpp"
#include "../plugins/input/csv/csv_getline.hpp"
2015-10-01 23:47:55 +02:00
class test : public benchmark::test_case
{
public:
std::string line_data_;
test(mapnik::parameters const& params)
: test_case(params),
line_data_("this is one line\nand this is a second line\nand a third line")
2015-10-01 23:47:55 +02:00
{
boost::optional<std::string> line_data = params.get<std::string>("line");
if (line_data)
2015-10-01 23:47:55 +02:00
{
line_data_ = *line_data;
2015-10-01 23:47:55 +02:00
}
}
bool validate() const
{
std::string first = line_data_.substr(line_data_.find_first_not_of('\n'));
char newline = '\n';
std::string csv_line;
std::stringstream s;
s << line_data_;
2015-10-05 16:59:28 +02:00
std::getline(s,csv_line,newline);
2015-10-01 23:47:55 +02:00
if (csv_line != first)
{
return true;
}
else
{
std::clog << "Error: the parsed line (" << csv_line << ") should be a subset of the original line (" << line_data_ << ") (ensure you pass a line with a \\n)\n";
}
return true;
}
bool operator()() const
{
char newline = '\n';
std::string csv_line;
std::stringstream s;
s << line_data_;
for (unsigned i=0;i<iterations_;++i)
{
2015-10-05 16:59:28 +02:00
std::getline(s,csv_line,newline);
2015-10-01 23:47:55 +02:00
}
return true;
}
};
class test2 : public benchmark::test_case
{
public:
std::string line_data_;
test2(mapnik::parameters const& params)
: test_case(params),
line_data_("this is one line\nand this is a second line\nand a third line")
2015-10-01 23:47:55 +02:00
{
boost::optional<std::string> line_data = params.get<std::string>("line");
if (line_data)
2015-10-01 23:47:55 +02:00
{
line_data_ = *line_data;
2015-10-01 23:47:55 +02:00
}
}
bool validate() const
{
std::string first = line_data_.substr(line_data_.find_first_not_of('\n'));
char newline = '\n';
2015-10-05 16:59:28 +02:00
char quote = '"';
2015-10-01 23:47:55 +02:00
std::string csv_line;
std::stringstream s;
s << line_data_;
2015-10-05 16:59:28 +02:00
csv_utils::getline_csv(s,csv_line,newline,quote);
2015-10-01 23:47:55 +02:00
if (csv_line != first)
{
return true;
}
else
{
std::clog << "Error: the parsed line (" << csv_line << ") should be a subset of the original line (" << line_data_ << ") (ensure you pass a line with a \\n)\n";
}
return true;
}
bool operator()() const
{
char newline = '\n';
2015-10-05 16:59:28 +02:00
char quote = '"';
2015-10-01 23:47:55 +02:00
std::string csv_line;
std::stringstream s;
s << line_data_;
for (unsigned i=0;i<iterations_;++i)
{
2015-10-05 16:59:28 +02:00
csv_utils::getline_csv(s,csv_line,newline,quote);
2015-10-01 23:47:55 +02:00
}
return true;
}
};
int main(int argc, char** argv)
{
int return_value = 0;
try
{
mapnik::parameters params;
benchmark::handle_args(argc,argv,params);
{
test test_runner(params);
return_value = return_value | run(test_runner,"std::getline");
}
{
test2 test_runner2(params);
return_value = return_value | run(test_runner2,"csv_utils::getline_csv");
}
}
catch (std::exception const& ex)
{
std::clog << ex.what() << "\n";
return -1;
}
return return_value;
}