benchmark/src/csv_reporter.cc
Go to the documentation of this file.
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/benchmark.h"
16 #include "complexity.h"
17 
18 #include <algorithm>
19 #include <cstdint>
20 #include <iostream>
21 #include <string>
22 #include <tuple>
23 #include <vector>
24 
25 #include "check.h"
26 #include "string_util.h"
27 #include "timers.h"
28 
29 // File format reference: http://edoceo.com/utilitas/csv-file-format.
30 
31 namespace benchmark {
32 
33 namespace {
34 std::vector<std::string> elements = {
35  "name", "iterations", "real_time", "cpu_time",
36  "time_unit", "bytes_per_second", "items_per_second", "label",
37  "error_occurred", "error_message"};
38 } // namespace
39 
42  tmp.reserve(s.size() + 2);
43  for (char c : s) {
44  switch (c) {
45  case '"' : tmp += "\"\""; break;
46  default : tmp += c; break;
47  }
48  }
49  return '"' + tmp + '"';
50 }
51 
54  return true;
55 }
56 
57 void CSVReporter::ReportRuns(const std::vector<Run>& reports) {
58  std::ostream& Out = GetOutputStream();
59 
60  if (!printed_header_) {
61  // save the names of all the user counters
62  for (const auto& run : reports) {
63  for (const auto& cnt : run.counters) {
64  if (cnt.first == "bytes_per_second" || cnt.first == "items_per_second")
65  continue;
66  user_counter_names_.insert(cnt.first);
67  }
68  }
69 
70  // print the header
71  for (auto B = elements.begin(); B != elements.end();) {
72  Out << *B++;
73  if (B != elements.end()) Out << ",";
74  }
75  for (auto B = user_counter_names_.begin();
76  B != user_counter_names_.end();) {
77  Out << ",\"" << *B++ << "\"";
78  }
79  Out << "\n";
80 
81  printed_header_ = true;
82  } else {
83  // check that all the current counters are saved in the name set
84  for (const auto& run : reports) {
85  for (const auto& cnt : run.counters) {
86  if (cnt.first == "bytes_per_second" || cnt.first == "items_per_second")
87  continue;
88  BM_CHECK(user_counter_names_.find(cnt.first) !=
89  user_counter_names_.end())
90  << "All counters must be present in each run. "
91  << "Counter named \"" << cnt.first
92  << "\" was not in a run after being added to the header";
93  }
94  }
95  }
96 
97  // print results for each run
98  for (const auto& run : reports) {
100  }
101 }
102 
104  std::ostream& Out = GetOutputStream();
105  Out << CsvEscape(run.benchmark_name()) << ",";
106  if (run.error_occurred) {
107  Out << std::string(elements.size() - 3, ',');
108  Out << "true,";
109  Out << CsvEscape(run.error_message) << "\n";
110  return;
111  }
112 
113  // Do not print iteration on bigO and RMS report
114  if (!run.report_big_o && !run.report_rms) {
115  Out << run.iterations;
116  }
117  Out << ",";
118 
119  Out << run.GetAdjustedRealTime() << ",";
120  Out << run.GetAdjustedCPUTime() << ",";
121 
122  // Do not print timeLabel on bigO and RMS report
123  if (run.report_big_o) {
124  Out << GetBigOString(run.complexity);
125  } else if (!run.report_rms) {
126  Out << GetTimeUnitString(run.time_unit);
127  }
128  Out << ",";
129 
130  if (run.counters.find("bytes_per_second") != run.counters.end()) {
131  Out << run.counters.at("bytes_per_second");
132  }
133  Out << ",";
134  if (run.counters.find("items_per_second") != run.counters.end()) {
135  Out << run.counters.at("items_per_second");
136  }
137  Out << ",";
138  if (!run.report_label.empty()) {
139  Out << CsvEscape(run.report_label);
140  }
141  Out << ",,"; // for error_occurred and error_message
142 
143  // Print user counters
144  for (const auto& ucn : user_counter_names_) {
145  auto it = run.counters.find(ucn);
146  if (it == run.counters.end()) {
147  Out << ",";
148  } else {
149  Out << "," << it->second;
150  }
151  }
152  Out << '\n';
153 }
154 
155 } // end namespace benchmark
benchmark::BenchmarkReporter::GetErrorStream
std::ostream & GetErrorStream() const
Definition: benchmark/include/benchmark/benchmark.h:1555
regen-readme.it
it
Definition: regen-readme.py:15
check.h
benchmark
Definition: bm_alarm.cc:55
asyncio_get_stats.default
default
Definition: asyncio_get_stats.py:38
BM_CHECK
#define BM_CHECK(b)
Definition: benchmark/src/check.h:58
timers.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
benchmark::CSVReporter::ReportContext
virtual bool ReportContext(const Context &context)
Definition: benchmark/src/csv_reporter.cc:52
benchmark::GetTimeUnitString
const char * GetTimeUnitString(TimeUnit unit)
Definition: benchmark/include/benchmark/benchmark.h:1650
benchmark::BenchmarkReporter::PrintBasicContext
static void PrintBasicContext(std::ostream *out, Context const &context)
Definition: benchmark/src/reporter.cc:39
benchmark::BenchmarkReporter::Run
Definition: benchmark/include/benchmark/benchmark.h:1423
benchmark::GetBigOString
std::string GetBigOString(BigO complexity)
Definition: benchmark/src/complexity.cc:53
string_util.h
client.run
def run()
Definition: examples/python/async_streaming/client.py:109
benchmark::CSVReporter::user_counter_names_
std::set< std::string > user_counter_names_
Definition: bloaty/third_party/protobuf/third_party/benchmark/include/benchmark/benchmark.h:1181
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
complexity.h
benchmark::BenchmarkReporter::Context
Definition: benchmark/include/benchmark/benchmark.h:1414
benchmark::BenchmarkReporter::GetOutputStream
std::ostream & GetOutputStream() const
Definition: benchmark/include/benchmark/benchmark.h:1553
benchmark::CSVReporter::PrintRunData
void PrintRunData(const Run &report)
Definition: benchmark/src/csv_reporter.cc:103
benchmark::CSVReporter::printed_header_
bool printed_header_
Definition: bloaty/third_party/protobuf/third_party/benchmark/include/benchmark/benchmark.h:1180
benchmark::CsvEscape
std::string CsvEscape(const std::string &s)
Definition: benchmark/src/csv_reporter.cc:40
benchmark::CSVReporter::ReportRuns
virtual void ReportRuns(const std::vector< Run > &reports)
Definition: benchmark/src/csv_reporter.cc:57


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:08