Files
floci/.github/ci/test_partition.py
T
Priamo Germosen 9eb3ba38ed ci: weigh the two slow EMR Docker classes when partitioning test shards (#7901)
partition.sh balances shards by class count, which says nothing of how
long a class runs. EmrDockerRunnerDockerIntegrationTest and
EmrHiveStepsDockerIntegrationTest take about 540s between them, so the
shard holding them ran 1246s of Surefire's 1500s while the others ran
641s to 869s, and one run has already been cut at the limit.

- SLOW_CLASSES gives the seconds of those two, by class name, so a
  package rename leaves them alone. A class counts for that many average
  classes, and every other class counts as one, so with none listed the
  assignment is what it was.
- Groups are packed heaviest first by that weight rather than by their
  number of classes, and a slow class without a profile goes before the
  others, so the rest are placed around them.
- A name that no longer matches a class is an annotation, not a failure,
  as a stale key is for the image prefetch: the shards are only uneven.

Summing the 2026-11-09 run's class timings over the classes at this
commit, the slowest shard goes from 1136s to 989s. A profile of its own
for the EMR classes gave 1321s and a sixth shard 1117s, as neither tells
the partition that those classes are slow.
2026-11-10 22:47:50 -06:00

155 lines
7.7 KiB
Python

"""Run with python3 -m unittest discover -s .github/ci -p 'test_partition.py'."""
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
PATH = Path(__file__).with_name('partition.sh').resolve()
class PartitionTest(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.root = Path(directory.name)
self.classes = self.root / 'classes.txt'
def source(self, name, annotation):
path = self.root / 'src/test/java' / (name + '.java')
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(annotation + '\nclass Fixture {}\n')
def invoke(self, total, index):
return subprocess.run([sys.executable, str(PATH), str(self.classes), str(total), str(index)],
cwd=self.root, capture_output=True, text=True)
def shards(self, classes, total):
self.classes.write_text('\n'.join(classes) + '\n')
shards = []
for index in range(1, total + 1):
result = self.invoke(total, index)
self.assertEqual(0, result.returncode, result.stderr)
lines = result.stdout.splitlines()
self.assertEqual(sorted(set(lines)), lines)
shards.append(lines)
flattened = [name for shard in shards for name in shard]
expected = {name.strip() for name in classes if name.strip()}
self.assertEqual(expected, set(flattened))
self.assertEqual(len(expected), len(flattened), 'Each class must appear exactly once')
return shards
def test_legacy_profile_and_plain_class_assignment_is_preserved(self):
for name in ('p/A', 'p/B'):
self.source(name, '@TestProfile(P.class)')
self.assertEqual([['p/A', 'p/B'], ['p/C', 'p/D']],
self.shards(['p/D', 'p/C', 'p/B', 'p/A'], 2))
def test_equal_profile_groups_use_stable_name_tiebreaking(self):
for name in ('p/A', 'p/B'):
self.source(name, '@TestProfile(Alpha.class)')
for name in ('p/C', 'p/D'):
self.source(name, '@TestProfile(Beta.class)')
self.assertEqual([['p/A', 'p/B', 'p/E'], ['p/C', 'p/D', 'p/F']],
self.shards(['p/F', 'p/E', 'p/D', 'p/C', 'p/B', 'p/A'], 2))
def test_profile_augmentation_weight_keeps_plain_classes_on_the_other_shard(self):
for name in ('p/A', 'p/B'):
self.source(name, '@TestProfile(P.class)')
self.assertEqual([['p/A', 'p/B'], ['p/C', 'p/D', 'p/E', 'p/F']],
self.shards(['p/F', 'p/E', 'p/D', 'p/C', 'p/B', 'p/A'], 2))
def test_larger_profile_groups_are_assigned_before_alphabetically_earlier_groups(self):
self.source('p/A', '@TestProfile(Alpha.class)')
for name in ('p/B', 'p/C', 'p/D'):
self.source(name, '@TestProfile(Zeta.class)')
self.assertEqual([['p/B', 'p/C', 'p/D'], ['p/A']],
self.shards(['p/D', 'p/C', 'p/B', 'p/A'], 2))
def test_plain_classes_fill_shards_by_class_count(self):
self.assertEqual([['p/A', 'p/D'], ['p/B', 'p/E'], ['p/C']],
self.shards(['p/E', 'p/D', 'p/C', 'p/B', 'p/A'], 3))
def test_input_order_duplicates_and_blank_lines_do_not_change_assignment(self):
classes = ['p/A', 'p/B', 'p/C', 'p/D', 'p/E']
self.source('p/A', '@TestProfile(P.class)')
self.source('p/B', '@TestProfile(P.class)')
expected = self.shards(classes, 3)
actual = self.shards(list(reversed(classes)) + classes + ['', ' ', ' p/C '], 3)
self.assertEqual(expected, actual)
def test_profiles_remain_atomic_with_complete_disjoint_coverage(self):
for name in ('p/A', 'p/B', 'p/C'):
self.source(name, '@TestProfile( Shared.class )')
classes = ['p/' + name for name in 'ABCDEFGHIJKL']
shards = self.shards(classes, 4)
self.assertTrue(any({'p/A', 'p/B', 'p/C'} <= set(shard) for shard in shards))
def test_source_paths_and_profile_tokens_group_across_packages(self):
self.source('first/A', '@TestProfile(profiles.Shared.class)')
self.source('second/B', '@TestProfile( profiles.Shared.class )')
self.assertEqual([['first/A', 'second/B'], ['third/C']],
self.shards(['third/C', 'second/B', 'first/A'], 2))
def test_a_slow_class_in_a_profile_keeps_other_classes_off_its_shard(self):
self.source('p/EmrDockerRunnerDockerIntegrationTest', '@TestProfile(Docker.class)')
plain = ['p/C%03d' % n for n in range(40)]
shards = self.shards(['p/EmrDockerRunnerDockerIntegrationTest'] + plain, 2)
self.assertEqual([['p/EmrDockerRunnerDockerIntegrationTest'], plain], shards)
def test_a_group_holding_a_slow_class_is_placed_before_a_larger_group(self):
self.source('p/EmrHiveStepsDockerIntegrationTest', '@TestProfile(Docker.class)')
for name in ('p/A', 'p/B', 'p/C', 'p/D'):
self.source(name, '@TestProfile(Zeta.class)')
shards = self.shards(['p/A', 'p/B', 'p/C', 'p/D', 'p/EmrHiveStepsDockerIntegrationTest'], 2)
self.assertEqual(['p/EmrHiveStepsDockerIntegrationTest'], shards[0])
def test_a_slow_class_without_a_profile_is_placed_before_the_others(self):
plain = ['p/A', 'p/B', 'p/C']
shards = self.shards(plain + ['p/EmrDockerRunnerDockerIntegrationTest'], 2)
self.assertEqual([['p/EmrDockerRunnerDockerIntegrationTest'], plain], shards)
def test_a_slow_class_is_known_by_its_name_whatever_its_package(self):
plain = ['some/other/C%03d' % n for n in range(40)]
shards = self.shards(['some/where/EmrHiveStepsDockerIntegrationTest'] + plain, 2)
self.assertEqual(['some/where/EmrHiveStepsDockerIntegrationTest'], shards[0])
def test_two_slow_classes_of_one_profile_stay_together(self):
self.source('p/EmrDockerRunnerDockerIntegrationTest', '@TestProfile(Docker.class)')
self.source('p/EmrHiveStepsDockerIntegrationTest', '@TestProfile(Docker.class)')
shards = self.shards(['p/EmrDockerRunnerDockerIntegrationTest', 'p/EmrHiveStepsDockerIntegrationTest', 'p/A'], 2)
self.assertEqual(['p/EmrDockerRunnerDockerIntegrationTest', 'p/EmrHiveStepsDockerIntegrationTest'], shards[0])
def test_a_slow_class_that_no_longer_exists_is_named_and_changes_nothing(self):
self.classes.write_text('p/A\np/EmrHiveStepsDockerIntegrationTest\n')
result = self.invoke(2, 1)
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('p/EmrHiveStepsDockerIntegrationTest\n', result.stdout)
self.assertIn('::warning title=partition.sh::stale SLOW_CLASSES key: no test class named '
'EmrDockerRunnerDockerIntegrationTest', result.stderr)
self.assertNotIn('EmrHiveStepsDockerIntegrationTest', result.stderr)
def test_invalid_shard_arguments_fail_without_emitting_assignments(self):
self.classes.write_text('p/A\np/B\n')
for total, index in ((0, 1), (2, 0), (2, 3), ('invalid', 1), (2, 'invalid')):
with self.subTest(total=total, index=index):
result = self.invoke(total, index)
self.assertNotEqual(0, result.returncode)
self.assertEqual('', result.stdout)
def test_empty_shard_fails_instead_of_running_the_entire_suite(self):
self.classes.write_text('p/A\n')
result = self.invoke(2, 2)
self.assertNotEqual(0, result.returncode)
self.assertEqual('', result.stdout)
self.assertIn('would be empty', result.stderr)
def test_missing_manifest_fails_without_emitting_assignments(self):
result = self.invoke(2, 1)
self.assertNotEqual(0, result.returncode)
self.assertEqual('', result.stdout)
if __name__ == '__main__':
unittest.main()