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.clear() 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_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): 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_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]) 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]) self.assertEqual(p_x, p_y) t_0_1.claim("a") self.assertNotEqual(p_x, p_y) t_1_1.claim("a") self.assertEqual(p_x, p_y) t_0_0.claim("b") t_1_0.claim("b") self.assertEqual(p_x, p_y)
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_cpu(package_count=DEFAULT_PACKAGE_COUNT, cores_per_package=DEFAULT_CORE_COUNT, threads_per_core=DEFAULT_THREAD_COUNT): packages = [] for p_i in range(package_count): cores = [] for c_i in range(cores_per_package): cores.append( Core( c_i, __get_threads(p_i, c_i, package_count, cores_per_package, threads_per_core))) packages.append(Package(p_i, cores)) return Cpu(packages)
def get_cpu( package_count=DEFAULT_PACKAGE_COUNT, cores_per_package=DEFAULT_CORE_COUNT, threads_per_core=DEFAULT_THREAD_COUNT): print("package count: " + str(package_count) + " cores_per_package:" + str(cores_per_package) + " threads_per_core:" + str(threads_per_core)) packages = [] for p_i in range(package_count): cores = [] for c_i in range(cores_per_package): cores.append( Core(c_i, __get_threads(p_i, c_i, package_count, cores_per_package, threads_per_core))) packages.append(Package(p_i, cores)) print("-------------") print(Cpu(packages)) print(Cpu(packages).get_threads()) print(len(Cpu(packages).get_threads())) print("-------------") return Cpu(packages)
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_invalid_package(self): with self.assertRaises(ValueError): Package(1, [])