Debian Codegraph @

benchmarks/cluster_async_pipeline.py

raw
1 import asyncio
2 import functools
3 import time
4
5 import aiovalkey_cluster
6 import avalkey
7 import uvloop
8
9 import valkey.asyncio as valkeypy
10
11
12 def timer(func):
13 @functools.wraps(func)
14 async def wrapper(*args, **kwargs):
15 tic = time.perf_counter()
16 await func(*args, **kwargs)
17 toc = time.perf_counter()
18 return f"{toc - tic:.4f}"
19
20 return wrapper
21
22
23 @timer
24 async def warmup(client):
25 await asyncio.gather(
26 *(asyncio.create_task(client.exists(f"bench:warmup_{i}")) for i in range(100))
27 )
28
29
30 @timer
31 async def run(client):
32 data_str = "a" * size
33 data_int = int("1" * size)
34
35 for i in range(count):
36 with client.pipeline() as pipe:
37 await (
38 pipe.set(f"bench:str_{i}", data_str)
39 .set(f"bench:int_{i}", data_int)
40 .get(f"bench:str_{i}")
41 .get(f"bench:int_{i}")
42 .hset("bench:hset", str(i), data_str)
43 .hget("bench:hset", str(i))
44 .incr("bench:incr")
45 .lpush("bench:lpush", data_int)
46 .lrange("bench:lpush", 0, 300)
47 .lpop("bench:lpush")
48 .execute()
49 )
50
51
52 async def main(loop):
53 arc = avalkey.StrictValkeyCluster(
54 host=host,
55 port=port,
56 password=password,
57 max_connections=2**31,
58 max_connections_per_node=2**31,
59 readonly=False,
60 reinitialize_steps=count,
61 skip_full_coverage_check=True,
62 decode_responses=False,
63 max_idle_time=count,
64 idle_check_interval=count,
65 )
66 print(f"{loop} {await warmup(arc)} avalkey")
67 print(await run(arc))
68 arc.connection_pool.disconnect()
69
70 aiorc = await aiovalkey_cluster.create_valkey_cluster(
71 [(host, port)],
72 password=password,
73 state_reload_interval=count,
74 idle_connection_timeout=count,
75 pool_maxsize=2**31,
76 )
77 print(f"{loop} {await warmup(aiorc)} aiovalkey-cluster")
78 print(await run(aiorc))
79 aiorc.close()
80 await aiorc.wait_closed()
81
82 async with valkeypy.ValkeyCluster(
83 host=host,
84 port=port,
85 password=password,
86 reinitialize_steps=count,
87 read_from_replicas=False,
88 decode_responses=False,
89 max_connections=2**31,
90 ) as rca:
91 print(f"{loop} {await warmup(rca)} valkeypy")
92 print(await run(rca))
93
94
95 if __name__ == "__main__":
96 host = "localhost"
97 port = 16379
98 password = None
99
100 count = 10000
101 size = 256
102
103 asyncio.run(main("asyncio"))
104
105 uvloop.install()
106
107 asyncio.run(main("uvloop"))