-
-
Notifications
You must be signed in to change notification settings - Fork 51
pytest test sharding #493
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
pytest test sharding #493
Conversation
I haven't had time to study this yet, note I had #401 which may or may not overlap. |
I did see #401 , but there hadn't been activity on it in a while. I think this could still be merged in the meantime. If you do want to commit it with #401 instead, I think it would still be worth while to vendor in the pytest-shard plugin with the round-robin patch, always loading it, since its a better UX for developers. |
round-robin is not the best strategy, because it tends to mean a shard has to do many different setup routines. Take as example (invented from what I remember of JUnit, but I think it applies)
If this is meant to have --shard_count=2, you'd rather split the test cases IIUC, round robin means you'll have both shards having to run all three setups. We did test sharding in Jasmine, https://github.com/aspect-build/rules_jasmine/blob/main/jasmine/private/runner.cjs#L32-L53 where we sort and then partition them |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks for bringing this in
@alexeagle I think that is less of a problem when you only have one test file per If I'm understanding your explanation correctly, instead of a round robin, we should just take the full list [i, ii, iii, iv, v, vi] and partition it by the number of shards, keeping the default ordering (n=3, [i, ii], [iii, iv], [v, vi]). Does that sound right? I did look at rules_jvm's implementation and they use hashes, so they don't take into account any ordering. |
Yes that's exactly right. Obviously this makes a difference for tests with heavy fixtures, with a larger number of test cases and a larger shard count the more we can "group by fixture" the less duplicate work is performed. I think rules_jvm probably has this wrong too, but I'm pretty sure the google3 implementation gets it right. |
my 2c: |
Sure, it's always possible to leave a TODO - but you have to appreciate that typically on OSS projects you have a contributor who's on the hook to land the change, and later is likely to disappear. As the maintainer you then end up with that tech debt on your own head. So we're usually a bit defensive about making it right at the beginning. I could trust you guys to come back to this optimization later though. |
Looks like a new change in https://github.com/aspect-build/rules_py/tree/main/examples/virtual_deps broke these changes. Investigating. |
|
Adds test sharding support to the pytest runner.
I took some inspiration from https://github.com/caseyduquettesc/rules_python_pytest . In particular, I used it as a reference for which Bazel environment variables need to be pulled in the shim.
The pytest plugin is a derivative work of https://github.com/AdamGleave/pytest-shard . That dependency hasn't been updated in a long time, and the sharding was based off hashing which often results in unbalanced shards or errors from empty shards. I vendored the code into the repo, modified it to use a plain round robin strategy for selection, and converted the code to be class based so it can be used with
pytest.main()
.Test plan
I added an example to the examples directory and I tested it with my own repository here: https://github.com/vinnybod/bazel-examples/blob/python-test-sharding/test-shard-python/BUILD.bazel