Slowdown of pytest collection in sqlalchemy in python 3.9+
See original GitHub issue- a detailed description of the bug or problem you are having
- output of
pip list
from the virtual environment you are using - pytest and operating system versions
- minimal example if possible
We noticed that from python 3.9 pytest takes a significantly more time (~4x) to collect the tests for sqlalchemy. You can run the following docker to compare. python 3.7 and 3.8 are ok (not fast not slow), python 3.9 and 3.10 are noticeably slower
docker run -ti --name py --rm python:3.7 bash -c 'git clone https://github.com/sqlalchemy/sqlalchemy.git --depth 100; pip install pytest; cd sqlalchemy; time pytest --collect-only'
docker run -ti --name py --rm python:3.8 bash -c 'git clone https://github.com/sqlalchemy/sqlalchemy.git --depth 100; pip install pytest; cd sqlalchemy; time pytest --collect-only'
docker run -ti --name py --rm python:3.9 bash -c 'git clone https://github.com/sqlalchemy/sqlalchemy.git --depth 100; pip install pytest; cd sqlalchemy; time pytest --collect-only'
docker run -ti --name py --rm python:3.10 bash -c 'git clone https://github.com/sqlalchemy/sqlalchemy.git --depth 100; pip install pytest; cd sqlalchemy; time pytest --collect-only'
the times on my pc are as floows:
#py37
real 0m27.949s
user 0m26.717s
sys 0m1.220s
#py38
real 0m27.952s
user 0m26.893s
sys 0m1.050s
# py39
real 1m45.581s
user 1m42.574s
sys 0m2.990s
# py310
real 1m46.255s
user 1m43.648s
sys 0m2.590s
cc @zzzeek
Issue Analytics
- State:
- Created 2 years ago
- Comments:31 (14 by maintainers)
Top Results From Across the Web
[prerelease] Performance regression? · Issue #9478 · pytest ...
Most of the slowdowns seem to come from posix.stat - on pytest 6, ... Tests collection time went from 32s (6.2.5) to 2.3s...
Read more >Setting up transactional tests with Pytest and SQLAlchemy
SQLAlchemy offers methods to easily create and drop tables declared in the schema: create_all and drop_all . We will use them at the...
Read more >SQLAlchemy ORM function works in main script but fails in ...
Using Postgres, SQLAlchemy 1.4 ORM, Python 3.7, Pytest. I have a script in myproject/src/db.py and the tests for it are located in myproject/ ......
Read more >Speeding Up Python with Concurrency, Parallelism, and asyncio
What are concurrency and parallelism, and how do they apply to Python? There are many reasons your applications can be slow.
Read more >A Guide To Database Unit Testing with Pytest and SQLAlchemy
We will discuss how to do unit testing using pytest, which is a Python testing tool. This tool has useful features to help...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok, I had to use
hookwrapper
inpytest_runtest_teardown
since that finalizer needs to run after all the class fixture ones, but it seems to be working,The last version is on gerrit https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3332
edit, yes the test all pass.
Having the previous behaviour of addfinalizer would be nice to have, but I think this can be closed. Thanks a lot for all the support and help!
We are planning a release candidate pretty soon, likely within the week, so shouldn’t be far.
The traceback shows
get_test_class_from_fn
, does that function returnpytest.Class
both in v6 and v7? If so things should just work, you should be able to attach finalizers to classes.I don’t seeget_test_class_from_fn
in the link, was that removed afterwards?Ahh I see it in Gerrit. You actually don’t need that function,
item.getparent(pytest.Class)
will return the class for that item, or None in case it doesn’t have a class (it is a free function). And this will work in v6 and v7.Same is valid for other places like this:
Which can be written as:
item.getparent(pytest.Module)
will always return apytest.Module
of course.