Add a function to report perf results in JSON format.
Add support to report perf results in the JSON format specified in [1]. [1] https://github.com/catapult-project/catapult/blob/master/dashboard/docs/data-format.md Bug: webrtc:8566 Change-Id: I25f829a4b012b3e2a3d56d61582a674f780148d0 Reviewed-on: https://webrtc-review.googlesource.com/26031 Reviewed-by: Patrik Höglund <phoglund@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Commit-Queue: Edward Lemur <ehmaldonado@webrtc.org> Cr-Commit-Position: refs/heads/master@{#20950}
This commit is contained in:
@ -8,14 +8,12 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
// A stripped-down version of Chromium's chrome/test/perf/perf_test.cc.
|
||||
// ResultsToString(), PrintResult(size_t value) and AppendResult(size_t value)
|
||||
// have been modified. The remainder are identical to the Chromium version.
|
||||
|
||||
#include "test/testsupport/perf_test.h"
|
||||
#include "rtc_base/criticalsection.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <stdio.h>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
@ -36,21 +34,131 @@ void PrintResultsImpl(const std::string& graph_name,
|
||||
values.c_str(), units.c_str());
|
||||
}
|
||||
|
||||
template <typename Container>
|
||||
void OutputListToStream(std::ostream* ostream, const Container& values) {
|
||||
const char* sep = "";
|
||||
for (const auto& v : values) {
|
||||
(*ostream) << sep << v;
|
||||
sep = ",";
|
||||
}
|
||||
}
|
||||
|
||||
class PerfResultsLogger {
|
||||
public:
|
||||
void ClearResults() {
|
||||
rtc::CritScope lock(&crit_);
|
||||
graphs_.clear();
|
||||
}
|
||||
void LogResult(const std::string& graph_name,
|
||||
const std::string& trace_name,
|
||||
const double value,
|
||||
const std::string& units,
|
||||
const bool important) {
|
||||
std::ostringstream value_stream;
|
||||
value_stream << value;
|
||||
PrintResultsImpl(graph_name, trace_name, value_stream.str(), units,
|
||||
important);
|
||||
|
||||
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) {
|
||||
std::ostringstream value_stream;
|
||||
value_stream << '{' << mean << ',' << error << '}';
|
||||
PrintResultsImpl(graph_name, trace_name, value_stream.str(), units,
|
||||
important);
|
||||
|
||||
std::ostringstream json_stream;
|
||||
json_stream << '"' << trace_name << R"(":{)";
|
||||
json_stream << R"("type":"list_of_scalars",)";
|
||||
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) {
|
||||
std::ostringstream value_stream;
|
||||
value_stream << '[';
|
||||
OutputListToStream(&value_stream, values);
|
||||
value_stream << ']';
|
||||
PrintResultsImpl(graph_name, trace_name, value_stream.str(), units,
|
||||
important);
|
||||
|
||||
std::ostringstream json_stream;
|
||||
json_stream << '"' << trace_name << R"(":{)";
|
||||
json_stream << R"("type":"list_of_scalars",)";
|
||||
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;
|
||||
|
||||
private:
|
||||
rtc::CriticalSection crit_;
|
||||
std::map<std::string, std::vector<std::string>> graphs_
|
||||
RTC_GUARDED_BY(&crit_);
|
||||
};
|
||||
|
||||
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_;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace webrtc {
|
||||
namespace test {
|
||||
|
||||
void ClearPerfResults() {
|
||||
GetPerfResultsLogger().ClearResults();
|
||||
}
|
||||
|
||||
std::string GetPerfResultsJSON() {
|
||||
return GetPerfResultsLogger().ToJSON();
|
||||
}
|
||||
|
||||
void PrintResult(const std::string& measurement,
|
||||
const std::string& modifier,
|
||||
const std::string& trace,
|
||||
const double value,
|
||||
const std::string& units,
|
||||
bool important) {
|
||||
std::ostringstream value_stream;
|
||||
value_stream << value;
|
||||
PrintResultsImpl(measurement + modifier, trace, value_stream.str(), units,
|
||||
important);
|
||||
GetPerfResultsLogger().LogResult(measurement + modifier, trace, value, units,
|
||||
important);
|
||||
}
|
||||
|
||||
void PrintResultMeanAndError(const std::string& measurement,
|
||||
@ -60,32 +168,18 @@ void PrintResultMeanAndError(const std::string& measurement,
|
||||
const double error,
|
||||
const std::string& units,
|
||||
bool important) {
|
||||
std::ostringstream value_stream;
|
||||
value_stream << '{' << mean << ',' << error << '}';
|
||||
PrintResultsImpl(measurement + modifier, trace, value_stream.str(), units,
|
||||
important);
|
||||
GetPerfResultsLogger().LogResultMeanAndError(measurement + modifier, trace,
|
||||
mean, error, units, important);
|
||||
}
|
||||
|
||||
void PrintResultList(const std::string& measurement,
|
||||
const std::string& modifier,
|
||||
const std::string& trace,
|
||||
const std::vector<double>& values,
|
||||
const rtc::ArrayView<const double> values,
|
||||
const std::string& units,
|
||||
bool important) {
|
||||
std::ostringstream value_stream;
|
||||
value_stream << '[';
|
||||
if (!values.empty()) {
|
||||
auto it = values.begin();
|
||||
while (true) {
|
||||
value_stream << *it;
|
||||
if (++it == values.end())
|
||||
break;
|
||||
value_stream << ',';
|
||||
}
|
||||
}
|
||||
value_stream << ']';
|
||||
PrintResultsImpl(measurement + modifier, trace, value_stream.str(), units,
|
||||
important);
|
||||
GetPerfResultsLogger().LogResultList(measurement + modifier, trace, values,
|
||||
units, important);
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
|
||||
Reference in New Issue
Block a user