forked from jinyus/related_post_gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_results.dart
192 lines (140 loc) · 5.24 KB
/
extract_results.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
import 'dart:io';
import 'dart:math';
// script to extract benchmark results and update readme.md
final langRegex = RegExp(r'^[a-zA-Z]');
final colonOrNewLineRegex = RegExp(r'[:\n]');
final pTimeRegex = RegExp(r'Processing time[^0-9]*([\d.]+)\s?(ms|s|milliseconds)');
final tTimeRegex = RegExp(r'Time[^0-9]*([\d.]+ (ms|s))');
const multiCoreHeading = '''
### Multicore Results
| Language | Time (5k posts) | 20k posts | 60k posts | Total |
| -------------- | --------------- | ---------------- | ---------------- | --------- |
''';
void main(List<String> args) {
final filename = args.firstOrNull;
if (filename == null) return print('Usage: extract <filename>');
final file = File(filename);
if (!file.existsSync()) return print('File "$filename" not found');
final lines = file.readAsLinesSync();
final scores = <String, List<Score>>{};
Score? currentScore;
String? currentLang;
for (final line in lines) {
if (langRegex.hasMatch(line)) {
final name = line.trim().replaceAll(colonOrNewLineRegex, '');
if (scores.containsKey(name)) {
if (currentLang != name) {
final newScore = Score(name: name);
scores[name]!.add(newScore);
currentScore = newScore;
currentLang = name;
continue;
} else {
currentScore = scores[name]!.last;
continue;
}
}
final newScore = Score(name: name);
scores[name] = [newScore];
currentScore = newScore;
currentLang = name;
continue;
}
if (currentScore == null) {
continue;
}
final processTimeMatch = pTimeRegex.firstMatch(line);
if (processTimeMatch != null) {
final unit = processTimeMatch.group(2)!.replaceFirst('milliseconds', 'ms');
final time = double.parse(processTimeMatch.group(1)!.trim());
currentScore.addTime(time, unit);
continue;
}
}
final sortedScores = scores.values.toList()
..sort((a, b) {
final aSum = a.fold(0.0, (total, sc) => sc.avgTimeMS() + total);
final bSum = b.fold(0.0, (total, sc) => sc.avgTimeMS() + total);
return aSum.compareTo(bSum);
});
if (args.length > 1) {
sortedScores.forEach(print);
}
final multiCoreScores = sortedScores.where((s) => s.first.name.contains('Concurrent')).toList();
sortedScores..removeWhere((s) => s.first.name.contains('Concurrent'));
if (sortedScores.first.length != 3) {
print('${file.readAsStringSync()}\n\nEnough scores not found. Need 3 scores for each language to update readme.md');
return;
}
final readmePathList = file.absolute.path.split(Platform.pathSeparator)
..removeLast()
..add('readme.md');
final readmeFile = File(readmePathList.join('/'));
if (!readmeFile.existsSync()) return print('$readmeFile not found');
final readmeLines = readmeFile.readAsLinesSync();
var shouldReplace = false;
var replaced = false;
final newReadmeContent = readmeLines
.map((line) {
if (line.startsWith('| -----') && !replaced && !shouldReplace) {
shouldReplace = true;
return line;
}
if (!shouldReplace) return line;
// removes every line between the table heading and details open tag
if (!line.trim().contains('<details>')) return null;
shouldReplace = false;
replaced = true;
final sCoreLines = sortedScores.map((e) => e.toRowString()).join('\n') + '\n\n';
final mCoreLines = multiCoreScores.map((e) => e.toRowString()).join('\n') + '\n\n';
// add back the line with detail opening tag
return sCoreLines + multiCoreHeading + mCoreLines + line;
})
.whereType<String>()
.join('\n');
readmeFile.writeAsStringSync(newReadmeContent);
}
typedef Time = ({double time, String unit});
class Score {
final String name;
final List<Time> processingTimes = [];
Score({
required this.name,
});
double avgTimeMS() {
// if (processingTimes.isEmpty) throw Exception('No processing time found for $name');
if (processingTimes.isEmpty) return double.maxFinite;
return processingTimes.fold(0.0, (total, el) => el.millis + total) / processingTimes.length;
}
String avgTimeString() {
final avg = avgTimeMS();
if (avg >= double.maxFinite) return 'OOM';
if (avg < 1000) return avgTimeMS().toStringAsFixed(2) + ' ms';
return (avg / 1000).toStringAsFixed(2) + ' s';
}
void addTime(double time, String unit) {
processingTimes.add((time: time, unit: unit));
}
@override
String toString() {
return '| $name | ${avgTimeString()} |';
}
}
// var min5k = double.maxFinite;
// var min20k = double.maxFinite;
// var min60k = double.maxFinite;
extension on List<Score> {
String toRowString() {
final name = first.name == "Julia HO" ? "_Julia HO_[^1]" : first.name;
return '| ${name} | ${first.avgTimeString()} | ${this[1].avgTimeString()} | ${this[2].avgTimeString()} | ${this.totalString} |';
}
String get totalString {
if (this[2].avgTimeString() == 'OOM') return 'N/A';
final sum = fold(0.0, (total, sc) => sc.avgTimeMS() + total);
if (sum < 1000) return sum.toStringAsFixed(2) + ' ms';
return (sum / 1000).toStringAsFixed(2) + ' s';
}
}
extension on Time {
double get millis => unit == 'ms' ? time : time * 1000;
}