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

Infrastructure for memory usage tests #299

Merged
merged 16 commits into from
Oct 14, 2024

Conversation

emolter
Copy link
Collaborator

@emolter emolter commented Oct 7, 2024

This PR adds a context manager to more easily check the peak memory usage of a code block and raise an error if a memory limit is exceeded. A unit test of the memory usage in the median calculation in outlier detection provides an example use-case.

Relates to JP-3775 but only fixes one small part of that ticket.

Tasks

  • update or add relevant tests
  • update relevant docstrings and / or docs/ page
  • Does this PR change any API used downstream? (if not, label with no-changelog-entry-needed)
    • write news fragment(s) in changes/: echo "changed something" > changes/<PR#>.<changetype>.rst (see below for change types)
    • run regression tests with this branch installed ("git+https://github.com/<fork>/stcal@<branch>")
news fragment change types...
  • changes/<PR#>.apichange.rst: change to public API
  • changes/<PR#>.bugfix.rst: fixes an issue
  • changes/<PR#>.general.rst: infrastructure or miscellaneous change

@emolter emolter marked this pull request as ready for review October 7, 2024 15:14
@emolter emolter requested a review from a team as a code owner October 7, 2024 15:14
@emolter emolter requested a review from tapastro October 7, 2024 15:17
Copy link

codecov bot commented Oct 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.30%. Comparing base (60bd3b8) to head (12c5886).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #299      +/-   ##
==========================================
+ Coverage   86.21%   86.30%   +0.09%     
==========================================
  Files          47       49       +2     
  Lines        8812     8870      +58     
==========================================
+ Hits         7597     7655      +58     
  Misses       1215     1215              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@braingram braingram left a comment

Choose a reason for hiding this comment

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

Since the parent ticket JP-3775 is for building out infrastructure for tests like this, what do you think about building out that infrastructure and using it here? Something like:

with memory_threshold(expected_mem):
    computer = MedianComputer...

I can understand that the infrastructure is beyond the scope of this PR but I think an argument can be made that building the infrastructure first (before this PR) makes sense since:

  • this is a useful test for the infrastructure
  • if we merge this PR first a follow-up PR will modify this test again when we have the new infrastructure

tests/outlier_detection/test_median.py Outdated Show resolved Hide resolved
tests/outlier_detection/test_median.py Outdated Show resolved Hide resolved
@emolter
Copy link
Collaborator Author

emolter commented Oct 8, 2024

I can understand that the infrastructure is beyond the scope of this PR but I think an argument can be made that building the infrastructure first (before this PR) makes sense

I completely agree. I'll work on making a context manager that is called like you show

@emolter emolter marked this pull request as draft October 8, 2024 21:16
@emolter
Copy link
Collaborator Author

emolter commented Oct 9, 2024

@braingram I added the context manager and a fixture that calls it. I wasn't sure exactly how to structure this, because based on what I read it's not great practice to import from conftest.py directly. Let me know if the proposed helper.py and conftest.py look right to you.

Also, do you think it's necessary/useful to add infrastructure for testing the fixture itself like in this StackOverflow answer?

@braingram
Copy link
Collaborator

@braingram I added the context manager and a fixture that calls it. I wasn't sure exactly how to structure this, because based on what I read it's not great practice to import from conftest.py directly. Let me know if the proposed helper.py and conftest.py look right to you.

Also, do you think it's necessary/useful to add infrastructure for testing the fixture itself like in this StackOverflow answer?

Since the fixture isn't needed for this test I'd say we leave it out of this PR. The class you added in tests.helpers seems to cover all the needs for the added test. I'll leave a few other comments inline.

tests/helpers.py Outdated Show resolved Hide resolved
tests/helpers.py Outdated Show resolved Hide resolved
tests/helpers.py Outdated Show resolved Hide resolved
@emolter emolter marked this pull request as ready for review October 9, 2024 16:11
@emolter emolter requested a review from braingram October 9, 2024 16:11
@emolter emolter changed the title Unit test for median memory usage Infrastructure for memory usage tests Oct 9, 2024
@emolter emolter requested a review from penaguerrero October 9, 2024 16:37
Copy link
Collaborator

@braingram braingram left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM.

@emolter
Copy link
Collaborator Author

emolter commented Oct 10, 2024

status update: @penaguerrero is using this to write a test for the jump step, and in so doing is evaluating this PR. So this is awaiting her review

Copy link
Collaborator

@penaguerrero penaguerrero left a comment

Choose a reason for hiding this comment

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

I tried the code in writing a test for jwst, and it works as intended. Other than suggesting to allow to present the result in other units I have no further comments. LGTM

src/stcal/testing_helpers.py Outdated Show resolved Hide resolved
@emolter emolter requested a review from penaguerrero October 14, 2024 13:31
@emolter
Copy link
Collaborator Author

emolter commented Oct 14, 2024

@penaguerrero can you test the units option I added and see if it covers your use-case?

Copy link
Collaborator

@penaguerrero penaguerrero left a comment

Choose a reason for hiding this comment

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

I just tested again with the changes. It works as expected. LGTM!

@emolter emolter merged commit dfe1d6d into spacetelescope:main Oct 14, 2024
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants