def test_construction(self): core0 = Core(0, [Thread(0), Thread(2)]) core1 = Core(1, [Thread(1), Thread(3)]) cores = [core0, core1] package = Package(0, cores) self.assertEqual(2, len(package.get_cores())) self.assertEqual(cores, package.get_cores())
def test_construction(self): thread0 = Thread(0) thread1 = Thread(1) core = Core(42, [thread0, thread1]) self.assertEqual(42, core.get_id()) self.assertEqual(2, len(core.get_threads())) self.assertEqual(thread0, core.get_threads()[0]) self.assertEqual(thread1, core.get_threads()[1])
def test_claim(self): workload_id = uuid.uuid4() t = Thread(42) self.assertEqual(0, len(t.get_workload_ids())) t.claim(workload_id) self.assertEqual(1, len(t.get_workload_ids())) self.assertEqual(workload_id, t.get_workload_ids()[0]) t.clear() self.assertEqual(0, len(t.get_workload_ids()))
def test_clear_multiple_claims(self): workload_id_a = "a" workload_id_b = "b" t = Thread(42) t.claim(workload_id_a) t.claim(workload_id_b) self.assertTrue(t.is_claimed()) self.assertEqual(2, len(t.get_workload_ids())) t.clear() self.assertFalse(t.is_claimed()) self.assertEqual(0, len(t.get_workload_ids()))
def test_claim(self): workload_id = uuid.uuid4() t = Thread(42) self.assertEqual(None, t.get_workload_id()) t.claim(workload_id) self.assertEqual(workload_id, t.get_workload_id()) t.free() self.assertEqual(None, t.get_workload_id())
def parse_cpu(cpu_dict: dict) -> Cpu: packages = [] for p in cpu_dict["packages"]: cores = [] for c in p["cores"]: threads = [] for t in c["threads"]: thread = Thread(t["id"]) for w_id in t["workload_id"]: thread.claim(w_id) threads.append(thread) cores.append(Core(c["id"], threads)) packages.append(Package(p["id"], cores)) return Cpu(packages)
def __get_threads(package_index, core_index, package_count, core_count, thread_count): threads = [] for row_index in range(thread_count): offset = row_index * package_count * core_count index = offset + package_index * core_count + core_index threads.append(Thread(index)) return threads
def _is_thread_occupied(thread: Thread, workload_map: dict, w_type: str) -> bool: for w_id in thread.get_workload_ids(): if w_id in workload_map: if workload_map[w_id].get_type() == w_type: return True return False
def test_get_empty_threads(self): c = Core(1, [Thread(0), Thread(1)]) self.assertEqual(c.get_threads(), c.get_empty_threads()) t0 = c.get_threads()[0] t1 = c.get_threads()[1] c.get_threads()[0].claim(uuid.uuid4()) self.assertEqual([t1], c.get_empty_threads()) t0.clear() self.assertEqual(c.get_threads(), c.get_empty_threads()) t1.claim(uuid.uuid4()) self.assertEqual([t0], c.get_empty_threads()) t0.claim(uuid.uuid4()) self.assertEqual([], c.get_empty_threads())
def test_construction(self): p0 = Package(0, [ Core(0, [Thread(0), Thread(4)]), Core(1, [Thread(1), Thread(5)])]) p1 = Package(1, [ Core(0, [Thread(2), Thread(6)]), Core(1, [Thread(3), Thread(7)])]) packages = [p0, p1] cpu = Cpu(packages) self.assertEqual(packages, cpu.get_packages())
def test_get_empty_threads(self): t0 = Thread(0) t1 = Thread(1) t2 = Thread(2) t3 = Thread(3) c0 = Core(0, [t0, t2]) c1 = Core(1, [t1, t3]) p = Package(1, [c0, c1]) self.assertEqual([t0, t2, t1, t3], p.get_empty_threads()) t1.claim(uuid.uuid4()) self.assertEqual([t0, t2, t3], p.get_empty_threads()) t1.clear() self.assertEqual([t0, t2, t1, t3], p.get_empty_threads())
def test_get_emptiest_core(self): c0 = Core(0, [Thread(0), Thread(2)]) c1 = Core(1, [Thread(1), Thread(3)]) # The first core should be the emptiest p = Package(0, [c0, c1]) self.assertEqual(c0, get_emptiest_core(p)) # The second core should be emptiest after we claim a thread on the first c0.get_threads()[0].claim(uuid.uuid4()) self.assertEqual(c1, get_emptiest_core(p)) # The first core should be the emptiest again, after we release the claimed thread c0.get_threads()[0].clear() self.assertEqual(c0, get_emptiest_core(p)) # The first core should be emptiest when we claim a thread on the second c1.get_threads()[1].claim(uuid.uuid4()) self.assertEqual(c0, get_emptiest_core(p)) # When an equal number of threads are claimed on both cores, the first should be returned c0.get_threads()[1].claim(uuid.uuid4()) self.assertEqual(c0, get_emptiest_core(p))
def test_get_emptiest_package(self): t0 = Thread(0) t1 = Thread(1) t2 = Thread(2) t3 = Thread(3) t4 = Thread(4) t5 = Thread(5) t6 = Thread(6) t7 = Thread(7) p0 = Package(0, [ Core(0, [t0, t4]), Core(1, [t1, t5])]) p1 = Package(1, [ Core(0, [t2, t6]), Core(1, [t3, t7])]) cpu = Cpu([p0, p1]) # The first package should be the emptiest self.assertEqual(p0, cpu.get_emptiest_package()) # The second package should be the emptiest after we claim a thread on the first t5.claim(uuid.uuid4()) self.assertEqual(p1, cpu.get_emptiest_package()) # The first package should be the emptiest again, after we release the claimed thread t5.free() self.assertEqual(p0, cpu.get_emptiest_package()) # The first package should be emptiest when we claim a thread on the second t3.claim(uuid.uuid4()) self.assertEqual(p0, cpu.get_emptiest_package()) # When an equal number of threads are claimed on both packages, the first should be returned t4.claim(uuid.uuid4()) self.assertEqual(p0, cpu.get_emptiest_package())
def test_equality(self): t_0_0 = Thread(0) t_0_1 = Thread(1) c_x = Core(0, [t_0_0, t_0_1]) t_1_0 = Thread(0) t_1_1 = Thread(1) c_y = Core(0, [t_1_0, t_1_1]) self.assertEqual(c_x, c_y) t_0_1.claim("a") self.assertNotEqual(c_x, c_y) t_1_1.claim("a") self.assertEqual(c_x, c_y) t_0_0.claim("b") t_1_0.claim("b") self.assertEqual(c_x, c_y)
def test_equality(self): t_x = Thread(42) t_y = Thread(42) self.assertEqual(t_x, t_y) t_x.claim("a") self.assertNotEqual(t_x, t_y) t_y.claim("a") self.assertEqual(t_x, t_y) t_x.claim("b") t_y.claim("b") self.assertEqual(t_x, t_y)
def test_multiple_claims(self): workload_id_a = "a" workload_id_b = "b" t = Thread(42) t.claim(workload_id_a) self.assertEqual(1, len(t.get_workload_ids())) self.assertEqual(workload_id_a, t.get_workload_ids()[0]) self.assertTrue(t.is_claimed()) t.claim(workload_id_b) self.assertEqual(2, len(t.get_workload_ids())) self.assertTrue(workload_id_a in t.get_workload_ids()) self.assertTrue(workload_id_b in t.get_workload_ids()) self.assertTrue(t.is_claimed()) t.free(workload_id_a) self.assertEqual(1, len(t.get_workload_ids())) self.assertEqual(workload_id_b, t.get_workload_ids()[0]) self.assertTrue(t.is_claimed()) t.free(workload_id_b) self.assertFalse(t.is_claimed()) self.assertEqual(0, len(t.get_workload_ids()))
def test_equality(self): t_0_0 = Thread(0) t_0_1 = Thread(1) c_x = Core(0, [t_0_0, t_0_1]) p_x = Package(0, [c_x]) cpu_x = Cpu([p_x]) t_1_0 = Thread(0) t_1_1 = Thread(1) c_y = Core(0, [t_1_0, t_1_1]) p_y = Package(0, [c_y]) cpu_y = Cpu([p_y]) self.assertEqual(cpu_x, cpu_y) t_0_1.claim("a") self.assertNotEqual(cpu_x, cpu_y) t_1_1.claim("a") self.assertEqual(cpu_x, cpu_y) t_0_0.claim("b") t_1_0.claim("b") self.assertEqual(cpu_x, cpu_y)
def test_invalid_thread(self): with self.assertRaises(ValueError): Thread(-1) with self.assertRaises(ValueError): Thread("foo")
def test_construction(self): thread0 = Thread(0) self.assertEqual(0, thread0.get_id())
def __get_workloads_str(t: Thread) -> str: return str(sorted([str(w_id) for w_id in t.get_workload_ids()]))
def test_free_unknown(self): t = Thread(42) self.assertEqual(0, len(t.get_workload_ids())) self.assertFalse(t.is_claimed()) t.free("unknown_id") self.assertEqual(0, len(t.get_workload_ids())) self.assertFalse(t.is_claimed()) workload_id = "a" t.claim(workload_id) t.free("unknown_id") self.assertEqual(1, len(t.get_workload_ids())) self.assertEqual(workload_id, t.get_workload_ids()[0]) self.assertTrue(t.is_claimed())