def test_async_rpc(self): s = simplerpc.Server() class LazyMath(MathService): def gcd(self, a, b): print "server zzz..." time.sleep(1) print "server wake up" while True: r = a % b if r == 0: return b else: a = b b = r s.reg_svc(LazyMath()) s.start("0.0.0.0:8848") c = simplerpc.Client() c.connect("127.0.0.1:8848") mp = MathProxy(c) n_jobs = 10 fu_all = [] for i in range(n_jobs): print "client calling..." fu = mp.async_gcd(124, 84) fu_all += fu, for fu in fu_all: print "client waiting..." print "error code: %d" % fu.error_code print "client got result:", fu.result
def test_timedwait(self): class BS(BenchmarkService): def sleep(self, sec): time.sleep(sec) s = simplerpc.Server() s.reg_svc(BS()) s.start("0.0.0.0:8848") c = simplerpc.Client() c.connect("127.0.0.1:8848") bp = BenchmarkProxy(c) fu = bp.async_sleep(2.3) fu.wait(1.0) print "done"
def test_service_gen(self): s = simplerpc.Server() class MyMath(MathService): def gcd(self, a, b): while True: r = a % b if r == 0: return b else: a = b b = r s.reg_svc(MyMath()) s.start("0.0.0.0:8848") c = simplerpc.Client() c.connect("127.0.0.1:8848") # raw marshal handling print c.sync_call(MathService.GCD, [124, 84], ["rpc::i64", "rpc::i64"], ["rpc::i64"]) mp = MathProxy(c) print mp.sync_gcd(124, 84) print "begin 10000 sync_gcd operation" start = time.time() for i in range(10000): mp.sync_gcd(124, 84) print "done 10000 sync_gcd operation" end = time.time() print "qps = %.2lf" % (10000.0 / (end - start)) n_async = 40000 print "begin 100000 async_gcd operation" start = time.time() fu_list = [] for i in range(n_async): fu_list += mp.async_gcd(124, 84), print "now waiting..." for fu in fu_list: fu.wait() print "done 100000 async_gcd operation" end = time.time() print "qps = %.2lf" % (n_async * 1.0 / (end - start)) c.close()
def main(): argparser = argparse.ArgumentParser(prog=sys.argv[0]) argparser.add_argument("-c", dest="client_addr", help="server address (ip:port)") argparser.add_argument("-s", dest="server_addr", help="client address (ip:port)") opt = argparser.parse_args(sys.argv[1:]) if opt.client_addr: c = simplerpc.Client() c.connect(opt.client_addr) bp = BenchmarkProxy(c) counter = 0 last_time = time.time() while True: now = time.time() if now - last_time > 1.0: print "qps=%d" % (counter / (now - last_time)) last_time = now counter = 0 bp.sync_nop("") counter += 1 elif opt.server_addr: s = simplerpc.Server() class MyBenchmarkService(BenchmarkService): def nop(self, in0): pass s.reg_svc(MyBenchmarkService()) s.start(opt.server_addr) while True: time.sleep(1) else: argparser.print_help()
def run(self): c = simplerpc.Client() c.connect("127.0.0.1:8848") mp = MathProxy(c) for i in range(n_jobs): mp.sync_gcd(124, 84)
#!/usr/bin/env python import os import sys sys.path += os.path.abspath( os.path.join(os.path.split(__file__)[0], "../pylib")), import simplerpc from floodtest_service import * fpath = os.path.join(os.path.split(__file__)[0], "floodtest-servers.txt") with open(fpath) as f: nodelist = [] for l in f: l = l.strip() if l.startswith("#") or l == "": continue nodelist += l, for n in nodelist: c = simplerpc.Client() print "connect to %s" % n c.connect(n) proxy = FloodProxy(c) proxy.sync_update_node_list(nodelist)