from fractions import Fraction as F from itertools import product import json from pathlib import Path import unittest from model import batch_completion, batch_examples, compare, conditional_mean, result class ProcessorSharingTests(unittest.TestCase): constant = [("1", "1")] mixture = [("99/100", "1/2"), ("1/100", "101/2")] def test_exact_rows(self): rows = result()["rows"] self.assertEqual([r["exact"]["ps_mean_response_ms"] for r in rows], ["5/4", "5/4", "5", "5"]) self.assertEqual([r["exact"]["fcfs_mean_response_ms"] for r in rows], ["9/8", "135/32", "3", "105/2"]) def test_fixed_mean_different_second_moment(self): a, b = (compare(d, 200) for d in (self.constant, self.mixture)) self.assertEqual(a["mean_service_ms"], b["mean_service_ms"]) self.assertEqual(b["second_moment_ms2"], F(103, 4)) self.assertEqual(a["ps_mean_response_ms"], b["ps_mean_response_ms"]) def test_policy_ranking_reverses(self): a, b = (compare(d, 200) for d in (self.constant, self.mixture)) self.assertLess(a["fcfs_mean_response_ms"], a["ps_mean_response_ms"]) self.assertGreater(b["fcfs_mean_response_ms"], b["ps_mean_response_ms"]) def test_conditional_expectations_average_to_unconditional(self): rho = compare(self.mixture, 200)["utilization"] mean = sum(F(p) * conditional_mean(s, rho) for p, s in self.mixture) self.assertEqual(mean, F(5, 4)) self.assertEqual(conditional_mean("1/2", rho), F(5, 8)) self.assertEqual(conditional_mean("101/2", rho), F(505, 8)) def test_no_arrival_limit(self): row = compare(self.mixture, 0) self.assertEqual(row["ps_mean_response_ms"], 1) self.assertEqual(row["fcfs_mean_response_ms"], 1) def test_time_scaling(self): original = compare(self.mixture, 200) scaled = compare([(p, F(s) * 10) for p, s in self.mixture], 20) self.assertEqual(scaled["utilization"], original["utilization"]) for key in ("ps_mean_response_ms", "fcfs_mean_response_ms"): self.assertEqual(scaled[key], original[key] * 10) def test_invalid_distributions(self): for distribution in ([], [("1/2", "1")], [("0", "1")], [("1", "0")], [("-1", "1"), ("2", "1")]): with self.subTest(distribution=distribution), self.assertRaises(ValueError): compare(distribution, 200) def test_invalid_rate(self): for rate in (-1, 1000, 1001, True, 0.2, "NaN", "1/0"): with self.subTest(rate=rate), self.assertRaises(ValueError): compare(self.constant, rate) def test_invalid_conditional_input(self): for size, rho in ((0, 0), (-1, 0), (1, -1), (1, 1), (True, 0)): with self.subTest(size=size, rho=rho), self.assertRaises(ValueError): conditional_mean(size, rho) def test_same_count_and_work_different_completion(self): rows = batch_examples()["rows"] self.assertEqual([r["other_job_count"] for r in rows], [2, 2]) self.assertEqual([r["total_other_remaining_ms"] for r in rows], ["2", "2"]) self.assertEqual([r["completion_ms_exact"] for r in rows], ["9/4", "3"]) def test_batch_matches_independent_event_oracle(self): def event_completion(tagged, others): active = {i: value for i, value in enumerate([tagged, *others]) if value > 0} elapsed = F(0) while 0 in active: service_step = min(active.values()) elapsed += service_step * len(active) active = {i: value - service_step for i, value in active.items() if value > service_step} return elapsed candidates = list(map(F, ["0", "1/4", "1/2", "1", "7/4", "2", "3"])) checked = 0 for size in map(F, ["1/4", "1/2", "1", "3/2", "2"]): for count in range(5): for remaining in product(candidates, repeat=count): self.assertEqual(batch_completion(size, remaining), event_completion(size, remaining)) checked += 1 self.assertEqual(checked, 14005) def test_batch_scaling_and_permutation(self): self.assertEqual(batch_completion("2", ["7/2", "1/2"]), F(9, 2)) self.assertEqual(batch_completion("1", []), 1) self.assertEqual(batch_completion("1", ["0", "1", "1"]), 3) def test_batch_invalid_inputs(self): for size, remaining in [(0, []), (-1, []), (True, []), (1, [-1]), (1, [False]), (1, [0.5]), (1, ["NaN"]), (1, ["1/0"]), (1, "1"), (1, None)]: with self.subTest(size=size, remaining=remaining), self.assertRaises(ValueError): batch_completion(size, remaining) def test_published_result_matches(self): self.assertEqual(json.loads(Path(__file__).with_name("results.json").read_text()), result()) if __name__ == "__main__": unittest.main()