-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgen_bench_report.dart
311 lines (259 loc) · 8.88 KB
/
gen_bench_report.dart
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import 'dart:io';
import 'dart:math' show pow, sqrt;
Future<void> main() async {
final reports = readAllBenchmarkReports();
// 1. Generate detailed test case table
generateTestCaseTable(reports);
// 2. Calculate overall scores
final scores = calculateScores(reports);
// 3. Generate ranking report
generateRankingReport(scores);
}
class ScoreResult {
final String framework;
final double overallScore;
final double successRate;
final int totalTests;
final int passedTests;
final Map<String, TestMetric> testMetrics;
final Duration totalTime;
ScoreResult({
required this.framework,
required this.overallScore,
required this.successRate,
required this.totalTests,
required this.passedTests,
required this.testMetrics,
required this.totalTime,
});
}
class TestMetric {
final int microseconds;
final double normalizedScore;
final double coefficient;
final String status;
TestMetric({
required this.microseconds,
required this.normalizedScore,
required this.coefficient,
required this.status,
});
}
Map<String, Map<String, ({String stateCaseName, int microseconds})>>
readAllBenchmarkReports() {
final reports = <String, Map<String, int>>{};
final benchReportDir = Directory('bench');
for (final file in benchReportDir.listSync()) {
if (file is! File) continue;
final lines = file.readAsLinesSync().skip(2);
for (final line in lines) {
final [framework, testCase, microseconds] =
line.split('|').skip(1).take(3).map((e) => e.trim()).toList();
reports[framework] ??= {};
reports[framework]![testCase] = int.parse(microseconds);
}
}
final testCases =
<String, Map<String, ({String stateCaseName, int microseconds})>>{};
for (final MapEntry(key: framework, value: cases) in reports.entries) {
for (final MapEntry(key: testCase, value: microseconds) in cases.entries) {
final trimmedTestCase = trimTestCaseName(testCase);
final group = testCases[trimmedTestCase] ??=
<String, ({String stateCaseName, int microseconds})>{};
group[framework] = (stateCaseName: testCase, microseconds: microseconds);
}
}
return testCases;
}
void generateTestCaseTable(
Map<String, Map<String, ({String stateCaseName, int microseconds})>>
reports) {
final testCaseTable = StringBuffer();
testCaseTable.write('| Test Case | ');
testCaseTable.write(reports.values.first.keys.join(' | '));
testCaseTable.writeln(' |');
testCaseTable.write('|---|');
testCaseTable.write(
Iterable.generate(reports.values.first.length, (_) => '---').join('|'));
testCaseTable.writeln('|');
for (final MapEntry(key: testCase, value: group) in reports.entries) {
testCaseTable.write('| $testCase | ');
testCaseTable.writeAll([
for (final (:microseconds, :stateCaseName) in group.values)
formatTestResult(microseconds, stateCaseName),
], ' | ');
testCaseTable.writeln(' |');
}
updateReadme(testCaseTable.toString(), 'test-case');
}
int calculateBaseline(
Map<String, ({String stateCaseName, int microseconds})> group) {
return group.values
.where((test) => calculateCoefficient(test.stateCaseName) >= 0.5)
.map((test) => test.microseconds)
.reduce((min, time) => time < min ? time : min);
}
double calculateCoefficient(String testCase) {
if (testCase.contains('(fail)')) return 0.0;
double coefficient = 1.0;
if (testCase.contains('sum: fail')) coefficient *= 0.5;
if (testCase.contains('count: fail')) coefficient *= 0.5;
if (testCase.contains('sum: fail') && testCase.contains('count: fail')) {
coefficient = 0.0;
}
return coefficient;
}
String getTestStatus(double coefficient) {
if (coefficient >= 1.0) return 'pass';
if (coefficient > 0.0) return 'partial';
return 'fail';
}
String formatDuration(Duration duration) {
final microseconds = duration.inMicroseconds;
if (microseconds < 1000) {
return '$microsecondsμs';
} else if (microseconds < 1000000) {
return '${(microseconds / 1000).toStringAsFixed(2)}ms';
}
return '${(microseconds / 1000000).toStringAsFixed(2)}s';
}
String formatTestResult(int microseconds, String testCase) {
final coefficient = calculateCoefficient(testCase);
final time = formatDuration(Duration(microseconds: microseconds));
if (coefficient == 0.0) return '$time (fail)';
if (coefficient == 0.5) return '$time (partial)';
return time;
}
void updateReadme(String content, String section) {
final readme = File('README.md');
final readmeContent = readme.readAsStringSync();
final sectionStart = readmeContent.indexOf('<!-- $section start -->') +
'<!-- $section start -->'.length;
final sectionEnd = readmeContent.indexOf('<!-- $section end -->');
final newContent =
readmeContent.replaceRange(sectionStart, sectionEnd, '\n$content\n');
readme.writeAsStringSync(newContent);
}
String trimTestCaseName(String name) {
const needRemoved = [
'(success)',
'(fail)',
'(first: pass,',
'(first: fail,',
'last: pass)',
'last: fail)',
', sum: pass',
', sum: fail',
', count: pass',
', count: fail',
];
for (final pattern in needRemoved) {
name = name.replaceAll(pattern, '').trim();
}
return name;
}
Map<String, ScoreResult> calculateScores(
Map<String, Map<String, ({String stateCaseName, int microseconds})>>
reports) {
final scores = <String, ScoreResult>{};
final testWeights = defineTestWeights();
Duration? fastestTotalTime;
// First pass to find fastest total time
for (final framework in reports.values.first.keys) {
var totalTime = Duration.zero;
for (final group in reports.values) {
final test = group[framework]!;
totalTime += Duration(microseconds: test.microseconds);
}
if (fastestTotalTime == null || totalTime < fastestTotalTime) {
fastestTotalTime = totalTime;
}
}
// Second pass to calculate scores
for (final framework in reports.values.first.keys) {
var testMetrics = <String, TestMetric>{};
var totalTests = 0;
var passedTests = 0;
var totalTime = Duration.zero;
for (final MapEntry(key: testCase, value: group) in reports.entries) {
final test = group[framework]!;
totalTests++;
final baseline = calculateBaseline(group);
final coefficient = calculateCoefficient(test.stateCaseName);
final normalizedScore = calculateNormalizedScore(test.microseconds,
baseline, coefficient, testWeights[testCase] ?? 1.0);
testMetrics[testCase] = TestMetric(
microseconds: test.microseconds,
normalizedScore: normalizedScore,
coefficient: coefficient,
status: getTestStatus(coefficient),
);
if (coefficient >= 1.0) passedTests++;
totalTime += Duration(microseconds: test.microseconds);
}
final overallScore =
calculateOverallScore(testMetrics, totalTime, fastestTotalTime!);
final successRate = passedTests / totalTests;
scores[framework] = ScoreResult(
framework: framework,
overallScore: overallScore,
successRate: successRate,
totalTests: totalTests,
passedTests: passedTests,
testMetrics: testMetrics,
totalTime: totalTime,
);
}
return scores;
}
double calculateOverallScore(
Map<String, TestMetric> metrics, Duration totalTime, Duration fastestTime) {
final baseScore =
geometricMean(metrics.values.map((m) => m.normalizedScore).toList());
final timeFactor = fastestTime.inMicroseconds / totalTime.inMicroseconds;
return baseScore * sqrt(timeFactor);
}
double geometricMean(List<double> values) {
return pow(values.reduce((a, b) => a * b), 1 / values.length).toDouble();
}
double calculateNormalizedScore(
int microseconds,
int baseline,
double coefficient,
double weight,
) {
final baseScore = baseline / microseconds;
return baseScore * coefficient * weight;
}
Map<String, double> defineTestWeights() {
return {
'Simple Counter': 1.0,
'Computed Properties': 1.2,
'Collection Operations': 1.3,
'Deep Update': 1.4,
'Complex Update': 1.5,
};
}
void generateRankingReport(Map<String, ScoreResult> scores) {
final rankTable = StringBuffer();
rankTable
.writeln('| Rank | Framework | Score | Success Rate | Tests | Time |');
rankTable
.writeln('|------|-----------|-------|--------------|-------|------|');
final sortedScores = scores.values.toList()
..sort((a, b) => b.overallScore.compareTo(a.overallScore));
for (final (index, score) in sortedScores.indexed) {
final rank = switch (index) {
0 => '🥇',
1 => '🥈',
2 => '🥉',
_ => '${index + 1}',
};
rankTable.writeln('| $rank | ${score.framework} | '
'${score.overallScore.toStringAsFixed(2)} | '
'${(score.successRate * 100).toStringAsFixed(1)}% | '
'${score.passedTests}/${score.totalTests} | '
'${formatDuration(score.totalTime)} |');
}
updateReadme(rankTable.toString(), 'ranking');
}