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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
|
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-1.1
#include "Planner.hpp"
#include "Files.hpp"
#include "Hash.hpp"
#include "Json.hpp"
#include <algorithm>
#include <cstdio>
#include <functional>
#include <numeric>
#include <random>
namespace Progmasoft::Catch3::Runner
{
namespace
{
using std::chrono::milliseconds;
/// The duration assumed for a test never seen before.
constexpr std::int64_t kUnknownDurationMicroseconds = 500000;
/// Durations by test key, from a history or from a JSON report, for
/// balancing shards.
Expected<std::map<std::string, std::int64_t>>
LoadDurations(const std::string &path)
{
auto text = ReadFile(path);
if (!text)
{
return text.GetError();
}
auto json = ParseJson(*text);
if (!json)
{
return json.GetError().Context(path);
}
std::map<std::string, std::int64_t> durations;
const std::string &format = (*json)["format"].AsString();
if (format == "catch3-history")
{
auto history = History::FromJson(*json);
if (!history)
{
return history.GetError().Context(path);
}
for (const auto &[key, test] : history->Tests())
{
if (const auto median = test.DurationPercentile(0.5))
{
durations[key] = *median;
}
}
}
else if (format == "catch3-run")
{
for (const auto &test : (*json)["tests"].Items())
{
const std::string key = test["id"].AsString().empty()
? test["name"].AsString()
: test["id"].AsString();
durations[key] =
test["durationMicroseconds"].AsInteger();
}
}
else
{
return Error{path + " is neither a Catch3 history nor a "
"Catch3 JSON report"};
}
return durations;
}
std::string
Percent(double probability)
{
char buffer[16];
std::snprintf(buffer,
sizeof(buffer),
"%.0f%%",
probability * 100.0);
return buffer;
}
/// Finds the root of a set in a union-find forest.
std::size_t
Root(std::vector<std::size_t> &parents, std::size_t index)
{
while (parents[index] != index)
{
parents[index] = parents[parents[index]];
index = parents[index];
}
return index;
}
} // namespace
unsigned
StableShard(const std::string &key, unsigned count) noexcept
{
if (count <= 1)
{
return 0;
}
return static_cast<unsigned>(Fnv1a64(key) % count);
}
Expected<Plan>
MakePlan(const Options &options, const PlanInputs &inputs)
{
const Catalog &catalog = *inputs.catalog;
const auto &tests = catalog.Tests();
const History *history = inputs.history;
Plan plan;
plan.unmatchedSpecs = inputs.unmatchedSpecs;
plan.candidates = inputs.matched.size();
const auto historyOf = [&](const TestEntry &entry) -> const TestHistory * {
return history == nullptr ? nullptr : history->Find(entry.key);
};
std::vector<bool> selected(tests.size(), false);
std::vector<std::string> reasons(tests.size());
std::vector<std::optional<std::vector<std::string>>> replayKeys(
tests.size());
std::vector<std::optional<std::uint32_t>> replaySeeds(tests.size());
// 1. Selection.
if (options.replayFailures)
{
if (history != nullptr)
{
for (const auto &[key, test] : history->Tests())
{
const TestEntry *entry = catalog.Find(key);
if (entry == nullptr || !test.lastFailure)
{
continue;
}
selected[entry->index] = true;
reasons[entry->index] =
"failed in run " + std::to_string(test.lastFailure->run);
replayKeys[entry->index] = test.lastFailure->precededBy;
replaySeeds[entry->index] = test.lastFailure->seed;
}
}
if (std::none_of(selected.begin(), selected.end(), [](bool b) {
return b;
}))
{
plan.notes.emplace_back(
"the history holds no failure to replay");
}
}
else
{
const bool needsChanges =
options.selection == Selection::kChanged ||
options.selection == Selection::kSmart;
if (needsChanges && inputs.changes == nullptr)
{
plan.notes.emplace_back(
"Git could not list the changed files, so no test is "
"selected for a change");
}
for (const std::size_t index : inputs.matched)
{
const TestEntry &entry = tests[index];
const TestHistory *past = historyOf(entry);
const TestRecord *last = past == nullptr ? nullptr : past->Last();
const bool failedLast =
last != nullptr && (IsFailure(last->outcome) || last->flaky);
const bool isNew = last == nullptr;
bool changed = false;
if (inputs.changes != nullptr)
{
for (const auto &file : inputs.changes->files)
{
if (SameSourceFile(file, entry.file))
{
changed = true;
break;
}
}
}
const double probability =
past == nullptr ? 0.3 : past->FailureProbability();
switch (options.selection)
{
case Selection::kAll:
selected[index] = true;
break;
case Selection::kFailed:
if (failedLast)
{
selected[index] = true;
reasons[index] = last->flaky ? "flaky last run"
: "failed last run";
}
break;
case Selection::kNew:
if (isNew)
{
selected[index] = true;
reasons[index] = "no earlier result";
}
break;
case Selection::kChanged:
if (changed)
{
selected[index] = true;
reasons[index] = "its source file changed";
}
break;
case Selection::kSmart:
if (changed)
{
reasons[index] = "its source file changed";
}
else if (failedLast)
{
reasons[index] = "failed last run";
}
else if (isNew)
{
reasons[index] = "no earlier result";
}
else if (probability >= 0.5)
{
reasons[index] = "likely to fail (" +
Percent(probability) + ")";
}
else if (past != nullptr && past->quarantine)
{
reasons[index] = "in quarantine";
}
selected[index] = !reasons[index].empty();
break;
}
}
}
// 2. Dependencies come along, whatever selected the test.
std::vector<std::vector<std::size_t>> dependencies(tests.size());
std::vector<std::size_t> pending;
for (std::size_t index = 0; index < tests.size(); ++index)
{
if (selected[index])
{
pending.push_back(index);
}
}
while (!pending.empty())
{
const std::size_t index = pending.back();
pending.pop_back();
for (const auto &name : tests[index].dependsOn)
{
const TestEntry *dependency = catalog.Find(name);
if (dependency == nullptr)
{
return Error{"test '" + tests[index].name +
"' depends on '" + name +
"', which is not a test of this binary"};
}
dependencies[index].push_back(dependency->index);
if (!selected[dependency->index])
{
selected[dependency->index] = true;
reasons[dependency->index] =
"needed by '" + tests[index].name + "'";
pending.push_back(dependency->index);
}
}
}
// 3. A cycle can never run.
{
std::vector<int> state(tests.size(), 0);
std::vector<std::size_t> path;
std::string cycle;
const std::function<bool(std::size_t)> visit =
[&](std::size_t index) {
state[index] = 1;
path.push_back(index);
for (const std::size_t next : dependencies[index])
{
if (state[next] == 1)
{
const auto start =
std::find(path.begin(), path.end(), next);
for (auto at = start; at != path.end(); ++at)
{
cycle += "'" + tests[*at].name + "' -> ";
}
cycle += "'" + tests[next].name + "'";
return true;
}
if (state[next] == 0 && visit(next))
{
return true;
}
}
path.pop_back();
state[index] = 2;
return false;
};
for (std::size_t index = 0; index < tests.size(); ++index)
{
if (selected[index] && state[index] == 0 && visit(index))
{
return Error{"the tests depend on each other in a "
"cycle: " +
cycle};
}
}
}
// 4. Sharding. Tests linked by dependencies stay in one shard.
if (options.shard.count > 1)
{
std::vector<std::size_t> parents(tests.size());
std::iota(parents.begin(), parents.end(), std::size_t{0});
for (std::size_t index = 0; index < tests.size(); ++index)
{
for (const std::size_t dependency : dependencies[index])
{
parents[Root(parents, index)] = Root(parents, dependency);
}
}
// A group is named by its first key, in registration order.
std::map<std::size_t, std::vector<std::size_t>> groups;
for (std::size_t index = 0; index < tests.size(); ++index)
{
if (selected[index])
{
groups[Root(parents, index)].push_back(index);
}
}
std::vector<unsigned> shardOf(tests.size(), 0);
if (!options.shardDurations.empty())
{
auto durations = LoadDurations(options.shardDurations);
if (!durations)
{
return durations.GetError();
}
struct Group
{
std::int64_t duration = 0;
std::string key;
const std::vector<std::size_t> *members = nullptr;
};
std::vector<Group> ordered;
for (const auto &[root, members] : groups)
{
Group group;
group.members = &members;
group.key = tests[members.front()].key;
for (const std::size_t member : members)
{
const auto found = durations->find(tests[member].key);
group.duration += found == durations->end()
? kUnknownDurationMicroseconds
: found->second;
}
ordered.push_back(group);
}
// Longest first into the least loaded shard balances the
// shards; the order is total, so every machine agrees.
std::sort(ordered.begin(),
ordered.end(),
[](const Group &left, const Group &right) {
return left.duration != right.duration
? left.duration > right.duration
: left.key < right.key;
});
std::vector<std::int64_t> load(options.shard.count, 0);
for (const auto &group : ordered)
{
const auto least = static_cast<unsigned>(
std::min_element(load.begin(), load.end()) -
load.begin());
load[least] += group.duration;
for (const std::size_t member : *group.members)
{
shardOf[member] = least;
}
}
}
else
{
for (const auto &[root, members] : groups)
{
const unsigned shard = StableShard(
tests[members.front()].key, options.shard.count);
for (const std::size_t member : members)
{
shardOf[member] = shard;
}
}
}
for (std::size_t index = 0; index < tests.size(); ++index)
{
if (selected[index] && shardOf[index] != options.shard.index)
{
selected[index] = false;
}
}
}
// 5. Fingerprints, and what an incremental run may keep.
std::vector<bool> kept(tests.size(), false);
for (std::size_t index = 0; index < tests.size(); ++index)
{
if (!selected[index])
{
continue;
}
const TestEntry &entry = tests[index];
const std::string fingerprint =
Sha256Hex(inputs.binaryFingerprint + "\n" + entry.key);
plan.fingerprints[entry.key] = fingerprint;
if (!options.incremental || options.replayFailures)
{
continue;
}
const TestHistory *past = historyOf(entry);
const TestRecord *last = past == nullptr ? nullptr : past->Last();
if (last != nullptr && last->outcome == Outcome::kPassed &&
!last->flaky && last->fingerprint == fingerprint &&
!(past->quarantine && options.quarantine != QuarantineMode::kOff))
{
kept[index] = true;
TestResult result;
result.id = entry.key;
result.name = entry.name;
result.className = entry.className;
result.file = entry.file;
result.line = entry.line;
result.tags = entry.tags;
result.outcome = Outcome::kUpToDate;
result.selectionReason = reasons[index];
plan.kept.push_back(std::move(result));
}
}
// 6. The tests to run, with their limits and predictions.
std::vector<std::size_t> order;
for (std::size_t index = 0; index < tests.size(); ++index)
{
if (selected[index] && !kept[index])
{
order.push_back(index);
}
}
std::map<std::size_t, PlannedTest> planned;
for (const std::size_t index : order)
{
const TestEntry &entry = tests[index];
const TestHistory *past = historyOf(entry);
PlannedTest test;
test.entry = &entry;
test.selectionReason = reasons[index];
test.fingerprint = plan.fingerprints[entry.key];
test.failureProbability =
past == nullptr ? 0.3 : past->FailureProbability();
test.flakyHistory = past != nullptr && past->FlakyEvents(20) > 0;
const auto median =
past == nullptr ? std::nullopt : past->DurationPercentile(0.5);
test.predictedMicroseconds =
median ? *median : kUnknownDurationMicroseconds;
const milliseconds cap =
options.timeoutGiven ? options.timeout : milliseconds{300000};
if (entry.timeout)
{
test.timeout = *entry.timeout;
}
else if (options.adaptiveTimeout && !entry.benchmark && past &&
median)
{
// Generous enough for a slow machine, tight enough that a
// hang is noticed long before the global limit.
const auto p95 = past->DurationPercentile(0.95).value_or(*median);
const auto learned = std::max(p95 * 4, *median * 10) / 1000 + 2000;
test.timeout = milliseconds{std::max<std::int64_t>(5000, learned)};
if (cap.count() > 0 && test.timeout > cap)
{
test.timeout = cap;
}
test.timeoutLearned = true;
}
else
{
test.timeout = cap;
}
if (entry.retries)
{
test.retryLimit = *entry.retries;
}
else
{
test.retryLimit = options.retryMode == RetryMode::kOff
? 0
: options.retries;
}
if (options.quarantine != QuarantineMode::kOff)
{
if (entry.quarantined)
{
test.quarantined = true;
test.quarantineReason = "tagged [!quarantine]";
}
else if (past != nullptr && past->quarantine)
{
test.quarantined = true;
test.quarantineReason = past->quarantine->reason;
}
}
if (replaySeeds[index])
{
test.replaySeed = replaySeeds[index];
}
planned.emplace(index, std::move(test));
}
// 7. The order the scheduler prefers.
switch (options.order)
{
case Order::kDeclared:
break;
case Order::kName:
std::stable_sort(order.begin(),
order.end(),
[&](std::size_t left, std::size_t right) {
return tests[left].name < tests[right].name;
});
break;
case Order::kRandom:
{
std::mt19937 generator{options.seed};
std::shuffle(order.begin(), order.end(), generator);
break;
}
case Order::kPriority:
{
// Tests likely to fail go first, the most failures per second
// first, so a failing run fails early. The rest go longest
// first, which keeps the workers busy until the end.
const auto risky = [&](std::size_t index) {
return planned.at(index).failureProbability >= 0.5;
};
const auto rate = [&](std::size_t index) {
const auto &test = planned.at(index);
const double seconds = std::max(
0.05,
static_cast<double>(test.predictedMicroseconds) / 1e6);
return test.failureProbability / seconds;
};
std::stable_sort(
order.begin(),
order.end(),
[&](std::size_t left, std::size_t right) {
if (risky(left) != risky(right))
{
return risky(left);
}
if (risky(left))
{
return rate(left) > rate(right);
}
return planned.at(left).predictedMicroseconds >
planned.at(right).predictedMicroseconds;
});
break;
}
}
std::map<std::size_t, std::size_t> position;
for (std::size_t at = 0; at < order.size(); ++at)
{
position[order[at]] = at;
}
std::map<std::string, std::size_t> indexByKey;
for (const auto &entry : tests)
{
indexByKey[entry.key] = entry.index;
}
for (const std::size_t index : order)
{
PlannedTest test = std::move(planned.at(index));
for (const std::size_t dependency : dependencies[index])
{
// A dependency kept as up to date has already passed.
if (const auto found = position.find(dependency);
found != position.end())
{
test.dependencies.push_back(found->second);
}
}
if (replayKeys[index])
{
std::vector<std::size_t> sequence;
for (const auto &key : *replayKeys[index])
{
if (const auto found = indexByKey.find(key);
found != indexByKey.end())
{
sequence.push_back(found->second);
}
}
test.replaySequence = std::move(sequence);
}
plan.tests.push_back(std::move(test));
}
return plan;
}
} // namespace Progmasoft::Catch3::Runner
|