diff --git a/benchmark/bin/run_all.py b/benchmark/bin/run_all.py index c3458d9..36a7930 100755 --- a/benchmark/bin/run_all.py +++ b/benchmark/bin/run_all.py @@ -10,28 +10,58 @@ import time _this_dir = os.path.dirname(__file__) + def parse_args(): parser = argparse.ArgumentParser() - parser.add_argument('-l', "--langs", help="Add languages to test, ie: -l pyproto -l pyproto_cpp", action='append', default=['pycapnp']) - parser.add_argument("-r", "--reuse", help="If this flag is passed, re-use tests will be run", action='store_true') - parser.add_argument("-c", "--compression", help="If this flag is passed, compression tests will be run", action='store_true') - parser.add_argument("-i", "--scale_iters", help="Scaling factor to multiply the default iters by", type=float, default=1.0) + parser.add_argument( + "-l", + "--langs", + help="Add languages to test, ie: -l pyproto -l pyproto_cpp", + action="append", + default=["pycapnp"], + ) + parser.add_argument( + "-r", + "--reuse", + help="If this flag is passed, re-use tests will be run", + action="store_true", + ) + parser.add_argument( + "-c", + "--compression", + help="If this flag is passed, compression tests will be run", + action="store_true", + ) + parser.add_argument( + "-i", + "--scale_iters", + help="Scaling factor to multiply the default iters by", + type=float, + default=1.0, + ) return parser.parse_args() + def run_one(prefix, name, mode, iters, faster, compression): res_type = prefix - reuse = 'no-reuse' + reuse = "no-reuse" if faster: - reuse = 'reuse' - res_type += '_reuse' - if compression != 'none': - res_type += '_' + compression + reuse = "reuse" + res_type += "_reuse" + if compression != "none": + res_type += "_" + compression - command = [os.path.join(_this_dir, prefix+"-"+name), mode, reuse, compression, str(iters)] + command = [ + os.path.join(_this_dir, prefix + "-" + name), + mode, + reuse, + compression, + str(iters), + ] start = time.time() - print('running: ' + ' '.join(command), file=sys.stderr) + print("running: " + " ".join(command), file=sys.stderr) p = Popen(command, stdout=PIPE, stderr=PIPE) res = p.wait() end = time.time() @@ -39,14 +69,19 @@ def run_one(prefix, name, mode, iters, faster, compression): data = {} if p.returncode != 0: - sys.stderr.write(' '.join(command) + ' failed to run with errors: \n' + p.stderr.read().decode(sys.stdout.encoding) + '\n') + sys.stderr.write( + " ".join(command) + + " failed to run with errors: \n" + + p.stderr.read().decode(sys.stdout.encoding) + + "\n" + ) sys.stderr.flush() - data['type'] = res_type - data['mode'] = mode - data['name'] = name - data['iters'] = iters - data['time'] = end - start + data["type"] = res_type + data["mode"] = mode + data["name"] = name + data["iters"] = iters + data["time"] = end - start return data @@ -55,28 +90,40 @@ def run_each(name, langs, reuse, compression, iters): ret = [] for lang_name in langs: - ret.append(run_one(lang_name, name, 'object', iters, False, 'none')) - ret.append(run_one(lang_name, name, 'bytes', iters, False, 'none')) + ret.append(run_one(lang_name, name, "object", iters, False, "none")) + ret.append(run_one(lang_name, name, "bytes", iters, False, "none")) if reuse: - ret.append(run_one(lang_name, name, 'object', iters, True, 'none')) - ret.append(run_one(lang_name, name, 'bytes', iters, True, 'none')) + ret.append(run_one(lang_name, name, "object", iters, True, "none")) + ret.append(run_one(lang_name, name, "bytes", iters, True, "none")) if compression: - ret.append(run_one(lang_name, name, 'bytes', iters, True, 'packed')) + ret.append(run_one(lang_name, name, "bytes", iters, True, "packed")) if compression: - ret.append(run_one(lang_name, name, 'bytes', iters, False, 'packed')) + ret.append(run_one(lang_name, name, "bytes", iters, False, "packed")) return ret + def main(): args = parse_args() - os.environ['PATH'] += ':.' + os.environ["PATH"] += ":." data = [] - data += run_each('carsales', args.langs, args.reuse, args.compression, int(2000 * args.scale_iters)) - data += run_each('catrank', args.langs, args.reuse, args.compression, int(100 * args.scale_iters)) - data += run_each('eval', args.langs, args.reuse, args.compression, int(10000 * args.scale_iters)) - json.dump(data, sys.stdout, sort_keys=True, indent=4, separators=(',', ': ')) + data += run_each( + "carsales", + args.langs, + args.reuse, + args.compression, + int(2000 * args.scale_iters), + ) + data += run_each( + "catrank", args.langs, args.reuse, args.compression, int(100 * args.scale_iters) + ) + data += run_each( + "eval", args.langs, args.reuse, args.compression, int(10000 * args.scale_iters) + ) + json.dump(data, sys.stdout, sort_keys=True, indent=4, separators=(",", ": ")) -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/benchmark/bin/runner.py b/benchmark/bin/runner.py index 1627bd8..d4654b8 100755 --- a/benchmark/bin/runner.py +++ b/benchmark/bin/runner.py @@ -8,46 +8,83 @@ from timeit import default_timer import random _this_dir = os.path.dirname(__file__) -sys.path.append(os.path.join(_this_dir, '..')) +sys.path.append(os.path.join(_this_dir, "..")) from common import do_benchmark + def parse_args_simple(): parser = argparse.ArgumentParser() - parser.add_argument("mode", help="Mode to use for serialization, ie. object or bytes") + parser.add_argument( + "mode", help="Mode to use for serialization, ie. object or bytes" + ) parser.add_argument("reuse", help="Currently ignored") parser.add_argument("compression", help="Valid values are none or packed") parser.add_argument("iters", help="Number of iterations to run for", type=int) - parser.add_argument("-I", "--includes", help="Directories to add to PYTHONPATH", default='/usr/local/include') + parser.add_argument( + "-I", + "--includes", + help="Directories to add to PYTHONPATH", + default="/usr/local/include", + ) return parser.parse_args() + def parse_args(): parser = argparse.ArgumentParser() - parser.add_argument("name", help="Name of the benchmark to run, eg. carsales", nargs='?', default='carsales') - parser.add_argument("-c", "--compression", help="Specify the compression type", default=None) - parser.add_argument("-s", "--suffix", help="Choose the protocol type.", default='pycapnp') - parser.add_argument("-m", "--mode", help="Specify the mode", default='object') - parser.add_argument("-i", "--iters", help="Specify the number of iterations manually. By default, it will be looked up in preset table", default=10, type=int) - parser.add_argument("-r", "--reuse", help="If this flag is passed, objects will be re-used", action='store_true') - parser.add_argument("-I", "--includes", help="Directories to add to PYTHONPATH", default='/usr/local/include') + parser.add_argument( + "name", + help="Name of the benchmark to run, eg. carsales", + nargs="?", + default="carsales", + ) + parser.add_argument( + "-c", "--compression", help="Specify the compression type", default=None + ) + parser.add_argument( + "-s", "--suffix", help="Choose the protocol type.", default="pycapnp" + ) + parser.add_argument("-m", "--mode", help="Specify the mode", default="object") + parser.add_argument( + "-i", + "--iters", + help="Specify the number of iterations manually. By default, it will be looked up in preset table", + default=10, + type=int, + ) + parser.add_argument( + "-r", + "--reuse", + help="If this flag is passed, objects will be re-used", + action="store_true", + ) + parser.add_argument( + "-I", + "--includes", + help="Directories to add to PYTHONPATH", + default="/usr/local/include", + ) return parser.parse_args() + def run_test(name, mode, reuse, compression, iters, suffix, includes): tic = default_timer() name = name sys.path.append(includes) - module = import_module(name + '_' + suffix) + module = import_module(name + "_" + suffix) benchmark = module.Benchmark(compression=compression) do_benchmark(mode=mode, benchmark=benchmark, iters=iters, reuse=reuse) toc = default_timer() return toc - tic + def main(): args = parse_args() run_test(**vars(args)) -if __name__ == '__main__': - main() \ No newline at end of file + +if __name__ == "__main__": + main() diff --git a/examples/async_calculator_client.py b/examples/async_calculator_client.py index a2a3e3c..c3e3e52 100755 --- a/examples/async_calculator_client.py +++ b/examples/async_calculator_client.py @@ -299,9 +299,11 @@ async def main(connection): print("PASS") + async def cmd_main(host): host, port = host.split(":") await main(await capnp.AsyncIoStream.create_connection(host=host, port=port)) + if __name__ == "__main__": asyncio.run(cmd_main(parse_args().host)) diff --git a/test/test_capability.py b/test/test_capability.py index 72fac7b..158ec7e 100644 --- a/test/test_capability.py +++ b/test/test_capability.py @@ -305,7 +305,7 @@ async def test_double_send(): class PromiseJoinServer(capability.TestPipeline.Server): async def getCap(self, n, inCap, _context, **kwargs): res = await inCap.foo(i=n) - response = await inCap.foo(i = int(res.x) + 1) + response = await inCap.foo(i=int(res.x) + 1) _results = _context.results _results.s = response.x + "_bar" _results.outBox.cap = inCap diff --git a/test/test_rpc.py b/test/test_rpc.py index 4028ec2..c6ae055 100644 --- a/test/test_rpc.py +++ b/test/test_rpc.py @@ -19,8 +19,8 @@ class Server(test_capability_capnp.TestInterface.Server): async def test_simple_rpc_with_options(): read, write = socket.socketpair() - read = await capnp.AsyncIoStream.create_connection(sock = read) - write = await capnp.AsyncIoStream.create_connection(sock = write) + read = await capnp.AsyncIoStream.create_connection(sock=read) + write = await capnp.AsyncIoStream.create_connection(sock=write) _ = capnp.TwoPartyServer(write, bootstrap=Server()) # This traversal limit is too low to receive the response in, so we expect @@ -36,8 +36,8 @@ async def test_simple_rpc_with_options(): async def test_simple_rpc_bootstrap(): read, write = socket.socketpair() - read = await capnp.AsyncIoStream.create_connection(sock = read) - write = await capnp.AsyncIoStream.create_connection(sock = write) + read = await capnp.AsyncIoStream.create_connection(sock=read) + write = await capnp.AsyncIoStream.create_connection(sock=write) _ = capnp.TwoPartyServer(write, bootstrap=Server(100)) client = capnp.TwoPartyClient(read) diff --git a/test/test_rpc_calculator.py b/test/test_rpc_calculator.py index d101bdf..19317c9 100644 --- a/test/test_rpc_calculator.py +++ b/test/test_rpc_calculator.py @@ -1,6 +1,5 @@ import gc import os -import pytest import socket import sys # add examples dir to sys.path @@ -15,8 +14,8 @@ import async_calculator_server # noqa: E402 async def test_calculator(): read, write = socket.socketpair() - read = await capnp.AsyncIoStream.create_connection(sock = read) - write = await capnp.AsyncIoStream.create_connection(sock = write) + read = await capnp.AsyncIoStream.create_connection(sock=read) + write = await capnp.AsyncIoStream.create_connection(sock=write) _ = capnp.TwoPartyServer(write, bootstrap=async_calculator_server.CalculatorImpl()) await async_calculator_client.main(read) @@ -31,8 +30,8 @@ async def test_calculator_gc(): return call read, write = socket.socketpair() - read = await capnp.AsyncIoStream.create_connection(sock = read) - write = await capnp.AsyncIoStream.create_connection(sock = write) + read = await capnp.AsyncIoStream.create_connection(sock=read) + write = await capnp.AsyncIoStream.create_connection(sock=write) # inject a gc.collect to the beginning of every evaluate_impl call evaluate_impl_orig = async_calculator_server.evaluate_impl