Repository navigation
Commit 23e68ea
perf(tests): skip stack walk in silo transaction validation unless it would raise (#127937)
`validate_transaction_using_for_silo_mode` runs on every
`transaction.atomic` / `on_commit` / `get_connection` call. In tests it
called `is_in_test_case_body()` first, which `str()`s every stack frame
and runs regexes over them. That walk happened about 500 times per test,
even though its result only matters when the silo check would raise.
This PR:
- Runs the cheap silo/`using` checks first and walks the stack only on a
mismatch, just before raising. The function raises in exactly the same
cases as before. Production behavior doesn't change, because the stack
walk only ran under `in_test_environment()`.
- Changes `is_in_test_case_body()` to compare `co_name`/`co_filename` on
`sys._getframe()` frames instead of using `str(frame)` + regex. It
matches the same frames.
**Evidence**
- cProfile on
`tests/sentry/api/bases/test_organization.py::GetProjectIdsTest` (25
tests): `validate_transaction_using_for_silo_mode` went from 5.7s to
0.43s cumulative, and `is_in_test_case_body` no longer runs. The call
phase went from 27.5s to 20.3s.
- Local `tests/sentry/api/bases/`: 54.3s to 45.6s (avg of 2 runs).
- Full backend suite on CI (workflow_dispatch, all 22 shards, 46,855
tests, green): the sum of per-test setup+call+teardown went from 26,753s
to 23,775s (-11%). Runner speed varies by up to about 2.5x between
shards, so I normalized by each shard's collection time, which this
change doesn't touch. The per-shard median is -9%. Postgres-heavy
directories like `tests/sentry/api/` and `tests/sentry/integrations/`
drop 15-17%. Snuba-heavy ones drop about 3%.
- `tests/sentry/silo/test_silo_aware_transaction_patch.py` passes,
including the mismatch-raises cases.
<!-- junior-request-attribution:start -->
via @joshuarli.
<!-- junior-request-attribution:end -->
<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3ACUHS29QJ0%3A1792248286.713619 -->
--
[View Junior
Session](https://junior-prod.sentry.dev/conversations/slack%3ACUHS29QJ0%3A1792248286.713619)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3ACUHS29QJ0%3A1792248286.713619/?project=4510944073809921)
<!-- junior-session-footer:end -->
---------
Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Joshua Li <14209781+joshuarli@users.noreply.github.com>1 parent ccfc7ab commit 23e68ea
1 file changed
Lines changed: 41 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
| 2 | + | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
87 | 100 | | |
| 101 | + | |
88 | 102 | | |
89 | 103 | | |
90 | 104 | | |
| |||
94 | 108 | | |
95 | 109 | | |
96 | 110 | | |
97 | | - | |
98 | | - | |
99 | | - | |
| 111 | + | |
| 112 | + | |
100 | 113 | | |
101 | 114 | | |
102 | | - | |
103 | 115 | | |
104 | 116 | | |
| 117 | + | |
| 118 | + | |
105 | 119 | | |
106 | | - | |
107 | | - | |
108 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
109 | 125 | | |
110 | 126 | | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
115 | 132 | | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
| 133 | + | |
120 | 134 | | |
121 | 135 | | |
122 | 136 | | |
| |||
0 commit comments