Home | History | Annotate | Download | only in perf
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "testing/perf/perf_test.h"
      6 
      7 #include <stdio.h>
      8 
      9 #include "base/logging.h"
     10 #include "base/strings/string_number_conversions.h"
     11 #include "base/strings/stringprintf.h"
     12 
     13 namespace {
     14 
     15 std::string ResultsToString(const std::string& measurement,
     16                             const std::string& modifier,
     17                             const std::string& trace,
     18                             const std::string& values,
     19                             const std::string& prefix,
     20                             const std::string& suffix,
     21                             const std::string& units,
     22                             bool important) {
     23   // <*>RESULT <graph_name>: <trace_name>= <value> <units>
     24   // <*>RESULT <graph_name>: <trace_name>= {<mean>, <std deviation>} <units>
     25   // <*>RESULT <graph_name>: <trace_name>= [<value>,value,value,...,] <units>
     26   return base::StringPrintf("%sRESULT %s%s: %s= %s%s%s %s\n",
     27          important ? "*" : "", measurement.c_str(), modifier.c_str(),
     28          trace.c_str(), prefix.c_str(), values.c_str(), suffix.c_str(),
     29          units.c_str());
     30 }
     31 
     32 void PrintResultsImpl(const std::string& measurement,
     33                       const std::string& modifier,
     34                       const std::string& trace,
     35                       const std::string& values,
     36                       const std::string& prefix,
     37                       const std::string& suffix,
     38                       const std::string& units,
     39                       bool important) {
     40   fflush(stdout);
     41   printf("%s", ResultsToString(measurement, modifier, trace, values,
     42                                prefix, suffix, units, important).c_str());
     43   fflush(stdout);
     44 }
     45 
     46 }  // namespace
     47 
     48 namespace perf_test {
     49 
     50 void PrintResult(const std::string& measurement,
     51                  const std::string& modifier,
     52                  const std::string& trace,
     53                  size_t value,
     54                  const std::string& units,
     55                  bool important) {
     56   PrintResultsImpl(measurement,
     57                    modifier,
     58                    trace,
     59                    base::UintToString(static_cast<unsigned int>(value)),
     60                    std::string(),
     61                    std::string(),
     62                    units,
     63                    important);
     64 }
     65 
     66 void PrintResult(const std::string& measurement,
     67                  const std::string& modifier,
     68                  const std::string& trace,
     69                  double value,
     70                  const std::string& units,
     71                  bool important) {
     72   PrintResultsImpl(measurement,
     73                    modifier,
     74                    trace,
     75                    base::DoubleToString(value),
     76                    std::string(),
     77                    std::string(),
     78                    units,
     79                    important);
     80 }
     81 
     82 void AppendResult(std::string& output,
     83                   const std::string& measurement,
     84                   const std::string& modifier,
     85                   const std::string& trace,
     86                   size_t value,
     87                   const std::string& units,
     88                   bool important) {
     89   output += ResultsToString(
     90       measurement,
     91       modifier,
     92       trace,
     93       base::UintToString(static_cast<unsigned int>(value)),
     94       std::string(),
     95       std::string(),
     96       units,
     97       important);
     98 }
     99 
    100 void PrintResult(const std::string& measurement,
    101                  const std::string& modifier,
    102                  const std::string& trace,
    103                  const std::string& value,
    104                  const std::string& units,
    105                  bool important) {
    106   PrintResultsImpl(measurement,
    107                    modifier,
    108                    trace,
    109                    value,
    110                    std::string(),
    111                    std::string(),
    112                    units,
    113                    important);
    114 }
    115 
    116 void AppendResult(std::string& output,
    117                   const std::string& measurement,
    118                   const std::string& modifier,
    119                   const std::string& trace,
    120                   const std::string& value,
    121                   const std::string& units,
    122                   bool important) {
    123   output += ResultsToString(measurement,
    124                             modifier,
    125                             trace,
    126                             value,
    127                             std::string(),
    128                             std::string(),
    129                             units,
    130                             important);
    131 }
    132 
    133 void PrintResultMeanAndError(const std::string& measurement,
    134                              const std::string& modifier,
    135                              const std::string& trace,
    136                              const std::string& mean_and_error,
    137                              const std::string& units,
    138                              bool important) {
    139   PrintResultsImpl(measurement, modifier, trace, mean_and_error,
    140                    "{", "}", units, important);
    141 }
    142 
    143 void AppendResultMeanAndError(std::string& output,
    144                               const std::string& measurement,
    145                               const std::string& modifier,
    146                               const std::string& trace,
    147                               const std::string& mean_and_error,
    148                               const std::string& units,
    149                               bool important) {
    150   output += ResultsToString(measurement, modifier, trace, mean_and_error,
    151                             "{", "}", units, important);
    152 }
    153 
    154 void PrintResultList(const std::string& measurement,
    155                      const std::string& modifier,
    156                      const std::string& trace,
    157                      const std::string& values,
    158                      const std::string& units,
    159                      bool important) {
    160   PrintResultsImpl(measurement, modifier, trace, values,
    161                    "[", "]", units, important);
    162 }
    163 
    164 void AppendResultList(std::string& output,
    165                       const std::string& measurement,
    166                       const std::string& modifier,
    167                       const std::string& trace,
    168                       const std::string& values,
    169                       const std::string& units,
    170                       bool important) {
    171   output += ResultsToString(measurement, modifier, trace, values,
    172                             "[", "]", units, important);
    173 }
    174 
    175 void PrintSystemCommitCharge(const std::string& test_name,
    176                              size_t charge,
    177                              bool important) {
    178   PrintSystemCommitCharge(stdout, test_name, charge, important);
    179 }
    180 
    181 void PrintSystemCommitCharge(FILE* target,
    182                              const std::string& test_name,
    183                              size_t charge,
    184                              bool important) {
    185   fprintf(target, "%s", SystemCommitChargeToString(test_name, charge,
    186                                                    important).c_str());
    187 }
    188 
    189 std::string SystemCommitChargeToString(const std::string& test_name,
    190                                        size_t charge,
    191                                        bool important) {
    192   std::string trace_name(test_name);
    193   std::string output;
    194   AppendResult(output,
    195                "commit_charge",
    196                std::string(),
    197                "cc" + trace_name,
    198                charge,
    199                "kb",
    200                important);
    201   return output;
    202 }
    203 
    204 }  // namespace perf_test
    205