1 // Copyright 2015 Google Inc. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "benchmark/reporter.h" 16 17 #include <cstdint> 18 #include <iostream> 19 #include <string> 20 #include <vector> 21 22 #include "string_util.h" 23 #include "walltime.h" 24 25 // File format reference: http://edoceo.com/utilitas/csv-file-format. 26 27 namespace benchmark { 28 29 bool CSVReporter::ReportContext(const Context& context) { 30 std::cerr << "Run on (" << context.num_cpus << " X " << context.mhz_per_cpu 31 << " MHz CPU " << ((context.num_cpus > 1) ? "s" : "") << ")\n"; 32 33 std::cerr << LocalDateTimeString() << "\n"; 34 35 if (context.cpu_scaling_enabled) { 36 std::cerr << "***WARNING*** CPU scaling is enabled, the benchmark " 37 "real time measurements may be noisy and will incur extra " 38 "overhead.\n"; 39 } 40 41 #ifndef NDEBUG 42 std::cerr << "***WARNING*** Library was built as DEBUG. Timings may be " 43 "affected.\n"; 44 #endif 45 std::cout << "name,iterations,real_time,cpu_time,bytes_per_second," 46 "items_per_second,label\n"; 47 return true; 48 } 49 50 void CSVReporter::ReportRuns(std::vector<Run> const& reports) { 51 if (reports.empty()) { 52 return; 53 } 54 55 std::vector<Run> reports_cp = reports; 56 if (reports.size() >= 2) { 57 Run mean_data; 58 Run stddev_data; 59 BenchmarkReporter::ComputeStats(reports, &mean_data, &stddev_data); 60 reports_cp.push_back(mean_data); 61 reports_cp.push_back(stddev_data); 62 } 63 for (auto it = reports_cp.begin(); it != reports_cp.end(); ++it) { 64 PrintRunData(*it); 65 } 66 } 67 68 void CSVReporter::PrintRunData(Run const& run) { 69 double const multiplier = 1e9; // nano second multiplier 70 double cpu_time = run.cpu_accumulated_time * multiplier; 71 double real_time = run.real_accumulated_time * multiplier; 72 if (run.iterations != 0) { 73 real_time = real_time / static_cast<double>(run.iterations); 74 cpu_time = cpu_time / static_cast<double>(run.iterations); 75 } 76 77 // Field with embedded double-quote characters must be doubled and the field 78 // delimited with double-quotes. 79 std::string name = run.benchmark_name; 80 ReplaceAll(&name, "\"", "\"\""); 81 std::cout << "\"" << name << "\","; 82 83 std::cout << run.iterations << ","; 84 std::cout << real_time << ","; 85 std::cout << cpu_time << ","; 86 87 if (run.bytes_per_second > 0.0) { 88 std::cout << run.bytes_per_second; 89 } 90 std::cout << ","; 91 if (run.items_per_second > 0.0) { 92 std::cout << run.items_per_second; 93 } 94 std::cout << ","; 95 if (!run.report_label.empty()) { 96 // Field with embedded double-quote characters must be doubled and the field 97 // delimited with double-quotes. 98 std::string label = run.report_label; 99 ReplaceAll(&label, "\"", "\"\""); 100 std::cout << "\"" << label << "\""; 101 } 102 std::cout << '\n'; 103 } 104 105 } // end namespace benchmark 106