aboutsummaryrefslogtreecommitdiff
path: root/test/testsupport/perf_test_graphjson_writer.cc
blob: cf49b29320cec4c8fffeeed3456c1fff9c332635 (plain)
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
/*
 *  Copyright (c) 2020 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_graphjson_writer.h"

#include <fstream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

#include "rtc_base/checks.h"
#include "rtc_base/critical_section.h"

namespace webrtc {
namespace test {

std::string UnitWithDirection(
    const std::string& units,
    webrtc::test::ImproveDirection improve_direction) {
  switch (improve_direction) {
    case webrtc::test::ImproveDirection::kNone:
      return units;
    case webrtc::test::ImproveDirection::kSmallerIsBetter:
      return units + "_smallerIsBetter";
    case webrtc::test::ImproveDirection::kBiggerIsBetter:
      return units + "_biggerIsBetter";
  }
}

template <typename Container>
void OutputListToStream(std::ostream* ostream, const Container& values) {
  const char* sep = "";
  for (const auto& v : values) {
    (*ostream) << sep << v;
    sep = ",";
  }
}

namespace {

class PerfTestGraphJsonWriter : public PerfTestResultWriter {
 public:
  PerfTestGraphJsonWriter() : crit_(), graphs_() {}
  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,
                 webrtc::test::ImproveDirection improve_direction) {
    std::ostringstream json_stream;
    json_stream << '"' << trace_name << R"(":{)";
    json_stream << R"("type":"scalar",)";
    json_stream << R"("value":)" << value << ',';
    json_stream << R"("units":")" << UnitWithDirection(units, improve_direction)
                << 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,
                             webrtc::test::ImproveDirection improve_direction) {
    std::ostringstream json_stream;
    json_stream << '"' << trace_name << R"(":{)";
    json_stream << R"("type":"list_of_scalar_values",)";
    json_stream << R"("values":[)" << mean << "],";
    json_stream << R"("std":)" << error << ',';
    json_stream << R"("units":")" << UnitWithDirection(units, improve_direction)
                << 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,
                     webrtc::test::ImproveDirection improve_direction) {
    std::ostringstream value_stream;
    value_stream.precision(8);
    value_stream << '[';
    OutputListToStream(&value_stream, values);
    value_stream << ']';

    std::ostringstream json_stream;
    json_stream << '"' << trace_name << R"(":{)";
    json_stream << R"("type":"list_of_scalar_values",)";
    json_stream << R"("values":)" << value_stream.str() << ',';
    json_stream << R"("units":")" << UnitWithDirection(units, improve_direction)
                << R"("})";
    rtc::CritScope lock(&crit_);
    graphs_[graph_name].push_back(json_stream.str());
  }

  std::string Serialize() 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();
  }

 private:
  rtc::CriticalSection crit_;
  std::map<std::string, std::vector<std::string>> graphs_
      RTC_GUARDED_BY(&crit_);
};

}  // namespace

PerfTestResultWriter* CreateGraphJsonWriter() {
  return new PerfTestGraphJsonWriter();
}

}  // namespace test
}  // namespace webrtc