Skip to content

⚡️ Speed up test suite via caching and fixture scopes to make it ~24% faster#13583

Merged
tiangolo merged 5 commits into
fastapi:masterfrom
dikos1337:feat/speed-up-tests
May 23, 2026
Merged

⚡️ Speed up test suite via caching and fixture scopes to make it ~24% faster#13583
tiangolo merged 5 commits into
fastapi:masterfrom
dikos1337:feat/speed-up-tests

Conversation

@dikos1337
Copy link
Copy Markdown
Contributor

@dikos1337 dikos1337 commented Apr 3, 2025

While running the tests, I noticed a couple of spots where we could speed things up a bit based on profiling.

  1. Caching verify_password:

    • The verify_password helper (using bcrypt) can be a bit slow, and I saw it was getting called with the same inputs multiple times during the test run.
    • I added @lru_cache() to it. Since the results are always the same for the same inputs, this is safe.
    • In my run, this cut its calls from ~70 down to ~40, and its share of the total test time dropped from ~26.9% to ~16.2%.
  2. Optimizing Fixture Scopes:

    • I also saw two fixtures that were being set up and torn down a lot (like 166 and 86 times!), taking up around 8.7% and 4.9% of the total test time respectively.
    • Since they didn't seem to need a completely fresh state for every single test function, I changed their scope to scope="module".
    • Now they run way less often, and their time usage basically disappeared from the profile.

Overall Speedup:

The profiling breakdown suggested these specific parts contributed to a relative reduction of ~24.3% of the original time.

Actual measured impact: Running the full test suite (2334 passed, 130 skipped) on my machine took 24.37s before these changes and now takes 16.85s.

That's a time saving of 7.52s, resulting in an overall test suite speedup of ~30.9% ((24.37 - 16.85) / 24.37 * 100%).

Hopefully, this makes the test suite noticeably faster for everyone! Let me know what you think. 🙂

image

image

image

@github-actions github-actions Bot added the docs Documentation about how to use FastAPI label Apr 3, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 3, 2025

📝 Docs preview for commit d37b0d5 at: https://74ae3147.fastapitiangolo.pages.dev

@dikos1337 dikos1337 force-pushed the feat/speed-up-tests branch from d37b0d5 to dc196e9 Compare April 3, 2025 17:44
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 3, 2025

📝 Docs preview for commit dc196e9 at: https://42a11227.fastapitiangolo.pages.dev

@alv2017
Copy link
Copy Markdown
Contributor

alv2017 commented Apr 3, 2025

  1. Caching verify_password:

Introduces a security vulnerability into the application.

@svlandeg svlandeg changed the title Speed up test suite via caching and fixture scopes (~31% faster) ⚡️ Speed up test suite via caching and fixture scopes to make it ~31% faster Apr 4, 2025
Copy link
Copy Markdown
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @dikos1337! Definitely appreciate you trying to make the test suite more efficient 🙏

With respect to @alv2017's comment, about the security of using @lru_cache for verify_password. I'm also unsure about this change: even if we would consider the usage safe in the context of running our test suite, we also have to consider that these code snippets are used verbatim in the tutorial (e.g. oauth2-scopes), and as such we need to consider whether this is the "best practices" approach we want to show users.

Another important point: by adding lines to the tutorialXXX.py files, the highlighting of specific lines in the tutorial will be wrong. For instance, on the oauth2-scopes link above you can see that e.g. the lines around the get_current_user function are highlighted wrongly (compare with the current version)).

@svlandeg svlandeg marked this pull request as draft April 4, 2025 09:36
@dikos1337
Copy link
Copy Markdown
Contributor Author

dikos1337 commented Apr 4, 2025

@svlandeg I think I found an elegant way to fix this.

@dikos1337 dikos1337 force-pushed the feat/speed-up-tests branch from dc196e9 to 8e62206 Compare April 5, 2025 07:06
@dikos1337 dikos1337 marked this pull request as ready for review April 5, 2025 07:11
@dikos1337 dikos1337 requested a review from svlandeg April 5, 2025 07:11
@dikos1337 dikos1337 changed the title ⚡️ Speed up test suite via caching and fixture scopes to make it ~31% faster ⚡️ Speed up test suite via caching and fixture scopes to make it ~24% faster Apr 5, 2025
@alv2017
Copy link
Copy Markdown
Contributor

alv2017 commented Apr 5, 2025

@dikos1337, which tool were you using to measure the performance of the tests?

@dikos1337
Copy link
Copy Markdown
Contributor Author

@alv2017 pytest-profiling

@svlandeg svlandeg removed their request for review April 7, 2025 09:16
@svlandeg
Copy link
Copy Markdown
Member

svlandeg commented Apr 7, 2025

@svlandeg I think I found an elegant way to fix this.

Could you be more specific about which parts of my comments you have addressed? Did you see my comment on line highlighting?

@dikos1337
Copy link
Copy Markdown
Contributor Author

dikos1337 commented Apr 7, 2025

Could you be more specific about which parts of my comments you have addressed? Did you see my comment on line highlighting?

@svlandeg I don't quite understand what's wrong with highlighting now. I don't make any changes to the documentation anymore. Can you show a screenshot of the problem?

@svlandeg
Copy link
Copy Markdown
Member

svlandeg commented Apr 7, 2025

I'm actually having a hard time reviewing the changes because you force pushed changes, meaning that I can't see the original commits anymore, or exactly what changed inbetween versions and inbetween comments. It also looks like your original post is unchanged and doesn't reflect the current state of the PR anymore, and the only comment you gave was that you found a way to fix "this" - but I don't even know what specifically "this" refers to.

So let's please take a step back, address the comments on the PR as they were and explain your current changes and their implications? And update the outdated information in the PR? 🙏

E.g. is the speedup still 24%? etc.

@dikos1337
Copy link
Copy Markdown
Contributor Author

dikos1337 commented Apr 7, 2025

@svlandeg The original post is fully consistent with the changes in the current pull request, so I did not change it.

"this" refers to both the issues that the use of @lru_cache was visible in the tutorial and the highlighting of specific lines in the tutorial was wrong. In the latest changes I don't change the code in the tutorial, so it's not an issue anymore, the tutorial remains unchanged.

I changed the speedup percentage from 31% to 24% in the pull request name because it matches the calculations based on the profiler data, and I also compared the speed of running tests in the current pull request and in other pull requests and it is actually closer to 24% than 31%, but it also depends on the power of the processor, the more powerful the more increase, on my machine the increase is 31%.

P.S. no more force pushes from me 🤝

@dikos1337 dikos1337 requested a review from svlandeg April 7, 2025 19:50
@svlandeg svlandeg self-assigned this Apr 8, 2025
@svlandeg svlandeg removed their request for review April 8, 2025 08:03
Copy link
Copy Markdown
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look fine to me, but I'll defer to Tiangolo for a final review. Thanks again for the contribution and explanations, @dikos1337 !

@svlandeg svlandeg removed their assignment Apr 10, 2025
@nidhishgajjar

This comment was marked as spam.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 23, 2026

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing dikos1337:feat/speed-up-tests (40a14dc) with master (973230c)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (4f37a43) during the generation of this report, so 973230c was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@tiangolo tiangolo added internal and removed docs Documentation about how to use FastAPI labels May 23, 2026
Copy link
Copy Markdown
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you! 🚀

And thanks for the review @svlandeg 🙌

@tiangolo tiangolo merged commit 8b647e3 into fastapi:master May 23, 2026
38 checks passed
@dikos1337
Copy link
Copy Markdown
Contributor Author

dikos1337 commented May 23, 2026

@tiangolo Thanks for merging my PR. I wanted to point out that apparently, since the PR was created, mod.engine.dispose() was added to the code. I also added mod.engine.dispose() in this PR, so now it's being called twice.

@tiangolo
Copy link
Copy Markdown
Member

Oh, dang, I'll check it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants