def test_gmpy2_parallelism_is_safe(self): """ Ensures running lots of parallel exponentiations still yields the correct answer. This verifies that nothing incorrect is happening in the GMPY2 library """ # Arrange scheduler = Scheduler() problem_size = 1000 random_secret_keys = Nonces(int_to_q_unchecked(3))[0:problem_size] log_info( f"testing GMPY2 powmod parallelism safety (cpus = {scheduler.cpu_count}, problem_size = {problem_size})" ) # Act start = timer() keypairs = scheduler.schedule( elgamal_keypair_from_secret, [list([secret_key]) for secret_key in random_secret_keys], ) end1 = timer() # Assert for keypair in keypairs: self.assertEqual( keypair.public_key, elgamal_keypair_from_secret(keypair.secret_key).public_key, ) end2 = timer() scheduler.close() log_info(f"Parallelism speedup: {(end2 - end1) / (end1 - start):.3f}")
def test_schedule_callable_throws(self): # Arrange subject = Scheduler() # Act result = subject.schedule(_exception_callable, [list([1]), list([2])]) # Assert self.assertIsNotNone(result) self.assertIsInstance(result, List) subject.close()
def test_safe_map(self): # Arrange from multiprocessing import Pool process_pool = Pool(1) subject = Scheduler() # Act result = subject.safe_map(process_pool, _callable, [1]) self.assertEqual(result, [1]) # verify exceptions are caught result = subject.safe_map(process_pool, _exception_callable, [1]) self.assertIsNotNone(result) self.assertIsInstance(result, List) # verify closing the poll catches the value error process_pool.close() result = subject.safe_map(process_pool, _callable, [1]) self.assertIsNotNone(result) self.assertIsInstance(result, List) subject.close()
def get_scheduler() -> Scheduler: return Scheduler()
"Wasn't expecting an invalid proof during a benchmark!") return end1 - start1, end2 - end1 def identity(x: int) -> int: """Placeholder function used just to warm up the parallel mapper prior to benchmarking.""" return x if __name__ == "__main__": problem_sizes = (100, 500, 1000, 5000) rands = Nonces(int_to_q_unchecked(31337)) speedup: Dict[int, float] = {} # warm up the pool to help get consistent measurements with Scheduler() as scheduler: results: List[int] = scheduler.schedule( identity, [list([x]) for x in range(1, 30000)]) assert results == list(range(1, 30000)) bench_start = timer() for size in problem_sizes: print("Benchmarking on problem size: ", size) seeds = rands[0:size] inputs = [ BenchInput( get_optional(elgamal_keypair_from_secret(a)), rands[size], rands[size + 1], ) for a in seeds