forked from dart-lang/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_results.dart
408 lines (348 loc) · 11.2 KB
/
diff_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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env dart
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:args/args.dart';
import 'package:glob/glob.dart';
final parser = ArgParser()
..addMultiOption('bot',
abbr: 'b',
help: 'Select the bots matching the glob pattern [option is repeatable]',
splitCommas: false)
..addFlag('verbose', abbr: 'v', help: 'Verbose output.', negatable: false)
..addFlag('help', help: 'Show the program usage.', negatable: false);
void printUsage() {
print("""
Usage: ${Platform.executable} ${Platform.script} [OLDER_COMMIT] [NEWER_COMMIT]
The options are as follows:
${parser.usage}""");
}
late bool verbose;
main(List<String> args) async {
final options = parser.parse(args);
if (options["help"]) {
printUsage();
return;
}
final commits = options.rest;
if (commits.length < 2) {
print('Need to supply at least two commits.');
printUsage();
exitCode = 1;
return;
}
verbose = options['verbose'] ?? false;
final globs = List<Glob>.from(options["bot"].map((pattern) => Glob(pattern)));
final vmBuilders = loadVmBuildersFromTestMatrix(globs);
final futures = <Future<List<Result>>>[];
for (final commit in commits) {
final DateTime date = await getDateOfCommit(commit);
futures.add(getResults(commit, date, vmBuilders));
}
final results = await Future.wait(futures);
for (int i = 0; i < results.length - 1; i++) {
final commitB = commits[i];
final commitA = commits[i + 1];
print('\nResult changes between $commitB -> $commitA:');
final commonGroups =
buildCommonGroups(commitA, commitB, results[i], results[i + 1]);
for (final commonGroup in commonGroups) {
final builders = commonGroup.builders;
print('');
for (final group in commonGroup.groups) {
final diff = group.diffs.first;
print('${group.test} ${diff.before} -> ${diff.after}');
}
for (final b in extractBuilderPattern(builders)) {
print(' on $b');
}
}
}
}
Future<DateTime> getDateOfCommit(String commit) async {
final result = await Process.run(
'git', ['show', '-s', '--format=%cd', '--date=iso-strict', commit]);
if (result.exitCode != 0) {
print('Could not determine date of commit $commit. Git reported:\n');
print(result.stdout);
print(result.stderr);
exit(1);
}
return DateTime.parse(result.stdout.trim());
}
Future<List<Result>> getResults(
String commit, DateTime dateC, Set<String> builders) async {
final DateTime date0 = dateC.add(const Duration(hours: 24));
final DateTime date2 = dateC.subtract(const Duration(hours: 24));
final query = '''
SELECT commit_time, builder_name, build_number, name, result, expected FROM `dart-ci.results.results`
WHERE commit_hash="$commit"
AND matches=false
AND (_PARTITIONDATE = "${formatDate(date0)}" OR
_PARTITIONDATE = "${formatDate(dateC)}" OR
_PARTITIONDATE = "${formatDate(date2)}" )
AND (STARTS_WITH(builder_name, "vm-") OR
STARTS_WITH(builder_name, "app-") OR
STARTS_WITH(builder_name, "cross-"))
AND ((flaky is NULL) OR flaky=false)
ORDER BY name''';
final arguments = <String>[
'query',
'--format=prettyjson',
'--project_id=dart-ci',
'--nouse_legacy_sql',
'-n',
'1000000',
query,
];
if (verbose) {
print('Executing query:\n bq ${arguments.join(' ')}');
}
final result = await Process.run('bq', arguments);
if (result.exitCode == 0) {
File('$commit.json').writeAsStringSync(result.stdout);
final resultsForCommit = json.decode(result.stdout);
final results = <Result>[];
for (final Map<String, dynamic> result in resultsForCommit) {
final builderName = result['builder_name'];
if (!builders.contains(builderName)) {
continue;
}
final failure = Result(commit, builderName, result['build_number'],
result['name'], result['expected'], result['result']);
results.add(failure);
}
results.sort((Result a, Result b) {
final c = a.name.compareTo(b.name);
if (c != 0) return c;
return a.builderName.compareTo(b.builderName);
});
return results;
} else {
print('Running the following query failed:\nbq ${arguments.join(' ')}');
print('Exit code: ${result.exitCode}');
final stdout = result.stdout.trim();
if (stdout.length > 0) {
print('Stdout:\n$stdout');
}
final stderr = result.stderr.trim();
if (stderr.length > 0) {
print('Stderr:\n$stderr');
}
return <Result>[];
}
}
List<CommonGroup> buildCommonGroups(String commitA, String commitB,
List<Result> commitResults, List<Result> commitResultsBefore) {
// If a test has same outcome across many vm builders
final diffs = <Diff>[];
int i = 0;
int j = 0;
while (i < commitResultsBefore.length && j < commitResults.length) {
final a = commitResultsBefore[i];
final b = commitResults[j];
// Is a smaller than b, then we had a failure before and no longer one.
if (a.name.compareTo(b.name) < 0 ||
(a.name.compareTo(b.name) == 0 &&
a.builderName.compareTo(b.builderName) < 0)) {
diffs.add(Diff(a, null));
i++;
continue;
}
// Is b smaller than a, then we had no failure before but have one now.
if (b.name.compareTo(a.name) < 0 ||
(b.name.compareTo(a.name) == 0 &&
b.builderName.compareTo(a.builderName) < 0)) {
diffs.add(Diff(null, b));
j++;
continue;
}
// Else we must have the same name and builder.
if (a.name != b.name || a.builderName != b.builderName) throw 'BUG';
if (a.expected != b.expected || a.result != b.result) {
diffs.add(Diff(a, b));
}
i++;
j++;
}
while (i < commitResultsBefore.length) {
final a = commitResultsBefore[i++];
diffs.add(Diff(a, null));
}
while (j < commitResults.length) {
final b = commitResults[j++];
diffs.add(Diff(null, b));
}
// If a test has same outcome across many vm builders
final groups = <GroupedDiff>[];
int h = 0;
while (h < diffs.length) {
final d = diffs[h++];
final builders = <String>{}..add(d.builder);
final groupDiffs = <Diff>[d];
while (h < diffs.length) {
final nd = diffs[h];
if (d.test == nd.test) {
if (d.sameExpectationDifferenceAs(nd)) {
builders.add(nd.builder);
groupDiffs.add(nd);
h++;
continue;
}
}
break;
}
groups.add(GroupedDiff(d.test, builders.toList()..sort(), groupDiffs));
}
final commonGroups = <String, List<GroupedDiff>>{};
for (final group in groups) {
final key = group.builders.join(' ');
commonGroups.putIfAbsent(key, () => <GroupedDiff>[]).add(group);
}
final commonGroupList = commonGroups.values
.map((list) => CommonGroup(list.first.builders, list))
.toList();
commonGroupList
.sort((a, b) => a.builders.length.compareTo(b.builders.length));
return commonGroupList;
}
class CommonGroup {
final List<String> builders;
final List<GroupedDiff> groups;
CommonGroup(this.builders, this.groups);
}
class GroupedDiff {
final String test;
final List<String> builders;
final List<Diff> diffs;
GroupedDiff(this.test, this.builders, this.diffs);
}
class Diff {
final Result? before;
final Result? after;
Diff(this.before, this.after);
String get test => (before?.name ?? after?.name)!;
String get builder => (before?.builderName ?? after?.builderName)!;
bool sameExpectationDifferenceAs(Diff other) {
if ((before == null) != (other.before == null)) return false;
if ((after == null) != (other.after == null)) return false;
if (before != null) {
if (!before!.sameResult(other.before!)) return false;
}
if (after != null) {
if (!after!.sameResult(other.after!)) return false;
}
return true;
}
}
class Result {
final String commit;
final String builderName;
final String buildNumber;
final String name;
final String expected;
final String result;
Result(this.commit, this.builderName, this.buildNumber, this.name,
this.expected, this.result);
@override
String toString() => '(expected: $expected, actual: $result)';
bool sameResult(Result other) {
return name == other.name &&
expected == other.expected &&
result == other.result;
}
bool equals(other) {
if (other is Result) {
if (name != other.name) return false;
if (builderName != other.builderName) return false;
}
return false;
}
@override
int get hashCode => name.hashCode ^ builderName.hashCode;
@override
bool operator ==(Object other) {
// TODO: implement ==
return super == other;
}
}
String currentDate() {
final timestamp = DateTime.now().toUtc().toIso8601String();
return timestamp.substring(0, timestamp.indexOf('T'));
}
Set<String> loadVmBuildersFromTestMatrix(List<Glob> globs) {
final contents = File('tools/bots/test_matrix.json').readAsStringSync();
final testMatrix = json.decode(contents);
final vmBuilders = <String>{};
for (final config in testMatrix['builder_configurations']) {
for (final builder in config['builders']) {
if (builder.startsWith('vm-') || builder.startsWith('app-')) {
vmBuilders.add(builder);
}
}
}
// This one is in the test_matrix.json but we don't run it on CI.
vmBuilders.remove('vm-kernel-asan-linux-release-ia32');
if (globs.isNotEmpty) {
vmBuilders.removeWhere((String builder) {
return !globs.any((Glob glob) => glob.matches(builder));
});
}
return vmBuilders;
}
List<String> extractBuilderPattern(List<String> builders) {
final all = Set<String>.from(builders);
String reduce(String builder, List<String> possibilities) {
for (final pos in possibilities) {
if (builder.contains(pos)) {
final existing = <String>[];
final available = <String>[];
for (final pos2 in possibilities) {
final builder2 = builder.replaceFirst(pos, pos2);
if (all.contains(builder2)) {
existing.add(builder2);
available.add(pos2);
}
}
if (existing.length > 1) {
all.removeAll(existing);
final replacement =
builder.replaceFirst(pos, '{${available.join(',')}}');
all.add(replacement);
return replacement;
}
}
}
return builder;
}
for (String builder in builders) {
if (all.contains(builder)) {
builder = reduce(builder, const ['debug', 'release', 'product']);
}
}
for (String builder in all.toList()) {
if (all.contains(builder)) {
builder = reduce(builder, const ['mac', 'linux', 'win']);
}
}
for (String builder in all.toList()) {
if (all.contains(builder)) {
builder = reduce(builder, const [
'ia32',
'x64',
'simarm',
'simarm64',
'arm',
'arm64',
]);
}
}
return all.toList()..sort();
}
String formatDate(DateTime date) {
final s = date.toIso8601String();
return s.substring(0, s.indexOf('T'));
}