Extract plottable counter from perf results logger.

Split out of https://webrtc-review.googlesource.com/c/src/+/165389.

I disentangled the plottable counter printer from the perf result
printer so it will work for both future implementations of the perf
test JSON writers. The only thing plottable counters and the
results writer had in common was that both wrote JSON anyway.

Bug: chromium:1029452
Change-Id: I041c3096641eda42542e8d994b246eb313940b4b
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/165397
Commit-Queue: Patrik Höglund <phoglund@webrtc.org>
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30198}
This commit is contained in:
Patrik Höglund 2020-01-09 15:38:50 +01:00 committed by Commit Bot
parent b40f75e427
commit e1cbb9c20e

View file

@ -40,6 +40,69 @@ struct PlottableCounter {
std::string units; std::string units;
}; };
class PlottableCounterPrinter {
public:
PlottableCounterPrinter() : output_(stdout) {}
void SetOutput(FILE* output) {
rtc::CritScope lock(&crit_);
output_ = output;
}
void AddCounter(const std::string& graph_name,
const std::string& trace_name,
const webrtc::SamplesStatsCounter& counter,
const std::string& units) {
rtc::CritScope lock(&crit_);
plottable_counters_.push_back({graph_name, trace_name, counter, 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());
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());
}
}
private:
rtc::CriticalSection crit_;
std::vector<PlottableCounter> plottable_counters_ RTC_GUARDED_BY(&crit_);
FILE* output_ RTC_GUARDED_BY(&crit_);
};
PlottableCounterPrinter& GetPlottableCounterPrinter() {
static PlottableCounterPrinter* printer_ = new PlottableCounterPrinter();
return *printer_;
}
class PerfResultsLogger { class PerfResultsLogger {
public: public:
PerfResultsLogger() : crit_(), output_(stdout), graphs_() {} PerfResultsLogger() : crit_(), output_(stdout), graphs_() {}
@ -51,20 +114,6 @@ class PerfResultsLogger {
rtc::CritScope lock(&crit_); rtc::CritScope lock(&crit_);
output_ = output; output_ = output;
} }
void LogResult(const std::string& graph_name,
const std::string& trace_name,
const webrtc::SamplesStatsCounter& counter,
const std::string& units,
const bool important,
webrtc::test::ImproveDirection improve_direction) {
LogResultMeanAndError(
graph_name, trace_name, counter.IsEmpty() ? 0 : counter.GetAverage(),
counter.IsEmpty() ? 0 : counter.GetStandardDeviation(), units,
important, improve_direction);
rtc::CritScope lock(&crit_);
plottable_counters_.push_back({graph_name, trace_name, counter, units});
}
void LogResult(const std::string& graph_name, void LogResult(const std::string& graph_name,
const std::string& trace_name, const std::string& trace_name,
const double value, const double value,
@ -144,41 +193,6 @@ class PerfResultsLogger {
graphs_[graph_name].push_back(json_stream.str()); graphs_[graph_name].push_back(json_stream.str());
} }
std::string ToJSON() const; std::string ToJSON() const;
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());
}
}
private: private:
void LogResultsImpl(const std::string& graph_name, void LogResultsImpl(const std::string& graph_name,
@ -217,7 +231,6 @@ class PerfResultsLogger {
FILE* output_ RTC_GUARDED_BY(&crit_); FILE* output_ RTC_GUARDED_BY(&crit_);
std::map<std::string, std::vector<std::string>> graphs_ std::map<std::string, std::vector<std::string>> graphs_
RTC_GUARDED_BY(&crit_); RTC_GUARDED_BY(&crit_);
std::vector<PlottableCounter> plottable_counters_ RTC_GUARDED_BY(&crit_);
}; };
std::string PerfResultsLogger::ToJSON() const { std::string PerfResultsLogger::ToJSON() const {
@ -254,6 +267,7 @@ void ClearPerfResults() {
void SetPerfResultsOutput(FILE* output) { void SetPerfResultsOutput(FILE* output) {
GetPerfResultsLogger().SetOutput(output); GetPerfResultsLogger().SetOutput(output);
GetPlottableCounterPrinter().SetOutput(output);
} }
std::string GetPerfResultsJSON() { std::string GetPerfResultsJSON() {
@ -261,7 +275,7 @@ std::string GetPerfResultsJSON() {
} }
void PrintPlottableResults(const std::vector<std::string>& desired_graphs) { void PrintPlottableResults(const std::vector<std::string>& desired_graphs) {
GetPerfResultsLogger().PrintPlottableCounters(desired_graphs); GetPlottableCounterPrinter().Print(desired_graphs);
} }
void WritePerfResults(const std::string& output_path) { void WritePerfResults(const std::string& output_path) {
@ -289,8 +303,13 @@ void PrintResult(const std::string& measurement,
const std::string& units, const std::string& units,
const bool important, const bool important,
ImproveDirection improve_direction) { ImproveDirection improve_direction) {
GetPerfResultsLogger().LogResult(measurement + modifier, trace, counter, PrintResultMeanAndError(
units, important, improve_direction); measurement, modifier, trace,
counter.IsEmpty() ? 0 : counter.GetAverage(),
counter.IsEmpty() ? 0 : counter.GetStandardDeviation(), units, important,
improve_direction);
GetPlottableCounterPrinter().AddCounter(measurement + modifier, trace,
counter, units);
} }
void PrintResultMeanAndError(const std::string& measurement, void PrintResultMeanAndError(const std::string& measurement,