Skip to content
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

Default SizeRange too large #320

Closed
nipunn1313 opened this issue May 2, 2023 · 3 comments · Fixed by #338
Closed

Default SizeRange too large #320

nipunn1313 opened this issue May 2, 2023 · 3 comments · Fixed by #338
Labels
quality-of-life This issue proposes a change that will improve the UX of proptest but isn't necessarily a "feature"

Comments

@nipunn1313
Copy link
Contributor

Hi proptest community. I'm finding that the default size range of 0..100 is too large, and is ending up broadly taking up most of the time in our testsuite (generating collections).

Would proptest be amenable to making this default range something that's adjustable? I would probably set it to 10 or 20. Perhaps by environment variable. It would be nice to avoid littering the codebase with strategy overrides for basic Vec.

https://docs.rs/proptest/latest/proptest/collection/struct.SizeRange.html

I am happy to submit a PR.
I'm also open to other solution ideas for this problem.

Thanks! Big fan

@rex-remind101
Copy link
Collaborator

I have no objections to configuring this via the environment. @matthew-russo @cameron1024 thoughts?

@matthew-russo
Copy link
Member

Sorry for the delay in getting to this -- do you have an example of whats problematic?

generating Vecs of specific lengths can just be done with

proptest::collections::vec(any::<T>(), 0..10)

or with imports it cuts down to

vec(any::<T>(), 0..10)

which is pretty terse. the range could be extracted out in to a const so its not hardcoded everywhere as well.

i get the feeling i'm not understanding part of this though

@nipunn1313
Copy link
Contributor Author

Yep that's exactly the workaround to override default settings.

I'll give an example from our codebase.

#[derive(Debug, Arbitrary)]
struct TestQuery {
    #[proptest(strategy = "prop::collection::vec(any::<TestValue>(), 0..4)")]
    search: Vec<TestValue>,
    filter: Option<TestValue>,
}

The default settings for us routinely lead to tests that spend majority of time in test-case-generation of large Vecs. I've seen such changes reduce individual test time from 20s to <1s w/ minimal impact to test quality (vec of 10 is plenty).

We tend to use the default settings most of the time and override them when they are non-performant. It is particularly likely to become non-performant in a nested collection scenario (eg if the TestValue struct also contains a vec in the example above).

When I grep our codebase (100k loc) for this pattern, I found 32 instances of this sort of pattern, mostly from us whack-a-moling tests that are too slow with default settings. Many more don't use this pattern. (I count ~150 #[derive(Arbitrary)] in the codebase).

@matthew-russo is certainly right - we could do a pass over the codebase and train everyone (or build a sophisticated linter) to stick a #[proptest(strategy = "prop::collection::vec(any::<T>(), 0..4)")] or similar in every spot, but there's gotta be a better way. Perhaps you might have a better idea? I am curious.

Thanks for the work on maintaining the project! We're fans of randomized testing.

@matthew-russo matthew-russo added the quality-of-life This issue proposes a change that will improve the UX of proptest but isn't necessarily a "feature" label Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
quality-of-life This issue proposes a change that will improve the UX of proptest but isn't necessarily a "feature"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants