2012-11-20 00:20:20 +00:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "test/testsupport/perf_test.h"
|
2012-11-20 00:20:20 +00:00
|
|
|
|
2013-05-27 08:02:22 +00:00
|
|
|
#include <stdio.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2018-01-05 15:34:09 +01:00
|
|
|
#include <fstream>
|
2019-09-12 20:30:54 +02:00
|
|
|
#include <set>
|
2017-11-30 11:43:42 +01:00
|
|
|
#include <sstream>
|
2017-11-24 13:40:01 +01:00
|
|
|
#include <vector>
|
2012-11-20 00:20:20 +00:00
|
|
|
|
2021-02-11 12:54:04 +01:00
|
|
|
#include "absl/strings/string_view.h"
|
2021-03-20 12:27:43 +01:00
|
|
|
#include "api/numerics/samples_stats_counter.h"
|
2018-10-03 13:53:44 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2021-02-11 12:54:04 +01:00
|
|
|
#include "rtc_base/strings/string_builder.h"
|
2020-07-08 16:09:21 +02:00
|
|
|
#include "rtc_base/synchronization/mutex.h"
|
2020-09-29 09:26:02 +02:00
|
|
|
#include "test/testsupport/file_utils.h"
|
2020-01-13 15:10:40 +01:00
|
|
|
#include "test/testsupport/perf_test_histogram_writer.h"
|
|
|
|
|
|
2020-01-10 11:11:40 +01:00
|
|
|
namespace webrtc {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
2012-11-20 00:20:20 +00:00
|
|
|
namespace {
|
|
|
|
|
|
2020-03-27 19:22:36 +01:00
|
|
|
std::string UnitWithDirection(
|
2021-02-11 12:54:04 +01:00
|
|
|
absl::string_view units,
|
2020-03-27 19:22:36 +01:00
|
|
|
webrtc::test::ImproveDirection improve_direction) {
|
|
|
|
|
switch (improve_direction) {
|
|
|
|
|
case webrtc::test::ImproveDirection::kNone:
|
2021-02-11 12:54:04 +01:00
|
|
|
return std::string(units);
|
2020-03-27 19:22:36 +01:00
|
|
|
case webrtc::test::ImproveDirection::kSmallerIsBetter:
|
2021-02-11 12:54:04 +01:00
|
|
|
return std::string(units) + "_smallerIsBetter";
|
2020-03-27 19:22:36 +01:00
|
|
|
case webrtc::test::ImproveDirection::kBiggerIsBetter:
|
2021-02-11 12:54:04 +01:00
|
|
|
return std::string(units) + "_biggerIsBetter";
|
2020-03-27 19:22:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-20 01:31:37 +01:00
|
|
|
std::vector<SamplesStatsCounter::StatsSample> GetSortedSamples(
|
|
|
|
|
const SamplesStatsCounter& counter) {
|
|
|
|
|
rtc::ArrayView<const SamplesStatsCounter::StatsSample> view =
|
|
|
|
|
counter.GetTimedSamples();
|
|
|
|
|
std::vector<SamplesStatsCounter::StatsSample> out(view.begin(), view.end());
|
|
|
|
|
std::sort(out.begin(), out.end(),
|
|
|
|
|
[](const SamplesStatsCounter::StatsSample& a,
|
|
|
|
|
const SamplesStatsCounter::StatsSample& b) {
|
|
|
|
|
return a.time < b.time;
|
|
|
|
|
});
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 11:43:42 +01:00
|
|
|
template <typename Container>
|
|
|
|
|
void OutputListToStream(std::ostream* ostream, const Container& values) {
|
|
|
|
|
const char* sep = "";
|
|
|
|
|
for (const auto& v : values) {
|
|
|
|
|
(*ostream) << sep << v;
|
|
|
|
|
sep = ",";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 20:30:54 +02:00
|
|
|
struct PlottableCounter {
|
|
|
|
|
std::string graph_name;
|
|
|
|
|
std::string trace_name;
|
|
|
|
|
webrtc::SamplesStatsCounter counter;
|
|
|
|
|
std::string units;
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-09 15:38:50 +01:00
|
|
|
class PlottableCounterPrinter {
|
|
|
|
|
public:
|
|
|
|
|
PlottableCounterPrinter() : output_(stdout) {}
|
|
|
|
|
|
|
|
|
|
void SetOutput(FILE* output) {
|
2020-07-08 16:09:21 +02:00
|
|
|
MutexLock lock(&mutex_);
|
2020-01-09 15:38:50 +01:00
|
|
|
output_ = output;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 12:54:04 +01:00
|
|
|
void AddCounter(absl::string_view graph_name,
|
|
|
|
|
absl::string_view trace_name,
|
2020-01-09 15:38:50 +01:00
|
|
|
const webrtc::SamplesStatsCounter& counter,
|
2021-02-11 12:54:04 +01:00
|
|
|
absl::string_view units) {
|
2020-07-08 16:09:21 +02:00
|
|
|
MutexLock lock(&mutex_);
|
2021-02-11 12:54:04 +01:00
|
|
|
plottable_counters_.push_back({std::string(graph_name),
|
|
|
|
|
std::string(trace_name), counter,
|
|
|
|
|
std::string(units)});
|
2020-01-09 15:38:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
2020-07-08 16:09:21 +02:00
|
|
|
MutexLock lock(&mutex_);
|
2020-01-09 15:38:50 +01:00
|
|
|
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:
|
2020-07-08 16:09:21 +02:00
|
|
|
mutable Mutex mutex_;
|
|
|
|
|
std::vector<PlottableCounter> plottable_counters_ RTC_GUARDED_BY(&mutex_);
|
|
|
|
|
FILE* output_ RTC_GUARDED_BY(&mutex_);
|
2020-01-09 15:38:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PlottableCounterPrinter& GetPlottableCounterPrinter() {
|
|
|
|
|
static PlottableCounterPrinter* printer_ = new PlottableCounterPrinter();
|
|
|
|
|
return *printer_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 11:11:40 +01:00
|
|
|
class ResultsLinePrinter {
|
|
|
|
|
public:
|
|
|
|
|
ResultsLinePrinter() : output_(stdout) {}
|
|
|
|
|
|
|
|
|
|
void SetOutput(FILE* output) {
|
2020-07-08 16:09:21 +02:00
|
|
|
MutexLock lock(&mutex_);
|
2020-01-10 11:11:40 +01:00
|
|
|
output_ = output;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 12:54:04 +01:00
|
|
|
void PrintResult(absl::string_view graph_name,
|
|
|
|
|
absl::string_view trace_name,
|
2020-01-10 11:11:40 +01:00
|
|
|
const double value,
|
2021-02-11 12:54:04 +01:00
|
|
|
absl::string_view units,
|
2020-01-10 11:11:40 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 12:54:04 +01:00
|
|
|
void PrintResultMeanAndError(absl::string_view graph_name,
|
|
|
|
|
absl::string_view trace_name,
|
2020-01-10 11:11:40 +01:00
|
|
|
const double mean,
|
|
|
|
|
const double error,
|
2021-02-11 12:54:04 +01:00
|
|
|
absl::string_view units,
|
2020-01-10 11:11:40 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 12:54:04 +01:00
|
|
|
void PrintResultList(absl::string_view graph_name,
|
|
|
|
|
absl::string_view trace_name,
|
2020-01-10 11:11:40 +01:00
|
|
|
const rtc::ArrayView<const double> values,
|
2021-02-11 12:54:04 +01:00
|
|
|
absl::string_view units,
|
2020-01-10 11:11:40 +01:00
|
|
|
const bool important,
|
|
|
|
|
webrtc::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:
|
2021-02-11 12:54:04 +01:00
|
|
|
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,
|
2020-01-10 11:11:40 +01:00
|
|
|
bool important) {
|
2020-07-08 16:09:21 +02:00
|
|
|
MutexLock lock(&mutex_);
|
2021-02-11 12:54:04 +01:00
|
|
|
rtc::StringBuilder message;
|
|
|
|
|
message << (important ? "*" : "") << "RESULT " << graph_name << ": "
|
|
|
|
|
<< trace_name << "= " << prefix << values << suffix << " " << units;
|
2020-01-10 11:11:40 +01:00
|
|
|
// <*>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>
|
2021-02-11 12:54:04 +01:00
|
|
|
fprintf(output_, "%s\n", message.str().c_str());
|
2020-01-10 11:11:40 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-08 16:09:21 +02:00
|
|
|
Mutex mutex_;
|
|
|
|
|
FILE* output_ RTC_GUARDED_BY(&mutex_);
|
2020-01-10 11:11:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ResultsLinePrinter& GetResultsLinePrinter() {
|
|
|
|
|
static ResultsLinePrinter* const printer_ = new ResultsLinePrinter();
|
|
|
|
|
return *printer_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 15:10:40 +01:00
|
|
|
PerfTestResultWriter& GetPerfWriter() {
|
2020-03-27 19:22:36 +01:00
|
|
|
static PerfTestResultWriter* writer = CreateHistogramWriter();
|
|
|
|
|
return *writer;
|
2017-11-30 11:43:42 +01:00
|
|
|
}
|
|
|
|
|
|
2012-11-20 00:20:20 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
2017-11-30 11:43:42 +01:00
|
|
|
void ClearPerfResults() {
|
2020-01-13 15:10:40 +01:00
|
|
|
GetPerfWriter().ClearResults();
|
2017-11-30 11:43:42 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-25 15:17:55 +01:00
|
|
|
void SetPerfResultsOutput(FILE* output) {
|
2020-01-09 15:38:50 +01:00
|
|
|
GetPlottableCounterPrinter().SetOutput(output);
|
2020-01-10 11:11:40 +01:00
|
|
|
GetResultsLinePrinter().SetOutput(output);
|
2018-01-25 15:17:55 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-31 11:22:42 +01:00
|
|
|
std::string GetPerfResults() {
|
|
|
|
|
return GetPerfWriter().Serialize();
|
2017-11-30 11:43:42 +01:00
|
|
|
}
|
|
|
|
|
|
2019-09-12 20:30:54 +02:00
|
|
|
void PrintPlottableResults(const std::vector<std::string>& desired_graphs) {
|
2020-01-09 15:38:50 +01:00
|
|
|
GetPlottableCounterPrinter().Print(desired_graphs);
|
2019-09-12 20:30:54 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-12 09:39:40 +01:00
|
|
|
bool WritePerfResults(const std::string& output_path) {
|
2020-01-31 11:22:42 +01:00
|
|
|
std::string results = GetPerfResults();
|
2020-09-29 09:26:02 +02:00
|
|
|
CreateDir(DirName(output_path));
|
2021-12-07 09:54:34 +01:00
|
|
|
FILE* output = fopen(output_path.c_str(), "ab");
|
2020-03-12 09:39:40 +01:00
|
|
|
if (output == NULL) {
|
|
|
|
|
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;
|
2018-01-05 15:34:09 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-11 12:54:04 +01:00
|
|
|
void PrintResult(absl::string_view measurement,
|
|
|
|
|
absl::string_view modifier,
|
|
|
|
|
absl::string_view trace,
|
2017-11-22 19:17:50 +01:00
|
|
|
const double value,
|
2021-02-11 12:54:04 +01:00
|
|
|
absl::string_view units,
|
2019-09-23 17:55:52 +02:00
|
|
|
bool important,
|
|
|
|
|
ImproveDirection improve_direction) {
|
2021-02-11 12:54:04 +01:00
|
|
|
rtc::StringBuilder graph_name;
|
|
|
|
|
graph_name << measurement << modifier;
|
2020-01-10 11:11:40 +01:00
|
|
|
RTC_CHECK(std::isfinite(value))
|
2021-02-11 12:54:04 +01:00
|
|
|
<< "Expected finite value for graph " << graph_name.str()
|
|
|
|
|
<< ", trace name " << trace << ", units " << units << ", got " << value;
|
|
|
|
|
GetPerfWriter().LogResult(graph_name.str(), trace, value, units, important,
|
2020-01-13 15:10:40 +01:00
|
|
|
improve_direction);
|
2021-02-11 12:54:04 +01:00
|
|
|
GetResultsLinePrinter().PrintResult(graph_name.str(), trace, value, units,
|
2020-01-10 11:11:40 +01:00
|
|
|
important, improve_direction);
|
2019-09-12 20:30:54 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-11 12:54:04 +01:00
|
|
|
void PrintResult(absl::string_view measurement,
|
|
|
|
|
absl::string_view modifier,
|
|
|
|
|
absl::string_view trace,
|
2019-09-12 20:30:54 +02:00
|
|
|
const SamplesStatsCounter& counter,
|
2021-02-11 12:54:04 +01:00
|
|
|
absl::string_view units,
|
2019-09-23 17:55:52 +02:00
|
|
|
const bool important,
|
|
|
|
|
ImproveDirection improve_direction) {
|
2021-02-11 12:54:04 +01:00
|
|
|
rtc::StringBuilder graph_name;
|
|
|
|
|
graph_name << measurement << modifier;
|
|
|
|
|
GetPlottableCounterPrinter().AddCounter(graph_name.str(), trace, counter,
|
|
|
|
|
units);
|
2020-01-10 11:11:40 +01:00
|
|
|
|
|
|
|
|
double mean = counter.IsEmpty() ? 0 : counter.GetAverage();
|
|
|
|
|
double error = counter.IsEmpty() ? 0 : counter.GetStandardDeviation();
|
2021-02-20 01:31:37 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2021-03-20 12:27:43 +01:00
|
|
|
// If we have an empty counter, default it to 0.
|
|
|
|
|
if (samples.empty()) {
|
|
|
|
|
samples.push_back(0);
|
|
|
|
|
}
|
2021-02-20 01:31:37 +01:00
|
|
|
|
|
|
|
|
GetPerfWriter().LogResultList(graph_name.str(), trace, samples, units,
|
|
|
|
|
important, improve_direction);
|
|
|
|
|
GetResultsLinePrinter().PrintResultMeanAndError(graph_name.str(), trace, mean,
|
|
|
|
|
error, units, important,
|
|
|
|
|
improve_direction);
|
2012-11-20 00:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-11 12:54:04 +01:00
|
|
|
void PrintResultMeanAndError(absl::string_view measurement,
|
|
|
|
|
absl::string_view modifier,
|
|
|
|
|
absl::string_view trace,
|
2017-11-23 14:06:04 +01:00
|
|
|
const double mean,
|
|
|
|
|
const double error,
|
2021-02-11 12:54:04 +01:00
|
|
|
absl::string_view units,
|
2019-09-23 17:55:52 +02:00
|
|
|
bool important,
|
|
|
|
|
ImproveDirection improve_direction) {
|
2020-01-10 11:11:40 +01:00
|
|
|
RTC_CHECK(std::isfinite(mean));
|
|
|
|
|
RTC_CHECK(std::isfinite(error));
|
|
|
|
|
|
2021-02-11 12:54:04 +01:00
|
|
|
rtc::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);
|
2012-11-20 00:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-11 12:54:04 +01:00
|
|
|
void PrintResultList(absl::string_view measurement,
|
|
|
|
|
absl::string_view modifier,
|
|
|
|
|
absl::string_view trace,
|
2017-11-30 11:43:42 +01:00
|
|
|
const rtc::ArrayView<const double> values,
|
2021-02-11 12:54:04 +01:00
|
|
|
absl::string_view units,
|
2019-09-23 17:55:52 +02:00
|
|
|
bool important,
|
|
|
|
|
ImproveDirection improve_direction) {
|
2020-01-10 11:11:40 +01:00
|
|
|
for (double v : values) {
|
|
|
|
|
RTC_CHECK(std::isfinite(v));
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 12:54:04 +01:00
|
|
|
rtc::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);
|
2012-11-20 00:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace webrtc
|