-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathspeed_test.py
executable file
·62 lines (48 loc) · 1.59 KB
/
speed_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
"""Benchmark how long it takes to set 10,000 keys in the database.
"""
import asyncio
import logging
import time
import asyncio_redis
async def main():
# Enable logging
logging.getLogger().addHandler(logging.StreamHandler())
logging.getLogger().setLevel(logging.INFO)
# connection = await asyncio_redis.Connection.create(host='localhost', port=6379)
connection = await asyncio_redis.Pool.create(
host="localhost", port=6379, poolsize=50
)
try:
# === Benchmark 1 ==
print(
"1. How much time does it take to set 10,000 values in Redis? "
"(without pipelining)"
)
print("Starting...")
start = time.time()
# Do 10,000 set requests
for i in range(10 * 1000):
await connection.set(
"key", "value"
) # By using await here, we wait for the answer.
print("Done. Duration=", time.time() - start)
print()
# === Benchmark 2 (should be at least 3x as fast) ==
print(
"2. How much time does it take if we use asyncio.gather, "
"and pipeline requests?"
)
print("Starting...")
start = time.time()
# Do 10,000 set requests
futures = [
asyncio.Task(connection.set("key", "value")) for x in range(10 * 1000)
]
await asyncio.gather(*futures)
print("Done. Duration=", time.time() - start)
finally:
connection.close()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())