forked from ngcpp/proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
246 lines (224 loc) · 7.42 KB
/
main.cpp
File metadata and controls
246 lines (224 loc) · 7.42 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) 2022-2026 Microsoft Corporation.
// Copyright (c) 2026-Present Next Gen C++ Foundation.
// Licensed under the MIT License.
#include <cstdio>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include <nlohmann/json.hpp>
struct Environment {
std::string Description;
std::string InfoPath;
std::string BenchmarkingResultsPath;
friend void to_json(nlohmann::json& j, const Environment& e) {
j = nlohmann::json{
{"Description", e.Description},
{"InfoPath", e.InfoPath},
{"BenchmarkingResultsPath", e.BenchmarkingResultsPath},
};
}
friend void from_json(const nlohmann::json& j, Environment& e) {
j.at("Description").get_to(e.Description);
j.at("InfoPath").get_to(e.InfoPath);
j.at("BenchmarkingResultsPath").get_to(e.BenchmarkingResultsPath);
}
};
struct EnvironmentInfo {
std::string OS;
std::string KernelVersion;
std::string Architecture;
std::string Compiler;
friend void to_json(nlohmann::json& j, const EnvironmentInfo& e) {
j = nlohmann::json{
{"OS", e.OS},
{"KernelVersion", e.KernelVersion},
{"Architecture", e.Architecture},
{"Compiler", e.Compiler},
};
}
friend void from_json(const nlohmann::json& j, EnvironmentInfo& e) {
j.at("OS").get_to(e.OS);
j.at("KernelVersion").get_to(e.KernelVersion);
j.at("Architecture").get_to(e.Architecture);
j.at("Compiler").get_to(e.Compiler);
}
};
struct Metric {
std::string Name;
std::string TargetBenchmarkName;
std::string BaselineBenchmarkName;
friend void to_json(nlohmann::json& j, const Metric& m) {
j = nlohmann::json{
{"Name", m.Name},
{"TargetBenchmarkName", m.TargetBenchmarkName},
{"BaselineBenchmarkName", m.BaselineBenchmarkName},
};
}
friend void from_json(const nlohmann::json& j, Metric& m) {
j.at("Name").get_to(m.Name);
j.at("TargetBenchmarkName").get_to(m.TargetBenchmarkName);
j.at("BaselineBenchmarkName").get_to(m.BaselineBenchmarkName);
}
};
struct MetricGroup {
std::string Name;
std::string Description;
std::vector<Metric> Metrics;
friend void to_json(nlohmann::json& j, const MetricGroup& m) {
j = nlohmann::json{
{"Name", m.Name},
{"Description", m.Description},
{"Metrics", m.Metrics},
};
}
friend void from_json(const nlohmann::json& j, MetricGroup& m) {
j.at("Name").get_to(m.Name);
j.at("Description").get_to(m.Description);
j.at("Metrics").get_to(m.Metrics);
}
};
struct ReportConfig {
std::string TargetName;
double YellowIndicatorThreshold;
std::string OutputPath;
std::vector<Environment> Environments;
std::vector<MetricGroup> MetricGroups;
friend void to_json(nlohmann::json& j, const ReportConfig& rc) {
j = nlohmann::json{
{"TargetName", rc.TargetName},
{"YellowIndicatorThreshold", rc.YellowIndicatorThreshold},
{"OutputPath", rc.OutputPath},
{"Environments", rc.Environments},
{"MetricGroups", rc.MetricGroups},
};
}
friend void from_json(const nlohmann::json& j, ReportConfig& rc) {
j.at("TargetName").get_to(rc.TargetName);
j.at("YellowIndicatorThreshold").get_to(rc.YellowIndicatorThreshold);
j.at("OutputPath").get_to(rc.OutputPath);
j.at("Environments").get_to(rc.Environments);
j.at("MetricGroups").get_to(rc.MetricGroups);
}
};
const std::string_view MedianSuffix = "_median";
EnvironmentInfo ParseEnvironmentInfo(const std::filesystem::path& file) {
nlohmann::json obj;
std::ifstream in;
in.exceptions(std::ios_base::failbit | std::ios_base::badbit);
in.open(file, std::ios_base::in | std::ios_base::binary);
in >> obj;
return obj.get<EnvironmentInfo>();
}
std::unordered_map<std::string, double>
ParseBenchmarkingResults(const std::filesystem::path& file) {
nlohmann::json obj;
{
std::ifstream in;
in.exceptions(std::ios_base::failbit | std::ios_base::badbit);
in.open(file, std::ios_base::in | std::ios_base::binary);
in >> obj;
}
std::unordered_map<std::string, double> result;
for (auto& node : obj["benchmarks"]) {
std::string name = node["name"];
if (name.ends_with(MedianSuffix)) {
name.resize(name.size() - MedianSuffix.size());
double value = node["real_time"];
result.emplace(std::move(name), value);
}
}
return result;
}
void GenerateReport(const std::filesystem::path& config_path) {
ReportConfig config;
{
nlohmann::json obj;
std::ifstream in;
in.exceptions(std::ios_base::failbit | std::ios_base::badbit);
in.open(config_path, std::ios_base::in | std::ios_base::binary);
in >> obj;
obj.get_to(config);
}
std::vector<std::unordered_map<std::string, double>> benchmarks;
benchmarks.reserve(config.Environments.size());
for (auto& environment : config.Environments) {
benchmarks.push_back(
ParseBenchmarkingResults(environment.BenchmarkingResultsPath));
}
std::ofstream out;
out.exceptions(std::ios_base::failbit | std::ios_base::badbit);
out.open(config.OutputPath,
std::ios_base::out | std::ios_base::app | std::ios_base::binary);
for (auto& metric_group : config.MetricGroups) {
out << "## " << metric_group.Name << "\n\n";
out << metric_group.Description << "\n\n";
out << "| |";
for (auto& environment : config.Environments) {
out << " " << environment.Description << " |";
}
out << "\n";
out << "| - |";
for (std::size_t i = 0; i < config.Environments.size(); ++i) {
out << " - |";
}
out << "\n";
for (auto& metric : metric_group.Metrics) {
out << "| " << metric.Name << " |";
for (auto& benchmark : benchmarks) {
double target = benchmark.at(metric.TargetBenchmarkName);
double baseline = benchmark.at(metric.BaselineBenchmarkName);
double rate = (baseline - target) * 100 / target;
bool is_negative = rate < 0;
if (is_negative) {
rate = -rate;
}
out << " ";
if (rate < config.YellowIndicatorThreshold) {
out << "\xf0\x9f\x9f\xa1"; // Yellow circle
} else if (is_negative) {
out << "\xf0\x9f\x94\xb4"; // Red circle
} else {
out << "\xf0\x9f\x9f\xa2"; // Green circle
}
auto rate_str = std::format("{:.1f}", rate);
std::string message;
if (rate_str == "0.0") {
out << config.TargetName << " has similar performance";
} else {
out << config.TargetName << " is about **" << rate_str << "% "
<< (is_negative ? "slower" : "faster") << "**";
}
out << " |";
}
out << "\n";
}
out << "\n";
}
out << "## Environments\n\n";
out << "| | Operating System | Kernel Version | Architecture | Compiler |\n";
out << "| - | - | - | - | - |\n";
for (auto& environment : config.Environments) {
EnvironmentInfo env_info = ParseEnvironmentInfo(environment.InfoPath);
out << "| **" << environment.Description << "** | " << env_info.OS << " | "
<< env_info.KernelVersion << " | " << env_info.Architecture << " | "
<< env_info.Compiler << " |\n";
}
}
int main(int argc, char** argv) {
if (argc != 2) {
puts("Usage: report_generator <config file path>");
return 0;
}
try {
GenerateReport(argv[1]);
} catch (const std::exception& e) {
fprintf(stderr, "An error occurred: %s\n", e.what());
return 1;
}
return 0;
}