forked from jinyus/related_post_gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_results.dart
124 lines (89 loc) · 3.12 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
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[^0-9]*([\d.]+)\s?(ms|s|milliseconds)');
final tTimeRegex = RegExp(r'Time[^0-9]*([\d.]+ (ms|s))');
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, Score>{};
Score? currentScore;
for (final line in lines) {
if (langRegex.hasMatch(line)) {
final name = line.trim().replaceAll(colonOrNewLineRegex, '');
currentScore = (scores[name] ??= Score(name: 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 totalTimeMatch = tTimeRegex.firstMatch(line);
if (totalTimeMatch != null) {
currentScore.totalTime = totalTimeMatch.group(1)!.trim();
}
}
final sortedScores = scores.values.toList()..sort((a, b) => a.avgTime().compareTo(b.avgTime()));
sortedScores.forEach(print);
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 = true;
return line;
}
if (!shouldReplace) return line;
// removes each result line until it finds an empty line
if (line.trim().isNotEmpty) return null;
shouldReplace = false;
replaced = true;
return sortedScores.map((e) => e.toString()).join('\n') + '\n';
})
.whereType<String>()
.join('\n');
readmeFile.writeAsStringSync(newReadmeContent);
}
class Score {
final String name;
final List<double> processingTimes = [];
String unit = "";
String totalTime = "";
Score({
required this.name,
});
double _avgAbsoluteTime() {
if (processingTimes.isEmpty) throw Exception('No processing time found for $name');
return processingTimes.reduce((a, b) => a + b) / processingTimes.length;
}
double avgTime() {
return _avgAbsoluteTime() * (unit == 's' ? 1000 : 1);
}
String avgTimeString() {
return _avgAbsoluteTime().toStringAsFixed(2) + ' $unit';
}
void addTime(double time, String unit) {
processingTimes.add(time);
this.unit = unit;
}
@override
String toString() {
return '| $name | ${avgTimeString()} | $totalTime |';
}
}