question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Comments:31 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
CaselITcommented, Nov 27, 2021

Ok, I had to use hookwrapper in pytest_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!

1reaction
nicoddemuscommented, Nov 27, 2021

alternatively is there an approximate date you are aiming towards for v7?

We are planning a release candidate pretty soon, likely within the week, so shouldn’t be far.

We also tripping an assert while adding a finalizer

The traceback shows get_test_class_from_fn, does that function return pytest.Class both in v6 and v7? If so things should just work, you should be able to attach finalizers to classes.

are are trying to add it in pytest_runtest_setup here

I don’t see get_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:

    items[:] = sorted(
        newitems,
        key=lambda item: (
            get_test_class_from_fn(item).parent.name,
            get_test_class_from_fn(item).name,
            item.name,
        ),
    )

Which can be written as:

    items[:] = sorted(
        newitems,
        key=lambda item: (
            item.getparent(pytest.Module).name,
            item.getparent(pytest.Class).name,
            item.name,
        ),
    )

item.getparent(pytest.Module) will always return a pytest.Module of course.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found