Exemple #1
0
class TestResourceHistory(TestCase):
  def setUp(self):
    self.max_len = 4
    self.resource_history = ResourceHistory(self.max_len)

  def test_add(self):
    next_resource_stamp = time() + 100
    value = ResourceMonitorBase.FullResourceResult({}, 0)

    assert (next_resource_stamp, value) not in self.resource_history._values
    self.resource_history.add(next_resource_stamp, value)
    assert (next_resource_stamp, value) == self.resource_history._values[1]

  def test_add_prevents_old_entries(self):
    with self.assertRaises(ValueError):
      self.resource_history.add(-1, 10)

  def test_get(self):
    resource_stamp = time() + 100

    value = ResourceMonitorBase.FullResourceResult({}, 0)
    value_wrong = ResourceMonitorBase.FullResourceResult({}, 50)

    self.resource_history.add(resource_stamp, value)
    self.resource_history.add(resource_stamp + 1000, value_wrong)
    self.resource_history.add(resource_stamp + 10000, value_wrong)
    assert resource_stamp, value == self.resource_history.get(resource_stamp)
Exemple #2
0
  def test_sample_by_process_from_history(self, mock_get_active_processes):

    fake_process_name_1 = 'fake-process-name-1'
    fake_process_name_2 = 'fake-process-name-2'
    task_path = '.'
    task_monitor = TaskMonitor(task_path, 'fake-task-id')
    fake_process_status_1 = ProcessStatus(process=fake_process_name_1)
    fake_process_status_2 = ProcessStatus(process=fake_process_name_2)
    mock_get_active_processes.return_value = [(fake_process_status_1, 1),
                                              (fake_process_status_2, 2)]

    fake_history = ResourceHistory(2)
    fake_history.add(time(), ResourceMonitorBase.FullResourceResult(
        {fake_process_status_1: ResourceMonitorBase.ProcResourceResult(ProcessSample.empty(), 1),
         fake_process_status_2: ResourceMonitorBase.ProcResourceResult(ProcessSample.empty(), 2),
         }, 10))

    task_resource_monitor = TaskResourceMonitor('fake-task-id', task_monitor,
        history_provider=self.FakeResourceHistoryProvider(fake_history))

    assert task_resource_monitor.name == 'TaskResourceMonitor[fake-task-id]'
    assert task_resource_monitor.sample_by_process(fake_process_name_1) == ProcessSample.empty()
    assert task_resource_monitor.sample_by_process(fake_process_name_2) == ProcessSample.empty()

    _, sample = task_resource_monitor.sample()
    assert sample.num_procs == 3  # 1 pid in fake_process_status_1 and 2 in fake_process_status_2
    assert sample.process_sample == ProcessSample.empty()
    assert sample.disk_usage == 10
    assert mock_get_active_processes.mock_calls == [mock.call(task_monitor),
        mock.call(task_monitor)]