From d6bf0c887a1f52aae32582248032e83045285cf2 Mon Sep 17 00:00:00 2001 From: Thibault Lestang Date: Mon, 18 Mar 2024 17:57:46 +0000 Subject: [PATCH] Add a test for carbon footprint estimation --- cats/carbonFootprint.py | 6 ++++-- tests/test_footprint.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 tests/test_footprint.py diff --git a/cats/carbonFootprint.py b/cats/carbonFootprint.py index b19245a..34e033e 100644 --- a/cats/carbonFootprint.py +++ b/cats/carbonFootprint.py @@ -8,13 +8,15 @@ def get_footprint_reduction_estimate( PUE: float, jobinfo: list[tuple[int, float]], runtime: datetime.timedelta, - average_best_ci: float, + average_best_ci: float, # in gCO2/kWh average_now_ci: float, ) -> Estimates: + # energy in kWh energy = ( PUE * (runtime.total_seconds() / 3600) - * sum([(nunits * power) / 1000 for nunits, power in jobinfo]) + * sum([(nunits * power) for nunits, power in jobinfo]) + / 1000 ) best = energy * average_best_ci now = energy * average_now_ci diff --git a/tests/test_footprint.py b/tests/test_footprint.py new file mode 100644 index 0000000..4847006 --- /dev/null +++ b/tests/test_footprint.py @@ -0,0 +1,17 @@ +import datetime + +from cats.carbonFootprint import Estimates, get_footprint_reduction_estimate + +JOBINFO = [(1, 2.0), (2, 3.0), (8, 1.0)] + + +def test_get_footprint_reduction_estimate(): + expected = Estimates(now="3 gCO2e", best="2 gCO2e", savings="gCO2e") + est = get_footprint_reduction_estimate( + PUE=1.0, + jobinfo=JOBINFO, + runtime=datetime.timedelta(minutes=60), + average_best_ci=150, # gCO2/kWh + average_now_ci=200, # gCO2/kWh + ) + assert est == expected