Home | History | Annotate | Download | only in src
      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 #include "string_util.h"
     26 
     27 namespace benchmark {
     28 
     29 BenchmarkReporter::BenchmarkReporter()
     30     : output_stream_(&std::cout), error_stream_(&std::cerr) {}
     31 
     32 BenchmarkReporter::~BenchmarkReporter() {}
     33 
     34 void BenchmarkReporter::PrintBasicContext(std::ostream *out,
     35                                           Context const &context) {
     36   CHECK(out) << "cannot be null";
     37   auto &Out = *out;
     38 
     39   Out << LocalDateTimeString() << "\n";
     40 
     41   if (context.executable_name)
     42     Out << "Running " << context.executable_name << "\n";
     43 
     44   const CPUInfo &info = context.cpu_info;
     45   Out << "Run on (" << info.num_cpus << " X "
     46       << (info.cycles_per_second / 1000000.0) << " MHz CPU "
     47       << ((info.num_cpus > 1) ? "s" : "") << ")\n";
     48   if (info.caches.size() != 0) {
     49     Out << "CPU Caches:\n";
     50     for (auto &CInfo : info.caches) {
     51       Out << "  L" << CInfo.level << " " << CInfo.type << " "
     52           << (CInfo.size / 1000) << "K";
     53       if (CInfo.num_sharing != 0)
     54         Out << " (x" << (info.num_cpus / CInfo.num_sharing) << ")";
     55       Out << "\n";
     56     }
     57   }
     58   if (!info.load_avg.empty()) {
     59     Out << "Load Average: ";
     60     for (auto It = info.load_avg.begin(); It != info.load_avg.end();) {
     61       Out << StrFormat("%.2f", *It++);
     62       if (It != info.load_avg.end()) Out << ", ";
     63     }
     64     Out << "\n";
     65   }
     66 
     67   if (info.scaling_enabled) {
     68     Out << "***WARNING*** CPU scaling is enabled, the benchmark "
     69            "real time measurements may be noisy and will incur extra "
     70            "overhead.\n";
     71   }
     72 
     73 #ifndef NDEBUG
     74   Out << "***WARNING*** Library was built as DEBUG. Timings may be "
     75          "affected.\n";
     76 #endif
     77 }
     78 
     79 // No initializer because it's already initialized to NULL.
     80 const char *BenchmarkReporter::Context::executable_name;
     81 
     82 BenchmarkReporter::Context::Context()
     83     : cpu_info(CPUInfo::Get()), sys_info(SystemInfo::Get()) {}
     84 
     85 std::string BenchmarkReporter::Run::benchmark_name() const {
     86   std::string name = run_name;
     87   if (run_type == RT_Aggregate) {
     88     name += "_" + aggregate_name;
     89   }
     90   return name;
     91 }
     92 
     93 double BenchmarkReporter::Run::GetAdjustedRealTime() const {
     94   double new_time = real_accumulated_time * GetTimeUnitMultiplier(time_unit);
     95   if (iterations != 0) new_time /= static_cast<double>(iterations);
     96   return new_time;
     97 }
     98 
     99 double BenchmarkReporter::Run::GetAdjustedCPUTime() const {
    100   double new_time = cpu_accumulated_time * GetTimeUnitMultiplier(time_unit);
    101   if (iterations != 0) new_time /= static_cast<double>(iterations);
    102   return new_time;
    103 }
    104 
    105 }  // end namespace benchmark
    106