def main(): server, = parse_args() print("Running %d iterations for each parameter..." % NUM_ITERATIONS) header = "%-15s | %-16s | %-16s | %-7s | %-14s | %-6s" % \ ("Binary Protocol", "Unicode Literals", "Packer Type", "Elapsed", "Iteration Rate", "Passed") print(header) print("=" * len(header)) # Test for both Python 2.x and 3.x... unicode_literal = str("") if sys.version_info[0] == 3 else unicode("") is_unicode = isinstance("", type(unicode_literal)) for binary in (True, False): for packer_type, packer_name in ((KT_PACKER_PICKLE, "KT_PACKER_PICKLE"), (KT_PACKER_JSON, "KT_PACKER_JSON"), (KT_PACKER_STRING, "KT_PACKER_STRING"), (KT_PACKER_BYTES, "KT_PACKER_BYTES"), (KT_PACKER_CUSTOM, "MemcachePacker()")): if packer_type == KT_PACKER_CUSTOM: memc_packer = MemcachePacker(gzip_enabled=False) kt = KyotoTycoon(binary=binary, pack_type=KT_PACKER_CUSTOM, custom_packer=memc_packer) else: kt = KyotoTycoon(binary=binary, pack_type=packer_type) with kt.connect(server["host"], server["port"], timeout=2) as db: start = time() bad = 0 for i in range(NUM_ITERATIONS): if is_unicode: key = "key-%d-u-%d-%d-café" % (binary, packer_type, i) value = "value-%d-u-%d-%d-café" % (binary, packer_type, i) else: key = "key-%d-s-%d-%d-cafe" % (binary, packer_type, i) value = "value-%d-s-%d-%d-cafe" % (binary, packer_type, i) # In this case we must feed it bytes, and have to encode them... if packer_type in (KT_PACKER_BYTES, KT_PACKER_CUSTOM): value = value.encode("utf-8") db.set(key, value) output = db.get(key) if output != value: bad += 1 db.remove(key) output = db.get(key) if output is not None: bad += 1 duration = time() - start rate = NUM_ITERATIONS / duration print("%-15s | %-16s | %-16s | %5.2f s | %10.2f ips | %-6s" % (binary, is_unicode, packer_name, duration, rate, "No" if bad > 0 else "Yes"))