def test_max_parallel_tasks(self): # Count task parallelism. 3 total tasks, but only 2 at a given time self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1015)) self.report.add_report( TimedResourceReport(start_time=TIME_1030, finish_time=TIME_1100)) self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1100)) self.assertEqual(self.report.total_tasks(), 3) self.assertEqual(self.report.max_parallel_tasks(), 2)
def test_max_parallel_tasks_handles_start_finish_bounds(self): # If a task finishes at the same time another starts, that is 1 parallel task and not 2 task_1000_1015 = TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1015) task_1015_1030 = TimedResourceReport(start_time=TIME_1015, finish_time=TIME_1030) self.assertEqual(task_1000_1015.finish_time, task_1015_1030.start_time) self.report.add_report(task_1000_1015) self.report.add_report(task_1015_1030) self.assertEqual(self.report.total_tasks(), 2) self.assertEqual(self.report.max_parallel_tasks(), 1)
def test_calculates_start_finish_times(self): self.assertIsNone(self.report.start_time) self.assertIsNone(self.report.finish()) self.report.add_report( TimedResourceReport(start_time=TIME_1015, finish_time=TIME_1100)) self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1030)) self.report.add_report( TimedResourceReport(start_time=TIME_1030, finish_time=TIME_1045)) self.assertEqual(self.report.start_time, TIME_1000) self.assertEqual(self.report.finish_time, TIME_1100)
def test_total_cpu_hours(self): # 1 hour at 1 CPU and 15 minutes at 4 cpu should total 2 CPU hours self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1100, cpus=1)) self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1015, cpus=4)) self.assertEqual(self.report.total_cpu_hours(), 2)
def test_total_ram_megabyte_hours(self): # 1 hour at 1024MB and 15 minutes at 8192MB cpu should total 3072 MB/hours self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1100, ram_megabytes=1024)) self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1015, ram_megabytes=8192)) self.assertEqual(self.report.total_tasks(), 2) self.assertEqual(self.report.total_ram_megabyte_hours(), 3072)
def test_max_parallel_cpus_complex(self): # 4 CPUs for a short burst, overlapping with 1 cpu, then a later period of 1 that doesnt overlap self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1015, cpus=4)) self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1045, cpus=1)) self.report.add_report( TimedResourceReport(start_time=TIME_1030, finish_time=TIME_1100, cpus=1)) self.assertEqual(self.report.total_tasks(), 3) self.assertEqual(self.report.max_parallel_cpus(), 5)
def test_max_parallel_cpus_overlap(self): # 1 cpu over 3 30 minute intervals, with the middle interval overlapping the first and last self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1030, cpus=1)) self.report.add_report( TimedResourceReport(start_time=TIME_1015, finish_time=TIME_1045, cpus=1)) self.report.add_report( TimedResourceReport(start_time=TIME_1030, finish_time=TIME_1100, cpus=1)) self.assertEqual(self.report.total_tasks(), 3) self.assertEqual(self.report.max_parallel_cpus(), 2)
def test_max_parallel_ram_megabytes_overlap(self): # 1024 MB over 3 30 minute intervals, with the middle interval overlapping the first and last self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1030, ram_megabytes=1024)) self.report.add_report( TimedResourceReport(start_time=TIME_1015, finish_time=TIME_1045, ram_megabytes=1024)) self.report.add_report( TimedResourceReport(start_time=TIME_1030, finish_time=TIME_1100, ram_megabytes=1024)) self.assertEqual(self.report.total_tasks(), 3) self.assertEqual(self.report.total_ram_megabyte_hours(), 1536) self.assertEqual(self.report.max_parallel_ram_megabytes(), 2048)
def test_write_report(self, mock_json, mock_open): initialize_reporter(128, 4) Reporter.add_report( TimedResourceReport(cpus=1, ram_megabytes=128, start_time=TIME_1000, finish_time=TIME_1100)) write_report('output.json') self.assertEqual(mock_open.call_args, call('output.json', 'w')) self.assertTrue(mock_json.dump.called)
def test_to_dict(self): self.report.cores_allowed = 4 self.report.ram_mb_allowed = 4096 self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1030, ram_megabytes=1024)) self.report.add_report( TimedResourceReport(start_time=TIME_1015, finish_time=TIME_1045, ram_megabytes=1024)) self.report.add_report( TimedResourceReport(start_time=TIME_1030, finish_time=TIME_1100, ram_megabytes=1024)) report_dict = self.report.to_dict() self.assertEqual(len(report_dict['children']), 3) self.assertEqual(report_dict['start_time'], TIME_1000) self.assertEqual(report_dict['finish_time'], TIME_1100) self.assertEqual(report_dict['cores_allowed'], 4) self.assertEqual(report_dict['ram_mb_allowed'], 4096)
def test_max_parallel_ram_megabytes_discrete(self): # 4 discrete 15 minute intervals of 1024 MB self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1015, ram_megabytes=1024)) self.report.add_report( TimedResourceReport(start_time=TIME_1015, finish_time=TIME_1030, ram_megabytes=1024)) self.report.add_report( TimedResourceReport(start_time=TIME_1030, finish_time=TIME_1045, ram_megabytes=1024)) self.report.add_report( TimedResourceReport(start_time=TIME_1045, finish_time=TIME_1100, ram_megabytes=1024)) self.assertEqual(self.report.total_tasks(), 4) self.assertEqual(self.report.total_ram_megabyte_hours(), 1024) self.assertEqual(self.report.max_parallel_ram_megabytes(), 1024)
def test_max_parallel_cpus_discrete(self): # 4 discrete 15 minute intervals of 1 cpu self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1015, cpus=1)) self.report.add_report( TimedResourceReport(start_time=TIME_1015, finish_time=TIME_1030, cpus=1)) self.report.add_report( TimedResourceReport(start_time=TIME_1030, finish_time=TIME_1045, cpus=1)) self.report.add_report( TimedResourceReport(start_time=TIME_1045, finish_time=TIME_1100, cpus=1)) self.assertEqual(self.report.total_tasks(), 4) self.assertEqual(self.report.total_cpu_hours(), 1) self.assertEqual(self.report.max_parallel_cpus(), 1)
def setUp(self): self.report = TimedResourceReport(name='timed-resource-report', start_time=TIME_1000, finish_time=TIME_1015)
def test_calculates_duration(self): self.report.add_report( TimedResourceReport(start_time=TIME_1000, finish_time=TIME_1015)) self.report.add_report( TimedResourceReport(start_time=TIME_1045, finish_time=TIME_1100)) self.assertEqual(self.report.elapsed_hours(), 1.0)
def test_total_tasks(self): self.report.add_report(TimedResourceReport()) self.report.add_report(TimedResourceReport()) self.assertEqual(self.report.total_tasks(), 2)
def test_add_report(self): child = TimedResourceReport() self.report.add_report(child) self.assertIn(child, self.report.children)