Skip to content

Commit d247bc6

Browse files
committed
getting approx energy measure from CPU time
1 parent 6eb6b7a commit d247bc6

1 file changed

Lines changed: 42 additions & 13 deletions

File tree

microbiorust-py/benchmarks/bench_pipeline.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import subprocess
44
from Bio import SeqIO
55
from microbiorust import gbk
6-
from codecarbon import OfflineEmissionsTracker
6+
77

88
#warnings.simplefilter('ignore', BioPythonWarning)
99

@@ -71,19 +71,48 @@ def track_energy(self, engine, context):
7171
Routes execution based on engine/context and tracks energy with CodeCarbon.
7272
Stores last measured energy per engine in self._energy_joules.
7373
"""
74+
is_ci = os.getenv('CI') or os.getenv('GITHUB_ACTIONS')
75+
start_cpu = time.process_time()
76+
start_wall = time.perf_counter()
7477
os.environ["CODECARBON_CARBON_INTENSITY"] = "475"
75-
tracker = OfflineEmissionsTracker(measure_power_secs=0.1, log_level="CRITICAL", country_iso_code="USA")
76-
tracker.start()
77-
iterations = 500 if engine == 'rust' else 50
78-
result = None
79-
try:
80-
self._run_repeatedly(engine, context, iterations)
81-
finally:
82-
tracker.stop()
83-
84-
energy_kwh = getattr(tracker, "total_energy", 0)
85-
# Store energy per engine in Joules
86-
return (energy_kwh * 3_600_000)/iterations # Joules/iterations
78+
if not is_ci:
79+
try:
80+
from codecarbon import OfflineEmissionsTracker
81+
tracker = OfflineEmissionsTracker(measure_power_secs=0.1, log_level="CRITICAL", country_iso_code="USA")
82+
tracker.start()
83+
iterations = 500 if engine == 'rust' else 50
84+
result = None
85+
try:
86+
self._run_repeatedly(engine, context, iterations)
87+
finally:
88+
tracker.stop()
89+
90+
energy_kwh = getattr(tracker, "total_energy", 0)
91+
except Exception:
92+
pass
93+
# Store energy per engine in Joules
94+
else:
95+
self.run_repeatedly(engine, context, iterations)
96+
cpu_time = time.process_time() - start_cpu
97+
wall_time = time.perf_counter() - start_wall
98+
99+
# Use CPU-based estimation if CodeCarbon didn't work or returned 0
100+
if energy_kwh == 0 or energy_kwh is None:
101+
# CPU utilization factor (how much of wall time was CPU work)
102+
cpu_utilization = min(cpu_time / wall_time, 1.0) if wall_time > 0 else 1.0
103+
104+
# Power estimates for GitHub Actions runner (2-core VM)
105+
base_power_w = 10 # System idle power
106+
cpu_power_w = 30 * cpu_utilization # CPU power under load
107+
avg_power_w = base_power_w + cpu_power_w
108+
109+
# Energy in kWh
110+
energy_kwh = (avg_power_w * wall_time / 3600) / 1000
111+
112+
# Convert to Joules per iteration
113+
energy_joules_per_iteration = (energy_kwh * 3_600_000) / iterations
114+
115+
return energy_joules_per_iteration
87116
track_energy.unit = "J"
88117

89118
# --- 1. PRIMARY TIME BENCHMARK (ASV automatic) ---

0 commit comments

Comments
 (0)