-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
86 lines (60 loc) · 2.1 KB
/
benchmark.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
78
79
80
81
82
83
84
85
86
import random
import time
from benchmark_base import BenchmarkBase
class Benchmark(BenchmarkBase):
def __init__(self) -> None:
super().__init__()
def run(self):
return {
"insert": self.insert(),
"select": self.select(),
"update": self.update(),
"delete": self.delete(),
}
def insert(self):
connection, cursor = self._connect()
# Drop the table if it exists and create it again
cursor.execute(self.drop_table_query)
cursor.execute(self.create_table_query)
start_time = time.time()
# Insert 1000 rows using pymysql
for _ in range(self.NUM_QUERIES):
data = f"Sample data {random.randint(1, 1000)}"
cursor.execute(self.insert_query, (data,))
connection.commit()
end_time = time.time()
cursor.close()
connection.close()
return end_time - start_time
def select(self):
connection, cursor = self._connect()
start_time = time.time()
# Execute 1000 SELECT queries using pymysql
for i in range(1, self.NUM_QUERIES + 1):
cursor.execute(self.select_query, (i,))
cursor.fetchone()
end_time = time.time()
cursor.close()
connection.close()
return end_time - start_time
def update(self):
connection, cursor = self._connect()
start_time = time.time()
for i in range(1, self.NUM_QUERIES + 1):
new_data = f"Updated data {random.randint(1, 1000)}"
cursor.execute(self.update_query, (new_data, i))
connection.commit()
end_time = time.time()
cursor.close()
connection.close()
return end_time - start_time
def delete(self):
connection, cursor = self._connect()
start_time = time.time()
for i in range(1, self.NUM_QUERIES + 1):
cursor.execute(self.delete_query)
connection.commit()
end_time = time.time()
cursor.close()
connection.close()
return end_time - start_time