-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_async.py
77 lines (56 loc) · 2.46 KB
/
benchmark_async.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import asyncio
import random
import time
import asyncmy
from benchmark_base import BenchmarkBase
class AsyncBenchmark(BenchmarkBase):
def __init__(self, num_batches=10):
super().__init__()
self.num_batches = num_batches # Number of queries to run in parallel at once
async def insert(self):
start_time = time.time()
async def insert_query(_):
async with self.pool.acquire() as connection:
async with connection.cursor() as cursor:
data = f"Sample data {random.randint(1, 1000)}"
await cursor.execute(self.insert_query, (data,))
await connection.commit()
await self._run_in_batches(insert_query)
end_time = time.time()
return end_time - start_time
async def select(self):
start_time = time.time()
async def select_query(i):
async with self.pool.acquire() as connection:
async with connection.cursor() as cursor:
await cursor.execute(self.select_query, (i,))
await cursor.fetchone()
await self._run_in_batches(select_query)
end_time = time.time()
return end_time - start_time
async def update(self):
start_time = time.time()
async def update_query(i):
async with self.pool.acquire() as connection:
async with connection.cursor() as cursor:
new_data = f"Updated data {random.randint(1, 1000)}"
await cursor.execute(self.update_query, (new_data, i))
await connection.commit()
await self._run_in_batches(update_query)
end_time = time.time()
return end_time - start_time
async def delete(self):
start_time = time.time()
async def delete_query(_):
async with self.pool.acquire() as connection:
async with connection.cursor() as cursor:
await cursor.execute(self.delete_query)
await connection.commit()
await self._run_in_batches(delete_query)
end_time = time.time()
return end_time - start_time
async def _run_in_batches(self, query_func):
# Run queries in smaller batches
for i in range(0, self.NUM_QUERIES, self.num_batches):
batch = range(i + 1, min(i + self.num_batches + 1, self.NUM_QUERIES + 1))
await asyncio.gather(*(query_func(j) for j in batch))