def test_carbon_tracker_TWO_GPU_PRIVATE_INFRA_CANADA( self, mocked_env_cloud_details, mocked_get_gpu_details, mocked_is_gpu_details_available, ): # GIVEN responses.add( responses.GET, "https://get.geojs.io/v1/ip/geo.json", json=GEO_METADATA_CANADA, status=200, ) tracker = EmissionsTracker(measure_power_secs=1, save_to_file=False) # WHEN tracker.start() heavy_computation() emissions = tracker.stop() # THEN self.assertGreaterEqual( mocked_get_gpu_details.call_count, 3) # at least 2 times in 5 seconds + once for init >= 3 self.assertEqual(1, mocked_is_gpu_details_available.call_count) self.assertEqual(1, len(responses.calls)) self.assertEqual("https://get.geojs.io/v1/ip/geo.json", responses.calls[0].request.url) assert isinstance(emissions, float) self.assertAlmostEqual(emissions, 6.262572537957655e-05, places=2)
def test_carbon_tracker_timeout( self, mocked_requests_get, mocked_env_cloud_details, mocked_get_gpu_details, mocked_is_gpu_details_available, mock_setup_intel_cli, mock_log_values, ): # GIVEN def raise_timeout_exception(*args, **kwargs): raise requests.exceptions.Timeout() mocked_requests_get.side_effect = raise_timeout_exception tracker = EmissionsTracker(measure_power_secs=1, save_to_file=False) # WHEN tracker.start() heavy_computation(run_time_secs=2) emissions = tracker.stop() self.assertEqual(1, mocked_requests_get.call_count) self.assertIsInstance(emissions, float) self.assertAlmostEqual(1.1037980397280433e-05, emissions, places=2)
def test_carbon_tracker_online_constant(self): tracker = EmissionsTracker() tracker.start() heavy_computation(run_time_secs=1) emissions = tracker.stop() assert isinstance(emissions, float) self.assertNotEqual(emissions, 0.0) self.assertAlmostEqual(emissions, 6.262572537957655e-05, places=2) self.verify_output_file(self.emissions_file_path)
def test_carbon_tracker_online_logging_output(self): tracker = EmissionsTracker( project_name=self.project_name, save_to_logger=True, logging_logger=self.external_logger, ) tracker.start() heavy_computation(run_time_secs=1) # tracker.flush() # heavy_computation(run_time_secs=1) emissions = tracker.stop() assert isinstance(emissions, float) self.assertNotEqual(emissions, 0.0) self.assertAlmostEqual(emissions, 6.262572537957655e-05, places=2) self.verify_logging_output(self.emissions_file_path)
def test_full_hierarchy(self): global_conf = dedent( """\ [codecarbon] measure_power_secs=10 output_dir=ERROR:not overwritten save_to_file=ERROR:not overwritten """ ) local_conf = dedent( """\ [codecarbon] output_dir=/success/overwritten emissions_endpoint=http://testhost:2000 gpu_ids=ERROR:not overwritten """ ) with patch( "builtins.open", new_callable=get_custom_mock_open(global_conf, local_conf) ): tracker = EmissionsTracker( project_name="test-project", co2_signal_api_token="signal-token" ) self.assertEqual(tracker._measure_power_secs, 10) self.assertEqual(tracker._output_dir, "/success/overwritten") self.assertEqual(tracker._emissions_endpoint, "http://testhost:2000") self.assertEqual(tracker._gpu_ids, [0, 1]) self.assertEqual(tracker._co2_signal_api_token, "signal-token") self.assertEqual(tracker._project_name, "test-project") self.assertTrue(tracker._save_to_file)
def test_graceful_failure( self, mocked_time, mocked_env_cloud_details, mocked_get_gpu_details, mocked_is_gpu_details_available, ): tracker = EmissionsTracker(measure_power_secs=1, save_to_file=False) def raise_exception(*args, **kwargs): raise Exception() mocked_time.side_effect = raise_exception tracker.start()