bevy/examples/stress_tests
Alice Cecile 2ad5908e58
Make Query::single (and friends) return a Result (#18082)
# Objective

As discussed in #14275, Bevy is currently too prone to panic, and makes
the easy / beginner-friendly way to do a large number of operations just
to panic on failure.

This is seriously frustrating in library code, but also slows down
development, as many of the `Query::single` panics can actually safely
be an early return (these panics are often due to a small ordering issue
or a change in game state.

More critically, in most "finished" products, panics are unacceptable:
any unexpected failures should be handled elsewhere. That's where the
new

With the advent of good system error handling, we can now remove this.

Note: I was instrumental in a) introducing this idea in the first place
and b) pushing to make the panicking variant the default. The
introduction of both `let else` statements in Rust and the fancy system
error handling work in 0.16 have changed my mind on the right balance
here.

## Solution

1. Make `Query::single` and `Query::single_mut` (and other random
related methods) return a `Result`.
2. Handle all of Bevy's internal usage of these APIs.
3. Deprecate `Query::get_single` and friends, since we've moved their
functionality to the nice names.
4. Add detailed advice on how to best handle these errors.

Generally I like the diff here, although `get_single().unwrap()` in
tests is a bit of a downgrade.

## Testing

I've done a global search for `.single` to track down any missed
deprecated usages.

As to whether or not all the migrations were successful, that's what CI
is for :)

## Future work

~~Rename `Query::get_single` and friends to `Query::single`!~~

~~I've opted not to do this in this PR, and smear it across two releases
in order to ease the migration. Successive deprecations are much easier
to manage than the semantics and types shifting under your feet.~~

Cart has convinced me to change my mind on this; see
https://github.com/bevyengine/bevy/pull/18082#discussion_r1974536085.

## Migration guide

`Query::single`, `Query::single_mut` and their `QueryState` equivalents
now return a `Result`. Generally, you'll want to:

1. Use Bevy 0.16's system error handling to return a `Result` using the
`?` operator.
2. Use a `let else Ok(data)` block to early return if it's an expected
failure.
3. Use `unwrap()` or `Ok` destructuring inside of tests.

The old `Query::get_single` (etc) methods which did this have been
deprecated.
2025-03-02 19:51:56 +00:00
..
bevymark.rs Make Query::single (and friends) return a Result (#18082) 2025-03-02 19:51:56 +00:00
many_animated_sprites.rs Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
many_buttons.rs Split the labels in many_buttons (#17802) 2025-02-11 18:29:06 +00:00
many_cameras_lights.rs Make Query::single (and friends) return a Result (#18082) 2025-03-02 19:51:56 +00:00
many_components.rs many_components stress test improvements (#16913) 2025-02-10 21:13:57 +00:00
many_cubes.rs Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
many_foxes.rs Allow users to customize history length in FrameTimeDiagnosticsPlugin (#17259) 2025-01-12 18:18:14 +00:00
many_gizmos.rs Allow users to customize history length in FrameTimeDiagnosticsPlugin (#17259) 2025-01-12 18:18:14 +00:00
many_glyphs.rs Allow users to customize history length in FrameTimeDiagnosticsPlugin (#17259) 2025-01-12 18:18:14 +00:00
many_lights.rs Allow users to customize history length in FrameTimeDiagnosticsPlugin (#17259) 2025-01-12 18:18:14 +00:00
many_materials.rs Add many_materials stress test (#17346) 2025-01-24 05:46:23 +00:00
many_sprites.rs Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
many_text2d.rs Make Query::single (and friends) return a Result (#18082) 2025-03-02 19:51:56 +00:00
README.md Add a stress test profile (#6901) 2022-12-20 15:59:41 +00:00
text_pipeline.rs Allow users to customize history length in FrameTimeDiagnosticsPlugin (#17259) 2025-01-12 18:18:14 +00:00
transform_hierarchy.rs Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
warning_string.txt Added performance warning when running stress test examples in debug mode (#5029) 2022-07-13 19:13:46 +00:00

Stress tests

These examples are used to stress test Bevy's performance in various ways. These should be run with the "stress-test" profile to accurately represent performance in production, otherwise they will run in cargo's default "dev" profile which is very slow.

Example Command

cargo run --profile stress-test --example <EXAMPLE>