|
| 1 | +#include "bench_framework.hpp" |
| 2 | +#include <cstring> |
| 3 | +#include <cstdlib> |
| 4 | +#include "../plugins/input/csv/csv_utils.hpp" |
| 5 | + |
| 6 | +class test : public benchmark::test_case |
| 7 | +{ |
| 8 | +public: |
| 9 | + std::string line_data_; |
| 10 | + test(mapnik::parameters const& params) |
| 11 | + : test_case(params) |
| 12 | + { |
| 13 | + boost::optional<std::string> line_data = params.get<std::string>("line"); |
| 14 | + if (!line_data) |
| 15 | + { |
| 16 | + throw std::runtime_error("please provide a --line \"one line\ntwo line\""); |
| 17 | + } |
| 18 | + line_data_ = *line_data; |
| 19 | + } |
| 20 | + |
| 21 | + bool validate() const |
| 22 | + { |
| 23 | + std::string first = line_data_.substr(line_data_.find_first_not_of('\n')); |
| 24 | + char newline = '\n'; |
| 25 | + std::string csv_line; |
| 26 | + std::stringstream s; |
| 27 | + s << line_data_; |
| 28 | + std::getline(s,csv_line,s.widen(newline)); |
| 29 | + if (csv_line != first) |
| 30 | + { |
| 31 | + return true; |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + 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"; |
| 36 | + } |
| 37 | + return true; |
| 38 | + } |
| 39 | + bool operator()() const |
| 40 | + { |
| 41 | + char newline = '\n'; |
| 42 | + std::string csv_line; |
| 43 | + std::stringstream s; |
| 44 | + s << line_data_; |
| 45 | + for (unsigned i=0;i<iterations_;++i) |
| 46 | + { |
| 47 | + std::getline(s,csv_line,s.widen(newline)); |
| 48 | + } |
| 49 | + return true; |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | + |
| 54 | +class test2 : public benchmark::test_case |
| 55 | +{ |
| 56 | +public: |
| 57 | + std::string line_data_; |
| 58 | + test2(mapnik::parameters const& params) |
| 59 | + : test_case(params) |
| 60 | + { |
| 61 | + boost::optional<std::string> line_data = params.get<std::string>("line"); |
| 62 | + if (!line_data) |
| 63 | + { |
| 64 | + throw std::runtime_error("please provide a --line \"one line\ntwo line\""); |
| 65 | + } |
| 66 | + line_data_ = *line_data; |
| 67 | + } |
| 68 | + |
| 69 | + bool validate() const |
| 70 | + { |
| 71 | + std::string first = line_data_.substr(line_data_.find_first_not_of('\n')); |
| 72 | + char newline = '\n'; |
| 73 | + std::string csv_line; |
| 74 | + std::stringstream s; |
| 75 | + s << line_data_; |
| 76 | + csv_utils::getline_csv(s,csv_line,s.widen(newline)); |
| 77 | + if (csv_line != first) |
| 78 | + { |
| 79 | + return true; |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + 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"; |
| 84 | + } |
| 85 | + return true; |
| 86 | + } |
| 87 | + bool operator()() const |
| 88 | + { |
| 89 | + char newline = '\n'; |
| 90 | + std::string csv_line; |
| 91 | + std::stringstream s; |
| 92 | + s << line_data_; |
| 93 | + for (unsigned i=0;i<iterations_;++i) |
| 94 | + { |
| 95 | + csv_utils::getline_csv(s,csv_line,s.widen(newline)); |
| 96 | + } |
| 97 | + return true; |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +int main(int argc, char** argv) |
| 102 | +{ |
| 103 | + int return_value = 0; |
| 104 | + try |
| 105 | + { |
| 106 | + mapnik::parameters params; |
| 107 | + benchmark::handle_args(argc,argv,params); |
| 108 | + { |
| 109 | + test test_runner(params); |
| 110 | + return_value = return_value | run(test_runner,"std::getline"); |
| 111 | + } |
| 112 | + { |
| 113 | + test2 test_runner2(params); |
| 114 | + return_value = return_value | run(test_runner2,"csv_utils::getline_csv"); |
| 115 | + } |
| 116 | + } |
| 117 | + catch (std::exception const& ex) |
| 118 | + { |
| 119 | + std::clog << ex.what() << "\n"; |
| 120 | + return -1; |
| 121 | + } |
| 122 | + return return_value; |
| 123 | +} |
0 commit comments