-
Notifications
You must be signed in to change notification settings - Fork 1.7k
chore: add test sharding to unit tests #17438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
01d4753
a7502d4
9ad0f0c
02c7653
3c8d057
73ab091
74f1ef7
a450254
af4d10c
15916df
49cac0f
d8dd522
de74a68
46d1583
6e3531d
e6167bd
fd5792d
8b825d5
32968a9
ff1274f
c49b06d
1cd8c1f
a0f5c9a
71f444a
cf1a1f1
b00967f
7bb323c
3633089
980d8eb
a5cc497
b36b559
c3cf49e
acdfb18
9c80a51
cf554ca
886d6d9
83ca04a
ae05642
7b565a4
72503de
e34b654
6c1be8b
8fb68d3
fb90860
6123e7a
c99a693
ee7cabc
40d31e3
89e5499
a1fab95
92626bd
8c4d2dd
7ccca59
2e9bafc
f14d489
e4495b8
80f0061
964e142
b4733da
8ea10ec
bdc977a
5cb0e26
deaabe7
b20bb1a
15421a7
b4f9d9d
b9f750d
073dc07
da2f41e
e611fe7
e453d2c
45b5e68
fa06713
cd33370
5bfe2d1
6c0566f
e1bedf1
60a3e74
a4566cf
010e1ad
e35ea34
d04104d
cf3de20
f1eb3a4
c70613a
eaf75e9
fb71278
a05d1fb
04fb332
4382fcc
4e8892c
7ca0171
eefea36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,94 @@ | ||||||
| import os | ||||||
| import subprocess | ||||||
| import json | ||||||
| import math | ||||||
| import sys | ||||||
|
|
||||||
| def get_packages(): | ||||||
| subdirs = ['packages'] | ||||||
| packages = [] | ||||||
| for subdir in subdirs: | ||||||
| if not os.path.exists(subdir): | ||||||
| continue | ||||||
| # Use the same sorting as the shell script | ||||||
| pkg_dirs = [os.path.join(subdir, d) + '/' for d in os.listdir(subdir) if os.path.isdir(os.path.join(subdir, d))] | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Using a consistent forward slash format for package paths avoids this issue.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to filter out hidden packages?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure, do we? I thought all packages have unit tests. Which ones would be candidates to filter out? |
||||||
| packages.extend(sorted(pkg_dirs)) | ||||||
| return packages | ||||||
|
|
||||||
| def get_packages_to_test(): | ||||||
| build_type = os.environ.get('BUILD_TYPE', 'presubmit') | ||||||
| target_branch = os.environ.get('TARGET_BRANCH', 'main') | ||||||
|
|
||||||
| all_packages = get_packages() | ||||||
|
|
||||||
| if build_type == 'presubmit': | ||||||
| git_diff_arg = f"origin/{target_branch}..." | ||||||
| elif build_type == 'continuous': | ||||||
| git_diff_arg = "HEAD~.." | ||||||
| else: | ||||||
| return all_packages | ||||||
|
|
||||||
| # Check if ci/ changed | ||||||
| try: | ||||||
| subprocess.check_call(['git', 'diff', '--quiet', git_diff_arg, 'ci']) | ||||||
| ci_changed = False | ||||||
| except subprocess.CalledProcessError: | ||||||
| ci_changed = True | ||||||
|
|
||||||
| if ci_changed: | ||||||
| return all_packages | ||||||
|
|
||||||
| try: | ||||||
| res = subprocess.check_output(['git', 'diff', '--name-only', git_diff_arg]).decode('utf-8') | ||||||
| changed_files = res.splitlines() | ||||||
| except subprocess.CalledProcessError: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a brief comment here explaining the fallback behaviour?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||||||
| return all_packages | ||||||
|
|
||||||
| to_test = [] | ||||||
| for pkg in all_packages: | ||||||
| # Check if any changed file starts with the package path | ||||||
| if any(f.startswith(pkg) for f in changed_files): | ||||||
| to_test.append(pkg) | ||||||
|
|
||||||
| return to_test | ||||||
|
|
||||||
| def group_packages(packages, max_packages_per_shard=25, max_total_shards=20): | ||||||
| if not packages: | ||||||
| return [] | ||||||
|
|
||||||
| num_packages = len(packages) | ||||||
|
|
||||||
| # Calculate number of shards based on packages per shard | ||||||
| num_shards = math.ceil(num_packages / max_packages_per_shard) | ||||||
|
|
||||||
| # Cap the total number of shards | ||||||
| num_shards = min(num_shards, max_total_shards) | ||||||
|
|
||||||
| # Recalculate shard size to be as even as possible given the capped shards | ||||||
| shard_size = math.ceil(num_packages / num_shards) | ||||||
|
|
||||||
| shards = [] | ||||||
| for i in range(num_shards): | ||||||
| start = i * shard_size | ||||||
| end = min((i + 1) * shard_size, num_packages) | ||||||
| if start >= num_packages: | ||||||
| break | ||||||
|
|
||||||
| shard_packages = packages[start:end] | ||||||
| if len(shard_packages) == 1: | ||||||
| name = shard_packages[0].strip('/').split('/')[-1] | ||||||
| else: | ||||||
| name = f"{shard_packages[0].strip('/').split('/')[-1]}...{shard_packages[-1].strip('/').split('/')[-1]}" | ||||||
|
|
||||||
| shards.append({ | ||||||
| "name": name, | ||||||
| "index": i + 1, | ||||||
| "packages": " ".join(shard_packages) | ||||||
| }) | ||||||
| return shards | ||||||
|
|
||||||
| if __name__ == "__main__": | ||||||
| packages = get_packages_to_test() | ||||||
| # Shard into groups of ~25 libraries, up to 20 parallel jobs | ||||||
| shards = group_packages(packages, max_packages_per_shard=25, max_total_shards=20) | ||||||
| print(json.dumps(shards)) | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test change for sharding |
Uh oh!
There was an error while loading. Please reload this page.