def test_system_cpu_gauge(self):
        # given
        gauge = SystemCpuUsageGauge()

        # when
        cpu_usage = gauge.value()

        # then
        self.assertGreater(cpu_usage, 0.0)
        self.assertLessEqual(cpu_usage, 100.0)
        self.assertEqual(float, type(cpu_usage))
 def create_cpu_usage_gauge(self):
     if self.__gauge_mode == GaugeMode.SYSTEM:
         return SystemCpuUsageGauge()
     elif self.__gauge_mode == GaugeMode.CGROUP:
         return CGroupCpuUsageGauge()
     else:
         raise self.__invalid_gauge_mode_exception()
Beispiel #3
0
    def test_format_fractional_cpu_core_count(self):
        # given
        system_resource_info = SystemResourceInfo(
            cpu_core_count=0.5,
            memory_amount_bytes=2 * BYTES_IN_ONE_GB,
            gpu_card_indices=[],
            gpu_memory_amount_bytes=0,
        )
        # and
        metrics_factory = MetricsFactory(self.gauge_factory,
                                         system_resource_info)

        # when
        metrics_container = metrics_factory.create_metrics_container()

        # then
        self.assertEqual(
            Metric(
                name=u"CPU - usage",
                description=u"average of all cores",
                resource_type=MetricResourceType.CPU,
                unit=u"%",
                min_value=0.0,
                max_value=100.0,
                gauges=[SystemCpuUsageGauge()],
            ),
            metrics_container.cpu_usage_metric,
        )
    def test_create_metrics_with_gpu(self):
        # given
        system_resource_info = SystemResourceInfo(
            cpu_core_count=4,
            memory_amount_bytes=16 * BYTES_IN_ONE_GB,
            gpu_card_indices=[0, 1],
            gpu_memory_amount_bytes=8 * BYTES_IN_ONE_GB)
        # and
        metrics_factory = MetricsFactory(self.gauge_factory,
                                         system_resource_info)

        # when
        metrics_container = metrics_factory.create_metrics_container()

        # then
        self.assertEqual(
            Metric(name=u'CPU - usage',
                   description=u'average of all cores',
                   resource_type=MetricResourceType.CPU,
                   unit=u'%',
                   min_value=0.0,
                   max_value=100.0,
                   gauges=[SystemCpuUsageGauge()]),
            metrics_container.cpu_usage_metric)
        # and
        self.assertEqual(
            Metric(name=u'RAM',
                   description=u'',
                   resource_type=MetricResourceType.RAM,
                   unit=u'GB',
                   min_value=0.0,
                   max_value=16.0,
                   gauges=[SystemMemoryUsageGauge()]),
            metrics_container.memory_metric)
        # and
        self.assertEqual(
            Metric(name=u'GPU - usage',
                   description=u'2 cards',
                   resource_type=MetricResourceType.GPU,
                   unit=u'%',
                   min_value=0.0,
                   max_value=100.0,
                   gauges=[
                       GpuUsageGauge(card_index=0),
                       GpuUsageGauge(card_index=1)
                   ]), metrics_container.gpu_usage_metric)
        # and
        self.assertEqual(
            Metric(name=u'GPU - memory',
                   description=u'2 cards',
                   resource_type=MetricResourceType.GPU_RAM,
                   unit=u'GB',
                   min_value=0.0,
                   max_value=8.0,
                   gauges=[
                       GpuMemoryGauge(card_index=0),
                       GpuMemoryGauge(card_index=1)
                   ]), metrics_container.gpu_memory_metric)
Beispiel #5
0
    def test_create_system_metrics(self):
        # given
        memory_amount_gb = psutil.virtual_memory().total / float(
            BYTES_IN_ONE_GB)

        # and
        experiment = MagicMock()

        # and
        cpu_metric_id = str(uuid.uuid4())
        ram_metric_id = str(uuid.uuid4())
        self.backend.create_hardware_metric.side_effect = [
            cpu_metric_id, ram_metric_id
        ]

        # when
        self.metric_service_factory.create(
            gauge_mode=GaugeMode.SYSTEM,
            experiment=experiment,
            reference_timestamp=time.time(),
        )

        # then
        self.backend.create_hardware_metric.assert_has_calls([
            call(
                experiment,
                Metric(
                    internal_id=cpu_metric_id,
                    name=u"CPU - usage",
                    description=u"average of all cores",
                    resource_type=MetricResourceType.CPU,
                    unit=u"%",
                    min_value=0.0,
                    max_value=100.0,
                    gauges=[SystemCpuUsageGauge()],
                ),
            ),
            call(
                experiment,
                Metric(
                    internal_id=ram_metric_id,
                    name=u"RAM",
                    description=u"",
                    resource_type=MetricResourceType.RAM,
                    unit=u"GB",
                    min_value=0.0,
                    max_value=memory_amount_gb,
                    gauges=[SystemMemoryUsageGauge()],
                ),
            ),
        ])
Beispiel #6
0
    def __init__(self):
        self.gauge_factory = MagicMock(spec_set=GaugeFactory)

        self.cpu_gauge_value = 1.0
        self.memory_gauge_value = 2.0

        self.gpu0_usage_gauge_value = 3.0
        self.gpu1_usage_gauge_value = 4.0

        self.gpu0_memory_gauge_value = 5.0
        self.gpu1_memory_gauge_value = 6.0

        cpu_gauge = MagicMock(wraps=SystemCpuUsageGauge())
        cpu_gauge.value.return_value = self.cpu_gauge_value
        self.gauge_factory.create_cpu_usage_gauge.return_value = cpu_gauge

        ram_gauge = MagicMock(wraps=SystemMemoryUsageGauge())
        ram_gauge.value.return_value = self.memory_gauge_value
        self.gauge_factory.create_memory_usage_gauge.return_value = ram_gauge

        gpu_usage_gauge_0 = MagicMock(wraps=GpuUsageGauge(card_index=0))
        gpu_usage_gauge_0.value.return_value = self.gpu0_usage_gauge_value

        gpu_usage_gauge_2 = MagicMock(wraps=GpuUsageGauge(card_index=2))
        gpu_usage_gauge_2.value.return_value = self.gpu1_usage_gauge_value

        self.gauge_factory.create_gpu_usage_gauge.side_effect = (
            lambda card_index: gpu_usage_gauge_0
            if card_index == 0 else gpu_usage_gauge_2)

        gpu_memory_gauge_0 = MagicMock(wraps=GpuMemoryGauge(card_index=0))
        gpu_memory_gauge_0.value.return_value = self.gpu0_memory_gauge_value

        gpu_memory_gauge_2 = MagicMock(wraps=GpuMemoryGauge(card_index=2))
        gpu_memory_gauge_2.value.return_value = self.gpu1_memory_gauge_value
        self.gauge_factory.create_gpu_memory_gauge.side_effect = (
            lambda card_index: gpu_memory_gauge_0
            if card_index == 0 else gpu_memory_gauge_2)