-
Notifications
You must be signed in to change notification settings - Fork 235
Improve performance by avoiding loading the GMT library repeatedly #2930
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
Changes from 4 commits
51f3943
713b0e9
6936f59
f589ff8
401a0f8
48c00ae
b31fe19
ce263ec
820e11b
7a2d96d
bf6083f
11cb76b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| import pytest | ||
| from pygmt.clib.loading import check_libgmt, clib_full_names, clib_names, load_libgmt | ||
| from pygmt.clib.session import Session | ||
| from pygmt.exceptions import GMTCLibError, GMTCLibNotFoundError, GMTOSError | ||
|
|
||
|
|
||
|
|
@@ -208,6 +209,45 @@ def test_brokenlib_brokenlib_workinglib(self): | |
| assert check_libgmt(load_libgmt(lib_fullnames=lib_fullnames)) is None | ||
|
|
||
|
|
||
| class TestLibgmtCount: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe better to put this unit test in
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer to keep it in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I kinda wanted to move the test since |
||
| """ | ||
| Test that the GMT library is not repeatly loaded in every session. | ||
seisman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
|
|
||
| loaded_libgmt = load_libgmt() # Load the GMT library and reuse it when necessary | ||
| counter = 0 # Count how many times ctypes.CDLL is called | ||
|
|
||
| def _mock_ctypes_cdll_return(self, libname): # noqa: ARG002 | ||
| """ | ||
| Mock ctypes.CDLL to count how many times the function is called. | ||
|
|
||
| If ctypes.CDLL is called, the counter increases by one. | ||
| """ | ||
| self.counter += 1 # Increase the counter | ||
| return self.loaded_libgmt | ||
|
|
||
| @pytest.fixture() | ||
| def _mock_ctypes(self, monkeypatch): | ||
| monkeypatch.setattr(ctypes, "CDLL", self._mock_ctypes_cdll_return) | ||
|
|
||
| @pytest.mark.usefixtures("_mock_ctypes") | ||
| def test_libgmt_load_counter(self): | ||
| """ | ||
| Make sure that the GMT library is not loaded in every session. | ||
| """ | ||
| with Session() as lib: | ||
| _ = lib | ||
| with Session() as lib: | ||
| _ = lib | ||
| assert self.counter == 0 # ctypes.CDLL is not called after two sessions. | ||
|
|
||
| # Explicitly calling load_libgmt to make sure the mock function is correct | ||
| load_libgmt() | ||
| assert self.counter == 1 | ||
| load_libgmt() | ||
| assert self.counter == 2 | ||
|
|
||
|
|
||
| ############################################################################### | ||
| # Test clib_full_names | ||
| @pytest.fixture(scope="module", name="gmt_lib_names") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.