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-10-03 13:53:44 +02:00
|
|
|
#include <cmath>
|
2018-01-05 15:34:09 +01:00
|
|
|
#include <fstream>
|
2017-11-30 11:43:42 +01:00
|
|
|
#include <map>
|
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
|
|
|
|
2018-10-03 13:53:44 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/critical_section.h"
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2012-11-20 00:20:20 +00:00
|
|
|
namespace {
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2017-11-30 11:43:42 +01:00
|
|
|
class PerfResultsLogger {
|
|
|
|
|
public:
|
2018-01-25 15:17:55 +01:00
|
|
|
PerfResultsLogger() : crit_(), output_(stdout), graphs_() {}
|
2017-11-30 11:43:42 +01:00
|
|
|
void ClearResults() {
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
graphs_.clear();
|
|
|
|
|
}
|
2018-01-25 15:17:55 +01:00
|
|
|
void SetOutput(FILE* output) {
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
output_ = output;
|
|
|
|
|
}
|
2019-09-12 20:30:54 +02:00
|
|
|
void LogResult(const std::string& graph_name,
|
|
|
|
|
const std::string& trace_name,
|
|
|
|
|
const webrtc::SamplesStatsCounter& counter,
|
|
|
|
|
const std::string& units,
|
|
|
|
|
const bool important) {
|
|
|
|
|
LogResultMeanAndError(
|
|
|
|
|
graph_name, trace_name, counter.IsEmpty() ? 0 : counter.GetAverage(),
|
|
|
|
|
counter.IsEmpty() ? 0 : counter.GetStandardDeviation(), units,
|
|
|
|
|
important);
|
|
|
|
|
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
plottable_counters_.push_back({graph_name, trace_name, counter, units});
|
|
|
|
|
}
|
2017-11-30 11:43:42 +01:00
|
|
|
void LogResult(const std::string& graph_name,
|
|
|
|
|
const std::string& trace_name,
|
|
|
|
|
const double value,
|
|
|
|
|
const std::string& units,
|
|
|
|
|
const bool important) {
|
2019-03-14 11:20:30 +01:00
|
|
|
RTC_CHECK(std::isfinite(value))
|
|
|
|
|
<< "Expected finite value for graph " << graph_name << ", trace name "
|
|
|
|
|
<< trace_name << ", units " << units << ", got " << value;
|
2018-10-03 13:53:44 +02:00
|
|
|
|
2017-11-30 11:43:42 +01:00
|
|
|
std::ostringstream value_stream;
|
2017-12-04 13:21:01 +01:00
|
|
|
value_stream.precision(8);
|
2017-11-30 11:43:42 +01:00
|
|
|
value_stream << value;
|
2018-01-25 15:17:55 +01:00
|
|
|
LogResultsImpl(graph_name, trace_name, value_stream.str(), units,
|
|
|
|
|
important);
|
2017-11-30 11:43:42 +01:00
|
|
|
|
|
|
|
|
std::ostringstream json_stream;
|
|
|
|
|
json_stream << '"' << trace_name << R"(":{)";
|
|
|
|
|
json_stream << R"("type":"scalar",)";
|
|
|
|
|
json_stream << R"("value":)" << value << ',';
|
|
|
|
|
json_stream << R"("units":")" << units << R"("})";
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
graphs_[graph_name].push_back(json_stream.str());
|
|
|
|
|
}
|
|
|
|
|
void LogResultMeanAndError(const std::string& graph_name,
|
|
|
|
|
const std::string& trace_name,
|
|
|
|
|
const double mean,
|
|
|
|
|
const double error,
|
|
|
|
|
const std::string& units,
|
|
|
|
|
const bool important) {
|
2018-10-03 13:53:44 +02:00
|
|
|
RTC_CHECK(std::isfinite(mean));
|
|
|
|
|
RTC_CHECK(std::isfinite(error));
|
|
|
|
|
|
2017-11-30 11:43:42 +01:00
|
|
|
std::ostringstream value_stream;
|
2017-12-04 13:21:01 +01:00
|
|
|
value_stream.precision(8);
|
2017-11-30 11:43:42 +01:00
|
|
|
value_stream << '{' << mean << ',' << error << '}';
|
2018-01-25 15:17:55 +01:00
|
|
|
LogResultsImpl(graph_name, trace_name, value_stream.str(), units,
|
|
|
|
|
important);
|
2017-11-30 11:43:42 +01:00
|
|
|
|
|
|
|
|
std::ostringstream json_stream;
|
|
|
|
|
json_stream << '"' << trace_name << R"(":{)";
|
2018-01-05 15:06:59 +01:00
|
|
|
json_stream << R"("type":"list_of_scalar_values",)";
|
2017-11-30 11:43:42 +01:00
|
|
|
json_stream << R"("values":[)" << mean << "],";
|
|
|
|
|
json_stream << R"("std":)" << error << ',';
|
|
|
|
|
json_stream << R"("units":")" << units << R"("})";
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
graphs_[graph_name].push_back(json_stream.str());
|
|
|
|
|
}
|
|
|
|
|
void LogResultList(const std::string& graph_name,
|
|
|
|
|
const std::string& trace_name,
|
|
|
|
|
const rtc::ArrayView<const double> values,
|
|
|
|
|
const std::string& units,
|
|
|
|
|
const bool important) {
|
2018-10-03 13:53:44 +02:00
|
|
|
for (double v : values) {
|
|
|
|
|
RTC_CHECK(std::isfinite(v));
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 11:43:42 +01:00
|
|
|
std::ostringstream value_stream;
|
2017-12-04 13:21:01 +01:00
|
|
|
value_stream.precision(8);
|
2017-11-30 11:43:42 +01:00
|
|
|
value_stream << '[';
|
|
|
|
|
OutputListToStream(&value_stream, values);
|
|
|
|
|
value_stream << ']';
|
2018-01-25 15:17:55 +01:00
|
|
|
LogResultsImpl(graph_name, trace_name, value_stream.str(), units,
|
|
|
|
|
important);
|
2017-11-30 11:43:42 +01:00
|
|
|
|
|
|
|
|
std::ostringstream json_stream;
|
|
|
|
|
json_stream << '"' << trace_name << R"(":{)";
|
2018-01-05 15:06:59 +01:00
|
|
|
json_stream << R"("type":"list_of_scalar_values",)";
|
2017-11-30 11:43:42 +01:00
|
|
|
json_stream << R"("values":)" << value_stream.str() << ',';
|
|
|
|
|
json_stream << R"("units":")" << units << R"("})";
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
graphs_[graph_name].push_back(json_stream.str());
|
|
|
|
|
}
|
|
|
|
|
std::string ToJSON() const;
|
2019-09-12 20:30:54 +02:00
|
|
|
void PrintPlottableCounters(
|
|
|
|
|
const std::vector<std::string>& desired_graphs_raw) const {
|
|
|
|
|
std::set<std::string> desired_graphs(desired_graphs_raw.begin(),
|
|
|
|
|
desired_graphs_raw.end());
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-30 11:43:42 +01:00
|
|
|
|
|
|
|
|
private:
|
2018-01-25 15:17:55 +01:00
|
|
|
void LogResultsImpl(const std::string& graph_name,
|
|
|
|
|
const std::string& trace,
|
|
|
|
|
const std::string& values,
|
|
|
|
|
const std::string& units,
|
|
|
|
|
bool important) {
|
|
|
|
|
// <*>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>
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
|
|
|
|
|
if (important) {
|
|
|
|
|
fprintf(output_, "*");
|
|
|
|
|
}
|
|
|
|
|
fprintf(output_, "RESULT %s: %s= %s %s\n", graph_name.c_str(),
|
|
|
|
|
trace.c_str(), values.c_str(), units.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 11:43:42 +01:00
|
|
|
rtc::CriticalSection crit_;
|
2018-01-25 15:17:55 +01:00
|
|
|
FILE* output_ RTC_GUARDED_BY(&crit_);
|
2017-11-30 11:43:42 +01:00
|
|
|
std::map<std::string, std::vector<std::string>> graphs_
|
|
|
|
|
RTC_GUARDED_BY(&crit_);
|
2019-09-12 20:30:54 +02:00
|
|
|
std::vector<PlottableCounter> plottable_counters_ RTC_GUARDED_BY(&crit_);
|
2017-11-30 11:43:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::string PerfResultsLogger::ToJSON() const {
|
|
|
|
|
std::ostringstream json_stream;
|
|
|
|
|
json_stream << R"({"format_version":"1.0",)";
|
|
|
|
|
json_stream << R"("charts":{)";
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
for (auto graphs_it = graphs_.begin(); graphs_it != graphs_.end();
|
|
|
|
|
++graphs_it) {
|
|
|
|
|
if (graphs_it != graphs_.begin())
|
|
|
|
|
json_stream << ',';
|
|
|
|
|
json_stream << '"' << graphs_it->first << "\":";
|
|
|
|
|
json_stream << '{';
|
|
|
|
|
OutputListToStream(&json_stream, graphs_it->second);
|
|
|
|
|
json_stream << '}';
|
|
|
|
|
}
|
|
|
|
|
json_stream << "}}";
|
|
|
|
|
return json_stream.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PerfResultsLogger& GetPerfResultsLogger() {
|
|
|
|
|
static PerfResultsLogger* const logger_ = new PerfResultsLogger();
|
|
|
|
|
return *logger_;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-20 00:20:20 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
2017-11-30 11:43:42 +01:00
|
|
|
void ClearPerfResults() {
|
|
|
|
|
GetPerfResultsLogger().ClearResults();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-25 15:17:55 +01:00
|
|
|
void SetPerfResultsOutput(FILE* output) {
|
|
|
|
|
GetPerfResultsLogger().SetOutput(output);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 11:43:42 +01:00
|
|
|
std::string GetPerfResultsJSON() {
|
|
|
|
|
return GetPerfResultsLogger().ToJSON();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 20:30:54 +02:00
|
|
|
void PrintPlottableResults(const std::vector<std::string>& desired_graphs) {
|
|
|
|
|
GetPerfResultsLogger().PrintPlottableCounters(desired_graphs);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 15:34:09 +01:00
|
|
|
void WritePerfResults(const std::string& output_path) {
|
|
|
|
|
std::string json_results = GetPerfResultsJSON();
|
|
|
|
|
std::fstream json_file(output_path, std::fstream::out);
|
|
|
|
|
json_file << json_results;
|
|
|
|
|
json_file.close();
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-20 00:20:20 +00:00
|
|
|
void PrintResult(const std::string& measurement,
|
|
|
|
|
const std::string& modifier,
|
|
|
|
|
const std::string& trace,
|
2017-11-22 19:17:50 +01:00
|
|
|
const double value,
|
2012-11-20 00:20:20 +00:00
|
|
|
const std::string& units,
|
|
|
|
|
bool important) {
|
2017-11-30 11:43:42 +01:00
|
|
|
GetPerfResultsLogger().LogResult(measurement + modifier, trace, value, units,
|
|
|
|
|
important);
|
2019-09-12 20:30:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PrintResult(const std::string& measurement,
|
|
|
|
|
const std::string& modifier,
|
|
|
|
|
const std::string& trace,
|
|
|
|
|
const SamplesStatsCounter& counter,
|
|
|
|
|
const std::string& units,
|
|
|
|
|
const bool important) {
|
|
|
|
|
GetPerfResultsLogger().LogResult(measurement + modifier, trace, counter,
|
|
|
|
|
units, important);
|
2012-11-20 00:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PrintResultMeanAndError(const std::string& measurement,
|
|
|
|
|
const std::string& modifier,
|
|
|
|
|
const std::string& trace,
|
2017-11-23 14:06:04 +01:00
|
|
|
const double mean,
|
|
|
|
|
const double error,
|
2012-11-20 00:20:20 +00:00
|
|
|
const std::string& units,
|
|
|
|
|
bool important) {
|
2017-11-30 11:43:42 +01:00
|
|
|
GetPerfResultsLogger().LogResultMeanAndError(measurement + modifier, trace,
|
|
|
|
|
mean, error, units, important);
|
2012-11-20 00:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PrintResultList(const std::string& measurement,
|
|
|
|
|
const std::string& modifier,
|
|
|
|
|
const std::string& trace,
|
2017-11-30 11:43:42 +01:00
|
|
|
const rtc::ArrayView<const double> values,
|
2012-11-20 00:20:20 +00:00
|
|
|
const std::string& units,
|
|
|
|
|
bool important) {
|
2017-11-30 11:43:42 +01:00
|
|
|
GetPerfResultsLogger().LogResultList(measurement + modifier, trace, values,
|
|
|
|
|
units, important);
|
2012-11-20 00:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace webrtc
|