def downstream_put(args): """usage:: ../build/credb upstream-test --listen ../build/credb downstream-test --port 10086 --upstream localhost ./downstream_read.py downstream_put --num_ops 10000 """ upstream_client = credb.create_client("pythontestclient", args.upstream_name, args.upstream_host, args.upstream_port) downstream_client = credb.create_client("pythontestclient", args.downstream_name, args.downstream_host, args.downstream_port) def random_client(): return upstream_client if random.random() < 0.5 else downstream_client d = {} for _ in range(args.num_ops): if random.random() < 0.5: key = random_string(6) value = random_string(int(math.exp(random.random() * 10))) random_client().put(key, value) d[key] = value else: key = random.choice(list(d.keys())) expected = d[key] got = random_client().get(key) if expected != got: raise 'key: {}, expected: {}, got: {}'.format( key, expected, got) print('success')
def setup_server(): conn = credb.create_client("c", "testserver", "localhost", port=args.server_port) c = conn.get_collection('test') c.put_code("func", func)
def run_bank_client(CID): BID = CID % NUM_BANKS conn = credb.create_client("client" + str(CID), "bank" + str(BID), "localhost", port=9000 + BID) c = conn.get_collection('test') other_clients = [] for i in range(NUM_CLIENTS): if i != CID: other_clients.append(i) for i in range(NUM_OPS): other_client = sample(other_clients, 1)[0] #randint(0, NUM_CLIENTS-1) other_bank = other_client % NUM_BANKS if other_bank == BID: res = c.call("accounts.move_money_locally", [ "client" + str(CID), "client" + str(other_client), str(1), 'test' ]) else: res = c.call("accounts.move_money_remotely", [ "client" + str(CID), "client" + str(other_client), "bank" + str(other_bank), str(1), 'test' ]) if not res: raise RuntimeError("Failed to move money")
def setup_servers(): conn1 = credb.create_client("c1", "testserver1", "localhost", port=args.server_port1) conn2 = credb.create_client("c2", "testserver2", "localhost", port=args.server_port2) c1 = conn1.get_collection('test') c2 = conn2.get_collection('test') c1.put_code("func1", func1) c1.put_code("func2", func2) c2.put_code("func1", func1) c2.put_code("func2", func2) conn1.peer("localhost:42001")
def find_data(args, start_id, stop_id): random.seed(args.seed + start_id) c = credb.create_client('client', args.name, args.host, args.port) accum = 0 for i in range(start_id, stop_id): st = time.perf_counter() key, eid, doc = c.find_one('key', {'i': i}) ed = time.perf_counter() assert key == 'key' + str(i) assert doc == gen_value(i, args.obj_size) accum += ed - st print('avg find time: {}us'.format(accum / (stop_id - start_id) * 1e6))
def put_upstream(args): """usage:: ../build/credb upstream-test --listen ./downstream_read.py put --num_ops 10000 """ upstream_conn = credb.create_client("pythontestclient", args.upstream_name, args.upstream_host, args.upstream_port) c = upstream_conn.get_collection('test') d = gen_data(args.num_ops) for k, v in d.items(): c.put(k, v)
def get_downstream(args): """usage:: ../build/credb downstream-test --port 10086 --upstream localhost ./downstream_read.py get --num_ops 10000 """ downstream_conn = credb.create_client("pythontestclient", args.downstream_name, args.downstream_host, args.downstream_port) c = downstream_conn.get_collection('test') d = gen_data(args.num_ops) notfound = set(d.keys()) for k, v in d.items(): value = c.get(k) if value != v: raise 'key: {}, expected: {}, got: {}'.format(k, v, value) notfound.remove(k) if notfound: raise 'key {} not found'.format(next(iter(notfound))) print('success')
#! /usr/bin/python3 import credb c = credb.create_client("test", "testserver", "localhost") c.peer("localhost")
#! /usr/bin/python3 import credb from test import * c1 = credb.create_client("test1", "testserver1", "localhost") c2 = credb.create_client("test2", "testserver2", "localhost", port=4242) c1.put("foo", "bar") res = c2.mirror("testserver1", "foo") assert_true(res) assert_equals(c1.get("foo"), "bar") assert_equals(c2.get("foo"), "bar") c1.put("foo", "xyz") assert_equals(c2.get("foo"), "xyz") c2.put("foo", "blabla") assert_equals(c1.get("foo"), "blabla")
def load(args): c = credb.create_client('client', args.name, args.host, args.port) assert c.load_everything(args.filename)
def dump(args): c = credb.create_client('client', args.name, args.host, args.port) assert c.dump_everything(args.filename)
def create_index(args): c = credb.create_client('client', args.name, args.host, args.port) c.create_index('debug_snapshot_on_i', 'key', ['i'])
def check_data(args, start_id, stop_id): random.seed(args.seed + start_id) c = credb.create_client('client', args.name, args.host, args.port) for i in range(start_id, stop_id): assert c.get('key' + str(i)) == gen_value(i, args.obj_size)
def create_test_client(server="localhost", port=0, server_name=DEFAULT_SERVER_NAME): return credb.create_client("pythontestclient", server_name, server, port=port)
func1 = """ import ledger res = ledger.call_on_peer("testserver2", "func2", argv) print("func1 done!") return res """ func2 = """ val = int(argv[0]) res = (val % 2) == 0 print("func2 done!") return res """ c1 = credb.create_client("c1", "testserver1", "localhost", port = args.server_port1) c2 = credb.create_client("c2", "testserver2", "localhost", port = args.server_port2) c1.put_code("func1", func1) c1.peer("localhost:42001") sleep(0.5) c2.put_code("func2", func2) print("Running calls") assert_false(c1.call("func1", ["1"])) assert_true(c1.call("func1", ["2"])) assert_false(c1.call("func1", ["3"]))
def create_client(args): return credb.create_client('latency bench client', args.server_name, args.host, args.port)