-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_serializer.cc
148 lines (129 loc) · 4.45 KB
/
json_serializer.cc
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
145
146
147
148
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ml_benchmark/json_serializer.h"
#include <base/json/json_writer.h>
#include <base/logging.h>
#include <base/values.h>
#include <brillo/file_utils.h>
#include <string>
#include <utility>
using chrome::ml_benchmark::BenchmarkResults;
using chrome::ml_benchmark::Metric;
namespace {
// Maps to |tast/common/perf/perf.go| |supportedUnits|.
base::Optional<std::string> metric_units(const Metric::Units u) {
switch (u) {
case Metric::UNITLESS:
return "unitless";
case Metric::BYTES:
return "bytes";
case Metric::JOULES:
return "J";
case Metric::WATTS:
return "W";
case Metric::COUNT:
return "count";
case Metric::MS:
return "ms";
case Metric::NPERCENT:
return "n%";
case Metric::SIGMA:
return "sigma";
case Metric::TS_MS:
return "tsMs";
default:
LOG(ERROR) << "Unhandled unit: " << u;
return base::nullopt;
}
}
// Maps to |mlbenchmark/scenario.go| |ImprovementDirection|.
base::Optional<std::string> metric_direction(const Metric::Direction d) {
switch (d) {
case Metric::SMALLER_IS_BETTER:
return "smaller_is_better";
case Metric::BIGGER_IS_BETTER:
return "bigger_is_better";
default:
LOG(ERROR) << "Unhandled direction: " << d;
return base::nullopt;
}
}
// Maps to |mlbenchmark/scenario.go| |Cardinality|.
base::Optional<std::string> metric_cardinality(const Metric::Cardinality c) {
switch (c) {
case Metric::SINGLE:
return "single";
case Metric::MULTIPLE:
return "multiple";
default:
LOG(ERROR) << "Unhandled cardinality: " << c;
return base::nullopt;
}
}
} // namespace
namespace ml_benchmark {
base::Optional<base::Value> BenchmarkResultsToJson(
const BenchmarkResults& results) {
base::Value doc(base::Value::Type::DICTIONARY);
doc.SetKey("status", base::Value(results.status()));
doc.SetKey("results_message", base::Value(results.results_message()));
doc.SetKey("total_accuracy",
base::Value(static_cast<double>(results.total_accuracy())));
base::Value percentiles(base::Value::Type::DICTIONARY);
for (const auto& latencies : results.percentile_latencies_in_us()) {
std::string percentile = std::to_string(latencies.first);
percentiles.SetKey(percentile,
base::Value(static_cast<int>(latencies.second)));
}
doc.SetKey("percentile_latencies_in_us", std::move(percentiles));
base::Value metrics(base::Value::Type::LIST);
for (const auto& m : results.metrics()) {
base::Value metric(base::Value::Type::DICTIONARY);
metric.SetKey("name", base::Value(m.name()));
const auto direction = metric_direction(m.direction());
if (!direction)
return base::nullopt;
metric.SetKey("improvement_direction", base::Value(*direction));
const auto units = metric_units(m.units());
if (!units)
return base::nullopt;
metric.SetKey("units", base::Value(*units));
const auto cardinality = metric_cardinality(m.cardinality());
if (!cardinality)
return base::nullopt;
metric.SetKey("cardinality", base::Value(*cardinality));
if (m.cardinality() == Metric::SINGLE && m.values().size() != 1) {
LOG(ERROR) << "Single cardinality metrics should contain a single value. "
<< m.values().size() << " values found instead for metric "
<< m.name();
return base::nullopt;
}
base::Value values(base::Value::Type::LIST);
for (const auto& v : m.values()) {
values.Append(base::Value(v));
}
metric.SetKey("values", std::move(values));
metrics.Append(std::move(metric));
}
doc.SetKey("metrics", std::move(metrics));
return doc;
}
void WriteResultsToPath(const BenchmarkResults& results,
const base::FilePath& output_path) {
base::Optional<base::Value> doc = BenchmarkResultsToJson(results);
if (!doc) {
return;
}
std::string results_string;
if (!base::JSONWriter::Write(*doc, &results_string)) {
LOG(ERROR) << "Unable to serialize benchmarking results.";
return;
}
constexpr mode_t kFileRWMode = 0644;
if (!brillo::WriteToFileAtomic(output_path, results_string.c_str(),
results_string.size(), kFileRWMode)) {
LOG(ERROR) << "Unable to write out the benchmarking results";
}
}
} // namespace ml_benchmark