1 // Copyright 2015 Google Inc. All rights reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "benchmark/benchmark.h"
24 #include "string_util.h"
29 extern std::map
<std::string
, std::string
> *global_context
;
32 BenchmarkReporter::BenchmarkReporter()
33 : output_stream_(&std::cout
), error_stream_(&std::cerr
) {}
35 BenchmarkReporter::~BenchmarkReporter() {}
37 void BenchmarkReporter::PrintBasicContext(std::ostream
*out
,
38 Context
const &context
) {
39 BM_CHECK(out
) << "cannot be null";
42 Out
<< LocalDateTimeString() << "\n";
44 if (context
.executable_name
)
45 Out
<< "Running " << context
.executable_name
<< "\n";
47 const CPUInfo
&info
= context
.cpu_info
;
48 Out
<< "Run on (" << info
.num_cpus
<< " X "
49 << (info
.cycles_per_second
/ 1000000.0) << " MHz CPU "
50 << ((info
.num_cpus
> 1) ? "s" : "") << ")\n";
51 if (info
.caches
.size() != 0) {
52 Out
<< "CPU Caches:\n";
53 for (auto &CInfo
: info
.caches
) {
54 Out
<< " L" << CInfo
.level
<< " " << CInfo
.type
<< " "
55 << (CInfo
.size
/ 1024) << " KiB";
56 if (CInfo
.num_sharing
!= 0)
57 Out
<< " (x" << (info
.num_cpus
/ CInfo
.num_sharing
) << ")";
61 if (!info
.load_avg
.empty()) {
62 Out
<< "Load Average: ";
63 for (auto It
= info
.load_avg
.begin(); It
!= info
.load_avg
.end();) {
64 Out
<< StrFormat("%.2f", *It
++);
65 if (It
!= info
.load_avg
.end()) Out
<< ", ";
70 if (internal::global_context
!= nullptr) {
71 for (const auto &kv
: *internal::global_context
) {
72 Out
<< kv
.first
<< ": " << kv
.second
<< "\n";
76 if (CPUInfo::Scaling::ENABLED
== info
.scaling
) {
77 Out
<< "***WARNING*** CPU scaling is enabled, the benchmark "
78 "real time measurements may be noisy and will incur extra "
83 Out
<< "***WARNING*** Library was built as DEBUG. Timings may be "
88 // No initializer because it's already initialized to NULL.
89 const char *BenchmarkReporter::Context::executable_name
;
91 BenchmarkReporter::Context::Context()
92 : cpu_info(CPUInfo::Get()), sys_info(SystemInfo::Get()) {}
94 std::string
BenchmarkReporter::Run::benchmark_name() const {
95 std::string name
= run_name
.str();
96 if (run_type
== RT_Aggregate
) {
97 name
+= "_" + aggregate_name
;
102 double BenchmarkReporter::Run::GetAdjustedRealTime() const {
103 double new_time
= real_accumulated_time
* GetTimeUnitMultiplier(time_unit
);
104 if (iterations
!= 0) new_time
/= static_cast<double>(iterations
);
108 double BenchmarkReporter::Run::GetAdjustedCPUTime() const {
109 double new_time
= cpu_accumulated_time
* GetTimeUnitMultiplier(time_unit
);
110 if (iterations
!= 0) new_time
/= static_cast<double>(iterations
);
114 } // end namespace benchmark