-
-
Notifications
You must be signed in to change notification settings - Fork 237
Feat/add caching to data sources #1015
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
Merged
+212
−31
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """ | ||
| Tests for codecarbon/input.py module-level caching. | ||
|
|
||
| The caching mechanism loads static reference data once at module import | ||
| to avoid file I/O on the hot path (start_task/stop_task). | ||
| """ | ||
|
|
||
| import unittest | ||
|
|
||
|
|
||
| class TestDataSourceCaching(unittest.TestCase): | ||
| """Test that DataSource uses module-level cache for static data.""" | ||
|
|
||
| def test_cache_populated_at_import(self): | ||
| """Verify that _CACHE is populated when module is imported.""" | ||
| from codecarbon.input import _CACHE | ||
|
|
||
| # All static data should be pre-loaded | ||
| self.assertIn("global_energy_mix", _CACHE) | ||
| self.assertIn("cloud_emissions", _CACHE) | ||
| self.assertIn("carbon_intensity_per_source", _CACHE) | ||
| self.assertIn("cpu_power", _CACHE) | ||
|
|
||
| # Verify data is non-empty | ||
| self.assertGreater(len(_CACHE["global_energy_mix"]), 0) | ||
| self.assertGreater(len(_CACHE["cloud_emissions"]), 0) | ||
| self.assertGreater(len(_CACHE["carbon_intensity_per_source"]), 0) | ||
| self.assertGreater(len(_CACHE["cpu_power"]), 0) | ||
|
|
||
| def test_get_global_energy_mix_returns_cached_data(self): | ||
| """Verify get_global_energy_mix_data() returns cached object.""" | ||
| from codecarbon.input import _CACHE, DataSource | ||
|
|
||
| ds = DataSource() | ||
| data = ds.get_global_energy_mix_data() | ||
|
|
||
| # Should return the exact same object from cache | ||
| self.assertIs(data, _CACHE["global_energy_mix"]) | ||
|
|
||
| def test_get_cloud_emissions_returns_cached_data(self): | ||
| """Verify get_cloud_emissions_data() returns cached object.""" | ||
| from codecarbon.input import _CACHE, DataSource | ||
|
|
||
| ds = DataSource() | ||
| data = ds.get_cloud_emissions_data() | ||
|
|
||
| # Should return the exact same object from cache | ||
| self.assertIs(data, _CACHE["cloud_emissions"]) | ||
|
|
||
| def test_get_carbon_intensity_returns_cached_data(self): | ||
| """Verify get_carbon_intensity_per_source_data() returns cached object.""" | ||
| from codecarbon.input import _CACHE, DataSource | ||
|
|
||
| ds = DataSource() | ||
| data = ds.get_carbon_intensity_per_source_data() | ||
|
|
||
| # Should return the exact same object from cache | ||
| self.assertIs(data, _CACHE["carbon_intensity_per_source"]) | ||
|
|
||
| def test_get_cpu_power_returns_cached_data(self): | ||
| """Verify get_cpu_power_data() returns cached object.""" | ||
| from codecarbon.input import _CACHE, DataSource | ||
|
|
||
| ds = DataSource() | ||
| data = ds.get_cpu_power_data() | ||
|
|
||
| # Should return the exact same object from cache | ||
| self.assertIs(data, _CACHE["cpu_power"]) | ||
|
|
||
| def test_country_data_lazy_loaded(self): | ||
| """Verify country-specific data is lazy-loaded and cached.""" | ||
| from codecarbon.input import _CACHE, DataSource | ||
|
|
||
| ds = DataSource() | ||
| cache_key = "country_emissions_usa" | ||
|
|
||
| # USA data may or may not be cached depending on prior test runs | ||
| # Just verify that after calling, it IS cached | ||
| data = ds.get_country_emissions_data("usa") | ||
| self.assertIn(cache_key, _CACHE) | ||
| self.assertIs(data, _CACHE[cache_key]) | ||
|
|
||
| def test_multiple_datasource_instances_share_cache(self): | ||
| """Verify that multiple DataSource instances share the same cache.""" | ||
| from codecarbon.input import DataSource | ||
|
|
||
| ds1 = DataSource() | ||
| ds2 = DataSource() | ||
|
|
||
| # Both instances should return the same cached object | ||
| data1 = ds1.get_global_energy_mix_data() | ||
| data2 = ds2.get_global_energy_mix_data() | ||
|
|
||
| self.assertIs(data1, data2) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.