-
Notifications
You must be signed in to change notification settings - Fork 98
/
extract_results.dart
302 lines (228 loc) · 8.88 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
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
import 'dart:io';
// 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 \(w/o IO\)[^0-9]*([\d.]+)\s?(ms|s|milliseconds)');
final tTimeRegex = RegExp(r'Time[^0-9]*([\d.]+ (ms|s))');
final memUsageRegex = RegExp(r'memory: (\d+)k');
const multiCoreHeading = '''
### Multicore Results
| Language | Time (5k posts) | 20k posts | 60k posts | Total |
| -------------- | --------------- | ---------------- | ---------------- | --------- |
''';
const memUsageHeading = '''
### Memory Usage Results
| Language | 5k posts | 20k posts | 60k posts | Total |
| -------- | -------- | --------- | --------- | ----- |
''';
var min5k = double.maxFinite;
var min20k = double.maxFinite;
var min60k = double.maxFinite;
var con_min5k = double.maxFinite;
var con_min20k = double.maxFinite;
var con_min60k = double.maxFinite;
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 memUsageMatches = memUsageRegex.firstMatch(line);
if (memUsageMatches != null) {
final memUsage = int.parse(memUsageMatches.group(1)!);
currentScore.addMemoryUsage(memUsage);
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);
});
final sortedMemScores = scores.values.toList()
..sort((a, b) {
final aSum = a.fold(0.0, (total, sc) => sc.avgMemUsage() + total);
final bSum = b.fold(0.0, (total, sc) => sc.avgMemUsage() + 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) {
sortedScores.forEach(print);
sortedMemScores.forEach(print);
print(
'${file.readAsStringSync()}\n\nEnough scores not found. Need 3 scores for each language to update readme.md - $currentLang');
return;
}
final scoresWithoutJulia =
sortedScores.where((s) => s.first.name != 'Julia HO').toList();
// caclulate min times
min5k = scoresWithoutJulia.fold(
min5k, (min, sc) => sc[0].avgTimeMS() < min ? sc[0].avgTimeMS() : min);
min20k = scoresWithoutJulia.fold(
min20k, (min, sc) => sc[1].avgTimeMS() < min ? sc[1].avgTimeMS() : min);
min60k = scoresWithoutJulia.fold(
min60k, (min, sc) => sc[2].avgTimeMS() < min ? sc[2].avgTimeMS() : min);
con_min5k = multiCoreScores.fold(con_min5k,
(min, sc) => sc[0].avgTimeMS() < min ? sc[0].avgTimeMS() : min);
con_min20k = multiCoreScores.fold(con_min20k,
(min, sc) => sc[1].avgTimeMS() < min ? sc[1].avgTimeMS() : min);
con_min60k = multiCoreScores.fold(con_min60k,
(min, sc) => sc[2].avgTimeMS() < min ? sc[2].avgTimeMS() : min);
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';
// final memUsageLines = sortedMemScores.map((e) => e.toRowString(true)).join('\n') + '\n\n';
// add back the line with detail opening tag
return sCoreLines + multiCoreHeading + mCoreLines + line;
// return sCoreLines + multiCoreHeading + mCoreLines + memUsageHeading + memUsageLines + line;
})
.whereType<String>()
.join('\n');
readmeFile.writeAsStringSync(newReadmeContent);
}
typedef Time = ({double time, String unit});
class Score {
final String name;
final List<Time> processingTimes = [];
final List<int> memoryUsages = [];
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;
}
double avgMemUsage() {
if (memoryUsages.isEmpty) return double.maxFinite;
return memoryUsages.fold(0, (total, el) => el + total) /
memoryUsages.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';
}
String avgMemUsageString() {
if (avgTimeMS() >= double.maxFinite) return 'OOM';
return (avgMemUsage() / 1000).toStringAsFixed(2) + ' MB';
}
void addTime(double time, String unit) {
processingTimes.add((time: time, unit: unit));
}
void addMemoryUsage(int memUsage) {
memoryUsages.add(memUsage);
}
@override
String toString() {
return '| $name | ${avgTimeString()} |';
}
String toMemUsageString() {
return '| $name | ${avgMemUsageString()} |';
}
}
extension on List<Score> {
String toRowString([bool isMemUsage = false]) {
var name = first.name == "Julia HO" ? "_Julia HO_[^1]" : first.name;
if (name == 'Julia HO') {
name = '_Julia HO_[^1]';
} else if (name == 'Inko') {
name = 'Inko[^2]';
}
if (isMemUsage) {
return '| ${name} | ${first.avgMemUsageString()} | ${this[1].avgMemUsageString()} | ${this[2].avgMemUsageString()} | ${this.totalMemString} |';
}
final isConcurrent = name.contains('Concurrent');
final min5KToUse = isConcurrent ? con_min5k : min5k;
final min20KToUse = isConcurrent ? con_min20k : min20k;
final min60KToUse = isConcurrent ? con_min60k : min60k;
final fiveKTime = first.avgTimeMS() == min5KToUse
? '\$\\textsf{\\color{lightgreen}${first.avgTimeString()}}\$'
: first.avgTimeString();
final twentyKTime = this[1].avgTimeMS() == min20KToUse
? '\$\\textsf{\\color{lightgreen}${this[1].avgTimeString()}}\$'
: this[1].avgTimeString();
final sixtyKTime = this[2].avgTimeMS() == min60KToUse
? '\$\\textsf{\\color{lightgreen}${this[2].avgTimeString()}}\$'
: this[2].avgTimeString();
return '| ${name} | ${fiveKTime} | ${twentyKTime} | ${sixtyKTime} | ${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';
}
String get totalMemString {
if (this[2].avgMemUsageString() == 'OOM') return 'N/A';
final sum = fold(0.0, (total, sc) => sc.avgMemUsage() + total);
return (sum / 1000).toStringAsFixed(2) + ' MB';
}
}
extension on Time {
double get millis => unit == 'ms' ? time : time * 1000;
}