示例#1
0
    def test_cloud_metadata_GCP(self, mock_get_env_cloud_details):
        # WHEN
        cloud = CloudMetadata.from_utils()

        # THEN
        self.assertEqual("gcp", cloud.provider)
        self.assertEqual("us-central1", cloud.region)
示例#2
0
    def test_cloud_metadata_AWS(self, mock_get_env_cloud_details):
        # WHEN
        cloud = CloudMetadata.from_utils()

        # THEN
        self.assertEqual("aws", cloud.provider)
        self.assertEqual("us-east-1", cloud.region)
示例#3
0
    def test_cloud_metadata_AZURE(self, mock_get_env_cloud_details):
        # WHEN
        cloud = CloudMetadata.from_utils()

        # THEN
        self.assertEqual("azure", cloud.provider)
        self.assertEqual("eastus", cloud.region)
示例#4
0
    def test_emissions_CLOUD_GCP(self):
        emissions = self._emissions.get_cloud_emissions(
            Energy.from_energy(kwh=0.01),
            CloudMetadata(provider="gcp", region="us-central1"),
        )

        # THEN
        assert isinstance(emissions, float)
        self.assertAlmostEqual(emissions, 0.01, places=2)
示例#5
0
    def test_emissions_CLOUD_AZURE(self):
        # WHEN
        emissions = self._emissions.get_cloud_emissions(
            Energy.from_energy(kwh=1.5),
            CloudMetadata(provider="azure", region="eastus"),
        )

        # THEN
        assert isinstance(emissions, float)
        self.assertAlmostEqual(emissions, 0.55, places=2)
示例#6
0
    def test_get_emissions_CLOUD_AWS(self):
        # WHEN

        emissions = self._emissions.get_cloud_emissions(
            Energy.from_energy(kwh=0.6),
            CloudMetadata(provider="aws", region="us-east-1"),
        )

        # THEN
        assert isinstance(emissions, float)
        self.assertAlmostEqual(emissions, 0.22, places=2)
示例#7
0
 def _get_cloud_metadata(self) -> CloudMetadata:
     if self._cloud is None:
         self._cloud = CloudMetadata.from_utils()
     return self._cloud
示例#8
0
 def _get_cloud_metadata(self) -> CloudMetadata:
     if self._cloud is None:
         self._cloud = CloudMetadata(provider=self._cloud_provider,
                                     region=self._cloud_region)
     return self._cloud
示例#9
0
def heavy_computation(run_time_secs: int = 3):
    end_time: float = time.time(
    ) + run_time_secs  # Run for `run_time_secs` seconds
    while time.time() < end_time:
        pass


@mock.patch("codecarbon.emissions_tracker.is_gpu_details_available",
            return_value=True)
@mock.patch(
    "codecarbon.external.hardware.get_gpu_details",
    return_value=TWO_GPU_DETAILS_RESPONSE,
)
@mock.patch(
    "codecarbon.emissions_tracker.EmissionsTracker._get_cloud_metadata",
    return_value=CloudMetadata(provider=None, region=None),
)
class TestCarbonTracker(unittest.TestCase):
    def setUp(self) -> None:
        self.data_source = get_test_data_source()
        self.project_name = "project_foo"
        self.emissions_file_path = os.path.join(os.getcwd(), "emissions.csv")

    def tearDown(self) -> None:
        if os.path.isfile(self.emissions_file_path):
            os.remove(
                self.emissions_file_path)  # delete test artifact if it exists.

    @responses.activate
    def test_carbon_tracker_TWO_GPU_PRIVATE_INFRA_CANADA(
        self,
示例#10
0
 def _get_cloud_metadata(self) -> CloudMetadata:
     return CloudMetadata.from_utils()
示例#11
0
 def _get_cloud_metadata(self) -> CloudMetadata:
     return CloudMetadata(provider=None, region=None)
 def _get_cloud_metadata(self) -> CloudMetadata:
     return CloudMetadata(provider=self._cloud_provider,
                          region=self._cloud_region)