xref: /aosp_15_r20/external/cronet/third_party/google_benchmark/src/src/reporter.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 <cstdlib>
16 #include <iostream>
17 #include <map>
18 #include <string>
19 #include <tuple>
20 #include <vector>
21 
22 #include "benchmark/benchmark.h"
23 #include "check.h"
24 #include "string_util.h"
25 #include "timers.h"
26 
27 namespace benchmark {
28 
BenchmarkReporter()29 BenchmarkReporter::BenchmarkReporter()
30     : output_stream_(&std::cout), error_stream_(&std::cerr) {}
31 
~BenchmarkReporter()32 BenchmarkReporter::~BenchmarkReporter() {}
33 
PrintBasicContext(std::ostream * out,Context const & context)34 void BenchmarkReporter::PrintBasicContext(std::ostream *out,
35                                           Context const &context) {
36   BM_CHECK(out) << "cannot be null";
37   auto &Out = *out;
38 
39 #ifndef BENCHMARK_OS_QURT
40   // Date/time information is not available on QuRT.
41   // Attempting to get it via this call cause the binary to crash.
42   Out << LocalDateTimeString() << "\n";
43 #endif
44 
45   if (context.executable_name)
46     Out << "Running " << context.executable_name << "\n";
47 
48   const CPUInfo &info = context.cpu_info;
49   Out << "Run on (" << info.num_cpus << " X "
50       << (info.cycles_per_second / 1000000.0) << " MHz CPU "
51       << ((info.num_cpus > 1) ? "s" : "") << ")\n";
52   if (info.caches.size() != 0) {
53     Out << "CPU Caches:\n";
54     for (auto &CInfo : info.caches) {
55       Out << "  L" << CInfo.level << " " << CInfo.type << " "
56           << (CInfo.size / 1024) << " KiB";
57       if (CInfo.num_sharing != 0)
58         Out << " (x" << (info.num_cpus / CInfo.num_sharing) << ")";
59       Out << "\n";
60     }
61   }
62   if (!info.load_avg.empty()) {
63     Out << "Load Average: ";
64     for (auto It = info.load_avg.begin(); It != info.load_avg.end();) {
65       Out << StrFormat("%.2f", *It++);
66       if (It != info.load_avg.end()) Out << ", ";
67     }
68     Out << "\n";
69   }
70 
71   std::map<std::string, std::string> *global_context =
72       internal::GetGlobalContext();
73 
74   if (global_context != nullptr) {
75     for (const auto &kv : *global_context) {
76       Out << kv.first << ": " << kv.second << "\n";
77     }
78   }
79 
80   if (CPUInfo::Scaling::ENABLED == info.scaling) {
81     Out << "***WARNING*** CPU scaling is enabled, the benchmark "
82            "real time measurements may be noisy and will incur extra "
83            "overhead.\n";
84   }
85 
86 #ifndef NDEBUG
87   Out << "***WARNING*** Library was built as DEBUG. Timings may be "
88          "affected.\n";
89 #endif
90 }
91 
92 // No initializer because it's already initialized to NULL.
93 const char *BenchmarkReporter::Context::executable_name;
94 
Context()95 BenchmarkReporter::Context::Context()
96     : cpu_info(CPUInfo::Get()), sys_info(SystemInfo::Get()) {}
97 
benchmark_name() const98 std::string BenchmarkReporter::Run::benchmark_name() const {
99   std::string name = run_name.str();
100   if (run_type == RT_Aggregate) {
101     name += "_" + aggregate_name;
102   }
103   return name;
104 }
105 
GetAdjustedRealTime() const106 double BenchmarkReporter::Run::GetAdjustedRealTime() const {
107   double new_time = real_accumulated_time * GetTimeUnitMultiplier(time_unit);
108   if (iterations != 0) new_time /= static_cast<double>(iterations);
109   return new_time;
110 }
111 
GetAdjustedCPUTime() const112 double BenchmarkReporter::Run::GetAdjustedCPUTime() const {
113   double new_time = cpu_accumulated_time * GetTimeUnitMultiplier(time_unit);
114   if (iterations != 0) new_time /= static_cast<double>(iterations);
115   return new_time;
116 }
117 
118 }  // end namespace benchmark
119