1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11// A stripped-down version of Chromium's chrome/test/perf/perf_test.cc.
12// ResultsToString(), PrintResult(size_t value) and AppendResult(size_t value)
13// have been modified. The remainder are identical to the Chromium version.
14
15#include "webrtc/test/testsupport/perf_test.h"
16
17#include <sstream>
18#include <stdio.h>
19
20namespace {
21
22std::string ResultsToString(const std::string& measurement,
23                            const std::string& modifier,
24                            const std::string& trace,
25                            const std::string& values,
26                            const std::string& prefix,
27                            const std::string& suffix,
28                            const std::string& units,
29                            bool important) {
30  // <*>RESULT <graph_name>: <trace_name>= <value> <units>
31  // <*>RESULT <graph_name>: <trace_name>= {<mean>, <std deviation>} <units>
32  // <*>RESULT <graph_name>: <trace_name>= [<value>,value,value,...,] <units>
33
34  // TODO(ajm): Use of a stream here may violate the style guide (depending on
35  // one's definition of "logging"). Consider adding StringPrintf-like
36  // functionality as in the original Chromium implementation.
37  std::ostringstream stream;
38  if (important) {
39    stream << "*";
40  }
41  stream << "RESULT " << measurement << modifier << ": " << trace << "= "
42         << prefix << values << suffix << " " << units << std::endl;
43  return stream.str();
44}
45
46void PrintResultsImpl(const std::string& measurement,
47                      const std::string& modifier,
48                      const std::string& trace,
49                      const std::string& values,
50                      const std::string& prefix,
51                      const std::string& suffix,
52                      const std::string& units,
53                      bool important) {
54  printf("%s", ResultsToString(measurement, modifier, trace, values,
55                               prefix, suffix, units, important).c_str());
56}
57
58}  // namespace
59
60namespace webrtc {
61namespace test {
62
63void PrintResult(const std::string& measurement,
64                 const std::string& modifier,
65                 const std::string& trace,
66                 size_t value,
67                 const std::string& units,
68                 bool important) {
69  std::ostringstream value_stream;
70  value_stream << value;
71  PrintResultsImpl(measurement, modifier, trace, value_stream.str(), "", "",
72                   units, important);
73}
74
75void AppendResult(std::string& output,
76                  const std::string& measurement,
77                  const std::string& modifier,
78                  const std::string& trace,
79                  size_t value,
80                  const std::string& units,
81                  bool important) {
82  std::ostringstream value_stream;
83  value_stream << value;
84  output += ResultsToString(measurement, modifier, trace,
85                            value_stream.str(),
86                            "", "", units, important);
87}
88
89void PrintResult(const std::string& measurement,
90                 const std::string& modifier,
91                 const std::string& trace,
92                 const std::string& value,
93                 const std::string& units,
94                 bool important) {
95  PrintResultsImpl(measurement, modifier, trace, value, "", "", units,
96                   important);
97}
98
99void AppendResult(std::string& output,
100                  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  output += ResultsToString(measurement, modifier, trace, value, "", "", units,
107                            important);
108}
109
110void PrintResultMeanAndError(const std::string& measurement,
111                             const std::string& modifier,
112                             const std::string& trace,
113                             const std::string& mean_and_error,
114                             const std::string& units,
115                             bool important) {
116  PrintResultsImpl(measurement, modifier, trace, mean_and_error,
117                   "{", "}", units, important);
118}
119
120void AppendResultMeanAndError(std::string& output,
121                              const std::string& measurement,
122                              const std::string& modifier,
123                              const std::string& trace,
124                              const std::string& mean_and_error,
125                              const std::string& units,
126                              bool important) {
127  output += ResultsToString(measurement, modifier, trace, mean_and_error,
128                            "{", "}", units, important);
129}
130
131void PrintResultList(const std::string& measurement,
132                     const std::string& modifier,
133                     const std::string& trace,
134                     const std::string& values,
135                     const std::string& units,
136                     bool important) {
137  PrintResultsImpl(measurement, modifier, trace, values,
138                   "[", "]", units, important);
139}
140
141void AppendResultList(std::string& output,
142                      const std::string& measurement,
143                      const std::string& modifier,
144                      const std::string& trace,
145                      const std::string& values,
146                      const std::string& units,
147                      bool important) {
148  output += ResultsToString(measurement, modifier, trace, values,
149                            "[", "]", units, important);
150}
151
152void PrintSystemCommitCharge(const std::string& test_name,
153                             size_t charge,
154                             bool important) {
155  PrintSystemCommitCharge(stdout, test_name, charge, important);
156}
157
158void PrintSystemCommitCharge(FILE* target,
159                             const std::string& test_name,
160                             size_t charge,
161                             bool important) {
162  fprintf(target, "%s", SystemCommitChargeToString(test_name, charge,
163                                                   important).c_str());
164}
165
166std::string SystemCommitChargeToString(const std::string& test_name,
167                                       size_t charge,
168                                       bool important) {
169  std::string trace_name(test_name);
170  std::string output;
171  AppendResult(output, "commit_charge", "", "cc" + trace_name, charge, "kb",
172               important);
173  return output;
174}
175
176}  // namespace test
177}  // namespace webrtc
178