|
| 1 | +import json |
1 | 2 | import os
|
2 | 3 | import pytest
|
3 | 4 | import pytest_mock
|
|
15 | 16 | import datetime
|
16 | 17 | import simvue
|
17 | 18 | from simvue.api.objects import Alert, Metrics
|
| 19 | +from simvue.eco.api_client import CO2SignalData, CO2SignalResponse |
18 | 20 | from simvue.exception import SimvueRunError
|
| 21 | +from simvue.eco.emissions_monitor import TIME_FORMAT, CO2Monitor |
19 | 22 | import simvue.run as sv_run
|
20 | 23 | import simvue.client as sv_cl
|
21 | 24 | import simvue.sender as sv_send
|
@@ -47,38 +50,51 @@ def test_check_run_initialised_decorator() -> None:
|
47 | 50 |
|
48 | 51 | @pytest.mark.run
|
49 | 52 | @pytest.mark.eco
|
50 |
| -def test_run_with_emissions(mock_co2_signal) -> None: |
51 |
| - with sv_run.Run() as run_created: |
52 |
| - run_created.init( |
53 |
| - name="test_run_with_emissions", |
54 |
| - folder="/simvue_client_unit_tests", |
55 |
| - retention_period="1 min", |
56 |
| - tags=["test_run_with_emissions"] |
| 53 | +@pytest.mark.online |
| 54 | +def test_run_with_emissions_online(speedy_heartbeat, mock_co2_signal, create_plain_run) -> None: |
| 55 | + run_created, _ = create_plain_run |
| 56 | + run_created.config(enable_emission_metrics=True) |
| 57 | + time.sleep(3) |
| 58 | + _run = RunObject(identifier=run_created.id) |
| 59 | + _metric_names = [item[0] for item in _run.metrics] |
| 60 | + client = sv_cl.Client() |
| 61 | + for _metric in ["emissions", "energy_consumed"]: |
| 62 | + _total_metric_name = f"sustainability.{_metric}.total" |
| 63 | + _delta_metric_name = f"sustainability.{_metric}.delta" |
| 64 | + assert _total_metric_name in _metric_names |
| 65 | + assert _delta_metric_name in _metric_names |
| 66 | + _metric_values = client.get_metric_values( |
| 67 | + metric_names=[_total_metric_name, _delta_metric_name], |
| 68 | + xaxis="time", |
| 69 | + output_format="dataframe", |
| 70 | + run_ids=[run_created.id], |
57 | 71 | )
|
58 |
| - run_created.config(enable_emission_metrics=True, resources_metrics_interval=1) |
59 |
| - time.sleep(5) |
60 |
| - _run = RunObject(identifier=run_created.id) |
61 |
| - _metric_names = [item[0] for item in _run.metrics] |
62 |
| - client = sv_cl.Client() |
63 |
| - for _metric in ["emissions", "energy_consumed"]: |
64 |
| - _total_metric_name = f"sustainability.{_metric}.total" |
65 |
| - _delta_metric_name = f"sustainability.{_metric}.delta" |
66 |
| - assert _total_metric_name in _metric_names |
67 |
| - assert _delta_metric_name in _metric_names |
68 |
| - _metric_values = client.get_metric_values( |
69 |
| - metric_names=[_total_metric_name, _delta_metric_name], |
70 |
| - xaxis="time", |
71 |
| - output_format="dataframe", |
72 |
| - run_ids=[run_created.id], |
73 |
| - ) |
| 72 | + assert _total_metric_name in _metric_values |
74 | 73 |
|
75 |
| - # Check that total = previous total + latest delta |
76 |
| - _total_values = _metric_values[_total_metric_name].tolist() |
77 |
| - _delta_values = _metric_values[_delta_metric_name].tolist() |
78 |
| - assert len(_total_values) > 1 |
79 |
| - for i in range(1, len(_total_values)): |
80 |
| - assert _total_values[i] == _total_values[i - 1] + _delta_values[i] |
81 | 74 |
|
| 75 | +@pytest.mark.run |
| 76 | +@pytest.mark.eco |
| 77 | +@pytest.mark.offline |
| 78 | +def test_run_with_emissions_offline(speedy_heartbeat, mock_co2_signal, create_plain_run_offline) -> None: |
| 79 | + run_created, _ = create_plain_run_offline |
| 80 | + run_created.config(enable_emission_metrics=True) |
| 81 | + time.sleep(2) |
| 82 | + id_mapping = sv_send.sender(os.environ["SIMVUE_OFFLINE_DIRECTORY"]) |
| 83 | + _run = RunObject(identifier=id_mapping[run_created.id]) |
| 84 | + _metric_names = [item[0] for item in _run.metrics] |
| 85 | + client = sv_cl.Client() |
| 86 | + for _metric in ["emissions", "energy_consumed"]: |
| 87 | + _total_metric_name = f"sustainability.{_metric}.total" |
| 88 | + _delta_metric_name = f"sustainability.{_metric}.delta" |
| 89 | + assert _total_metric_name in _metric_names |
| 90 | + assert _delta_metric_name in _metric_names |
| 91 | + _metric_values = client.get_metric_values( |
| 92 | + metric_names=[_total_metric_name, _delta_metric_name], |
| 93 | + xaxis="time", |
| 94 | + output_format="dataframe", |
| 95 | + run_ids=[id_mapping[run_created.id]], |
| 96 | + ) |
| 97 | + assert _total_metric_name in _metric_values |
82 | 98 |
|
83 | 99 | @pytest.mark.run
|
84 | 100 | @pytest.mark.parametrize(
|
|
0 commit comments