plasma-umass / scalene

Scalene: a high-performance, high-precision CPU, GPU, and memory profiler for Python with AI-powered optimization proposals
Apache License 2.0
11.63k stars 388 forks source link

pytest plugin #70

Open johanvergeer opened 4 years ago

johanvergeer commented 4 years ago

Is your feature request related to a problem? Please describe. We already have a big test suite that could be used directly to profile specific functionality. I have not found a way to run Scalene with Pytest.

Describe the solution you'd like It would be really cool when a Pytest plugin could be created that allows us to run a (set of) tests with Scalene running in the background. I could come up with two solutions:

The first solution is to run pytest trough Scalene

scalene pytest test_mod.py

The second solution is to add a flag to pytest that runs Scalene.

pytest --scalene test_mod.py

The latter would have my preference, since that would allow us to have more fine-grained tests and it would allow us to add test configurations in an IDE (like PyCharm).

Describe alternatives you've considered

At this moment, the only alternative that I could come up with is to create a separate script that runs the code just for profiling.

pearu commented 3 years ago

Inserting

if __name__ == '__main__':
    import sys, pytest
    pytest.main(sys.argv)

to a test script (say, test_mod.py) allows running scalene on the pytest compatible test functions. For instance:

scalene --profile-all test_mod.py -sv -x

In general, to invoke scalene, the following pattern appears to apply:

scalene <scalene options> /path/to/test_mod.py <pytest options>
kingjin94 commented 1 year ago

I found this way to run a single test case: scalene -m test_case_file -k TestCaseClass.test_case_function thanks to ChatGPT :sweat_smile: not sure if this is documented anywhere.