• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "timers.h"
17 
18 #include <cstdlib>
19 
20 #include <iostream>
21 #include <tuple>
22 #include <vector>
23 
24 #include "check.h"
25 
26 namespace benchmark {
27 
BenchmarkReporter()28 BenchmarkReporter::BenchmarkReporter()
29     : output_stream_(&std::cout), error_stream_(&std::cerr) {}
30 
~BenchmarkReporter()31 BenchmarkReporter::~BenchmarkReporter() {}
32 
PrintBasicContext(std::ostream * out,Context const & context)33 void BenchmarkReporter::PrintBasicContext(std::ostream *out,
34                                           Context const &context) {
35   CHECK(out) << "cannot be null";
36   auto &Out = *out;
37 
38   Out << LocalDateTimeString() << "\n";
39 
40   const CPUInfo &info = context.cpu_info;
41   Out << "Run on (" << info.num_cpus << " X "
42       << (info.cycles_per_second / 1000000.0) << " MHz CPU "
43       << ((info.num_cpus > 1) ? "s" : "") << ")\n";
44   if (info.caches.size() != 0) {
45     Out << "CPU Caches:\n";
46     for (auto &CInfo : info.caches) {
47       Out << "  L" << CInfo.level << " " << CInfo.type << " "
48           << (CInfo.size / 1000) << "K";
49       if (CInfo.num_sharing != 0)
50         Out << " (x" << (info.num_cpus / CInfo.num_sharing) << ")";
51       Out << "\n";
52     }
53   }
54 
55   if (info.scaling_enabled) {
56     Out << "***WARNING*** CPU scaling is enabled, the benchmark "
57            "real time measurements may be noisy and will incur extra "
58            "overhead.\n";
59   }
60 
61 #ifndef NDEBUG
62   Out << "***WARNING*** Library was built as DEBUG. Timings may be "
63          "affected.\n";
64 #endif
65 }
66 
Context()67 BenchmarkReporter::Context::Context() : cpu_info(CPUInfo::Get()) {}
68 
GetAdjustedRealTime() const69 double BenchmarkReporter::Run::GetAdjustedRealTime() const {
70   double new_time = real_accumulated_time * GetTimeUnitMultiplier(time_unit);
71   if (iterations != 0) new_time /= static_cast<double>(iterations);
72   return new_time;
73 }
74 
GetAdjustedCPUTime() const75 double BenchmarkReporter::Run::GetAdjustedCPUTime() const {
76   double new_time = cpu_accumulated_time * GetTimeUnitMultiplier(time_unit);
77   if (iterations != 0) new_time /= static_cast<double>(iterations);
78   return new_time;
79 }
80 
81 }  // end namespace benchmark
82