1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "test/testsupport/perf_test.h"
#include <stdio.h>
#include <algorithm>
#include <fstream>
#include <set>
#include <sstream>
#include <vector>
#include "absl/strings/string_view.h"
#include "api/numerics/samples_stats_counter.h"
#include "rtc_base/checks.h"
#include "rtc_base/strings/string_builder.h"
#include "rtc_base/synchronization/mutex.h"
#include "test/testsupport/file_utils.h"
#include "test/testsupport/perf_test_histogram_writer.h"
namespace webrtc {
namespace test {
namespace {
std::string UnitWithDirection(absl::string_view units,
test::ImproveDirection improve_direction) {
switch (improve_direction) {
case test::ImproveDirection::kNone:
return std::string(units);
case test::ImproveDirection::kSmallerIsBetter:
return std::string(units) + "_smallerIsBetter";
case test::ImproveDirection::kBiggerIsBetter:
return std::string(units) + "_biggerIsBetter";
}
}
std::vector<SamplesStatsCounter::StatsSample> GetSortedSamples(
const SamplesStatsCounter& counter) {
ArrayView<const SamplesStatsCounter::StatsSample> view =
counter.GetTimedSamples();
std::vector<SamplesStatsCounter::StatsSample> out(view.begin(), view.end());
std::stable_sort(out.begin(), out.end(),
[](const SamplesStatsCounter::StatsSample& a,
const SamplesStatsCounter::StatsSample& b) {
return a.time < b.time;
});
return out;
}
template <typename Container>
void OutputListToStream(std::ostream* ostream, const Container& values) {
const char* sep = "";
for (const auto& v : values) {
(*ostream) << sep << v;
sep = ",";
}
}
struct PlottableCounter {
std::string graph_name;
std::string trace_name;
SamplesStatsCounter counter;
std::string units;
};
class PlottableCounterPrinter {
public:
PlottableCounterPrinter() : output_(stdout) {}
void SetOutput(FILE* output) {
MutexLock lock(&mutex_);
output_ = output;
}
void AddCounter(absl::string_view graph_name,
absl::string_view trace_name,
const SamplesStatsCounter& counter,
absl::string_view units) {
MutexLock lock(&mutex_);
plottable_counters_.push_back({std::string(graph_name),
std::string(trace_name), counter,
std::string(units)});
}
void Print(const std::vector<std::string>& desired_graphs_raw) const {
std::set<std::string> desired_graphs(desired_graphs_raw.begin(),
desired_graphs_raw.end());
MutexLock lock(&mutex_);
for (auto& counter : plottable_counters_) {
if (!desired_graphs.empty()) {
auto it = desired_graphs.find(counter.graph_name);
if (it == desired_graphs.end()) {
continue;
}
}
std::ostringstream value_stream;
value_stream.precision(8);
value_stream << R"({"graph_name":")" << counter.graph_name << R"(",)";
value_stream << R"("trace_name":")" << counter.trace_name << R"(",)";
value_stream << R"("units":")" << counter.units << R"(",)";
if (!counter.counter.IsEmpty()) {
value_stream << R"("mean":)" << counter.counter.GetAverage() << ',';
value_stream << R"("std":)" << counter.counter.GetStandardDeviation()
<< ',';
}
value_stream << R"("samples":[)";
const char* sep = "";
for (const auto& sample : counter.counter.GetTimedSamples()) {
value_stream << sep << R"({"time":)" << sample.time.us() << ','
<< R"("value":)" << sample.value << '}';
sep = ",";
}
value_stream << "]}";
fprintf(output_, "PLOTTABLE_DATA: %s\n", value_stream.str().c_str());
}
}
private:
mutable Mutex mutex_;
std::vector<PlottableCounter> plottable_counters_ RTC_GUARDED_BY(&mutex_);
FILE* output_ RTC_GUARDED_BY(&mutex_);
};
PlottableCounterPrinter& GetPlottableCounterPrinter() {
static PlottableCounterPrinter* printer_ = new PlottableCounterPrinter();
return *printer_;
}
class ResultsLinePrinter {
public:
ResultsLinePrinter() : output_(stdout) {}
void SetOutput(FILE* output) {
MutexLock lock(&mutex_);
output_ = output;
}
void PrintResult(absl::string_view graph_name,
absl::string_view trace_name,
const double value,
absl::string_view units,
bool important,
ImproveDirection improve_direction) {
std::ostringstream value_stream;
value_stream.precision(8);
value_stream << value;
PrintResultImpl(graph_name, trace_name, value_stream.str(), std::string(),
std::string(), UnitWithDirection(units, improve_direction),
important);
}
void PrintResultMeanAndError(absl::string_view graph_name,
absl::string_view trace_name,
const double mean,
const double error,
absl::string_view units,
bool important,
ImproveDirection improve_direction) {
std::ostringstream value_stream;
value_stream.precision(8);
value_stream << mean << ',' << error;
PrintResultImpl(graph_name, trace_name, value_stream.str(), "{", "}",
UnitWithDirection(units, improve_direction), important);
}
void PrintResultList(absl::string_view graph_name,
absl::string_view trace_name,
const ArrayView<const double> values,
absl::string_view units,
const bool important,
test::ImproveDirection improve_direction) {
std::ostringstream value_stream;
value_stream.precision(8);
OutputListToStream(&value_stream, values);
PrintResultImpl(graph_name, trace_name, value_stream.str(), "[", "]", units,
important);
}
private:
void PrintResultImpl(absl::string_view graph_name,
absl::string_view trace_name,
absl::string_view values,
absl::string_view prefix,
absl::string_view suffix,
absl::string_view units,
bool important) {
MutexLock lock(&mutex_);
StringBuilder message;
message << (important ? "*" : "") << "RESULT " << graph_name << ": "
<< trace_name << "= " << prefix << values << suffix << " " << units;
// <*>RESULT <graph_name>: <trace_name>= <value> <units>
// <*>RESULT <graph_name>: <trace_name>= {<mean>, <std deviation>} <units>
// <*>RESULT <graph_name>: <trace_name>= [<value>,value,value,...,] <units>
fprintf(output_, "%s\n", message.str().c_str());
}
Mutex mutex_;
FILE* output_ RTC_GUARDED_BY(&mutex_);
};
ResultsLinePrinter& GetResultsLinePrinter() {
static ResultsLinePrinter* const printer_ = new ResultsLinePrinter();
return *printer_;
}
PerfTestResultWriter& GetPerfWriter() {
static PerfTestResultWriter* writer = CreateHistogramWriter();
return *writer;
}
} // namespace
void ClearPerfResults() {
GetPerfWriter().ClearResults();
}
void SetPerfResultsOutput(FILE* output) {
GetPlottableCounterPrinter().SetOutput(output);
GetResultsLinePrinter().SetOutput(output);
}
std::string GetPerfResults() {
return GetPerfWriter().Serialize();
}
void PrintPlottableResults(const std::vector<std::string>& desired_graphs) {
GetPlottableCounterPrinter().Print(desired_graphs);
}
bool WritePerfResults(const std::string& output_path) {
std::string results = GetPerfResults();
CreateDir(DirName(output_path));
FILE* output = fopen(output_path.c_str(), "wb");
if (output == nullptr) {
printf("Failed to write to %s.\n", output_path.c_str());
return false;
}
size_t written =
fwrite(results.c_str(), sizeof(char), results.size(), output);
fclose(output);
if (written != results.size()) {
long expected = results.size();
printf("Wrote %zu, tried to write %lu\n", written, expected);
return false;
}
return true;
}
void PrintResult(absl::string_view measurement,
absl::string_view modifier,
absl::string_view trace,
const double value,
absl::string_view units,
bool important,
ImproveDirection improve_direction) {
StringBuilder graph_name;
graph_name << measurement << modifier;
RTC_CHECK(std::isfinite(value))
<< "Expected finite value for graph " << graph_name.str()
<< ", trace name " << trace << ", units " << units << ", got " << value;
GetPerfWriter().LogResult(graph_name.str(), trace, value, units, important,
improve_direction);
GetResultsLinePrinter().PrintResult(graph_name.str(), trace, value, units,
important, improve_direction);
}
void PrintResult(absl::string_view measurement,
absl::string_view modifier,
absl::string_view trace,
const SamplesStatsCounter& counter,
absl::string_view units,
const bool important,
ImproveDirection improve_direction) {
StringBuilder graph_name;
graph_name << measurement << modifier;
GetPlottableCounterPrinter().AddCounter(graph_name.str(), trace, counter,
units);
double mean = counter.IsEmpty() ? 0 : counter.GetAverage();
double error = counter.IsEmpty() ? 0 : counter.GetStandardDeviation();
std::vector<SamplesStatsCounter::StatsSample> timed_samples =
GetSortedSamples(counter);
std::vector<double> samples(timed_samples.size());
for (size_t i = 0; i < timed_samples.size(); ++i) {
samples[i] = timed_samples[i].value;
}
// If we have an empty counter, default it to 0.
if (samples.empty()) {
samples.push_back(0);
}
GetPerfWriter().LogResultList(graph_name.str(), trace, samples, units,
important, improve_direction);
GetResultsLinePrinter().PrintResultMeanAndError(graph_name.str(), trace, mean,
error, units, important,
improve_direction);
}
void PrintResultMeanAndError(absl::string_view measurement,
absl::string_view modifier,
absl::string_view trace,
const double mean,
const double error,
absl::string_view units,
bool important,
ImproveDirection improve_direction) {
RTC_CHECK(std::isfinite(mean));
RTC_CHECK(std::isfinite(error));
StringBuilder graph_name;
graph_name << measurement << modifier;
GetPerfWriter().LogResultMeanAndError(graph_name.str(), trace, mean, error,
units, important, improve_direction);
GetResultsLinePrinter().PrintResultMeanAndError(graph_name.str(), trace, mean,
error, units, important,
improve_direction);
}
void PrintResultList(absl::string_view measurement,
absl::string_view modifier,
absl::string_view trace,
const ArrayView<const double> values,
absl::string_view units,
bool important,
ImproveDirection improve_direction) {
for (double v : values) {
RTC_CHECK(std::isfinite(v));
}
StringBuilder graph_name;
graph_name << measurement << modifier;
GetPerfWriter().LogResultList(graph_name.str(), trace, values, units,
important, improve_direction);
GetResultsLinePrinter().PrintResultList(graph_name.str(), trace, values,
units, important, improve_direction);
}
} // namespace test
} // namespace webrtc
|