Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory usage assertions in tests #3

Open
itamarst opened this issue Jul 20, 2024 · 1 comment
Open

Memory usage assertions in tests #3

itamarst opened this issue Jul 20, 2024 · 1 comment

Comments

@itamarst
Copy link

itamarst commented Jul 20, 2024

It's easy to have high memory usage without noticing, or to add regressions that increase memory usage. As a developer of a library, I would like to ensure my code doesn't use too much memory; making assertions about memory usage in tests is one way to do this.

The key measure is peak or high water mark memory usage. The memory usage pattern in scientific computing involves spiky large allocations, and the peak is the bottleneck that will drive hardware requirements.

Unlike performance benchmarking, memory measurements can be integrated into existing tests as an additional assertion. It does not require a special setup. So this is actually pretty easy technically, the main constraint is education and cultural norms around what counts as best practices.

High memory usage is a lot harder to notice than slow code, because until you hit a certain threshold and start swapping it doesn't have visible symptoms. But it has a significant financial cost at scale. For an example of regression see e.g. pola-rs/polars#15098: someone introduced a bug and never noticed because main symptom was higher memory usage.

@itamarst
Copy link
Author

itamarst commented Jul 20, 2024

How to measure memory in a test

For libraries that use Python's memory allocator APIs out of the box, tracemalloc from the Python standard library is all you need. See e.g. https://github.com/pola-rs/polars/blob/main/py-polars/tests/unit/conftest.py#L169 for a sample pytest fixture.

For libraries that use other memory allocators, options include:

@itamarst itamarst changed the title Memory benchmarking Memory usage assertions in tests Jul 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant