-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmarks.py
More file actions
230 lines (167 loc) · 5.64 KB
/
benchmarks.py
File metadata and controls
230 lines (167 loc) · 5.64 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import sys
import time
import typing
import pypika
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import dialect
import rapidquery as rq
# Postgres dialect is faster than other dialects (according to benchmarks)
# and also providing dialect in SQLALchemy can make it faster
SA_DIALECT = dialect()
# Benchmark configuration
ITERATIONS = 100_000
WARMUP_ITERATIONS = 100
def benchmark(func: typing.Callable, number=ITERATIONS) -> float:
for _ in range(min(WARMUP_ITERATIONS, number // 10)):
func()
perf = time.perf_counter_ns()
for _ in range(number):
func()
perf = time.perf_counter_ns() - perf
return perf / 1000000
def format_results(results: typing.Dict[str, float]) -> str:
if not results:
return "No results to display"
# Find fastest time
fastest = min(results.values())
lines = []
lines.append("-" * 70)
lines.append(f"{'Library':<20} {'Time (ms)':<15} {'vs Fastest':<15} {'Status':<20}")
lines.append("-" * 70)
for lib, time_ms in sorted(results.items(), key=lambda x: x[1]):
if time_ms == fastest:
ratio = "1.00x (FASTEST)"
status = "🏆"
else:
ratio = f"{time_ms / fastest:.2f}x slower"
status = ""
lines.append(f"{lib:<20} {time_ms:>10.2f} {ratio:<15} {status}")
lines.append("-" * 70)
return "\n".join(lines)
# SELECT Query Benchmarks
def bench_select_rapidquery():
query = (
rq.SelectStatement(rq.Expr.asterisk())
.from_table("users")
.where(rq.Expr.col("name").like(r"%linus%"))
.offset(20)
.limit(20)
)
query.to_sql("postgresql")
_users_tb = sa.table("users")
def bench_select_sqlalchemy():
query = (
sa.select(sa.text("*"))
.select_from(_users_tb)
.where(sa.column("name").like(r"%linus%"))
.offset(20)
.limit(20)
)
str(query.compile(dialect=SA_DIALECT, compile_kwargs={"literal_binds": True}))
def bench_select_pypika():
query = (
pypika.Query.from_("users")
.where(pypika.Field("name").like(r"%linus%"))
.offset(20)
.limit(20)
.select("*")
)
str(query)
# INSERT Query Benchmarks
def bench_insert_rapidquery():
query = (
rq.InsertStatement("glyph")
.columns("aspect", "image")
.values(5.15, "12A")
.values(16, "14A")
.returning(rq.query.Returning("id"))
)
query.to_sql("postgresql")
sa_glyph = sa.table("glyph", sa.column("aspect", sa.Float), sa.column("image", sa.String))
def bench_insert_sqlalchemy():
query = sa.insert(sa_glyph).values(
[{"aspect": 5.15, "image": "12A"}, {"aspect": 16, "image": "14A"}]
)
str(query.compile(dialect=SA_DIALECT, compile_kwargs={"literal_binds": True}))
def bench_insert_pypika():
query = (
pypika.Query.into("glyph").columns("aspect", "image").insert(5.15, "12A").insert(16, "14A")
)
str(query)
# UPDATE Query Benchmarks
def bench_update_rapidquery():
query = (
rq.UpdateStatement("wallets")
.values(amount=rq.Expr.col("amount") + 10)
.where(rq.Expr.col("id").between(10, 30))
)
query.to_sql("postgresql")
sa_wallets = sa.table("wallets", sa.column("amount", sa.Integer), sa.column("id", sa.Integer))
def bench_update_sqlalchemy():
query = (
sa.update(sa_wallets)
.values(amount=sa_wallets.c.amount + 10)
.where(sa.between(sa_wallets.c.id, 10, 30))
)
str(query.compile(dialect=SA_DIALECT, compile_kwargs={"literal_binds": True}))
def bench_update_pypika():
query = (
pypika.Query.update("wallets")
.set("amount", pypika.Field("amount") + 10)
.where(pypika.Field("id").between(10, 30))
)
str(query)
# DELETE Query Benchmarks
def bench_delete_rapidquery():
query = rq.DeleteStatement("users").where(
rq.all(
rq.Expr.col("id") > 10,
rq.Expr.col("id") < 30,
)
)
query.to_sql("postgresql")
sa_users = sa.table("users", sa.column("id", sa.Integer))
def bench_delete_sqlalchemy():
query = sa.delete(sa_users).where(sa.and_(sa_users.c.id > 10, sa_users.c.id < 30))
str(query.compile(dialect=SA_DIALECT, compile_kwargs={"literal_binds": True}))
def bench_delete_pypika():
query = (
pypika.Query.from_("users")
.where((pypika.Field("id") > 10) & (pypika.Field("id") < 30))
.delete()
)
str(query)
def run_benchmarks():
print(f"Iterations per test: {ITERATIONS:,}")
print(f"Python version: {sys.version.split()[0]}")
print()
print("\n📊 SELECT Query Benchmark")
results = {
"RapidQuery": benchmark(bench_select_rapidquery),
"SQLAlchemy": benchmark(bench_select_sqlalchemy),
"PyPika": benchmark(bench_select_pypika),
}
print(format_results(results))
print("\n📊 INSERT Query Benchmark")
results = {
"RapidQuery": benchmark(bench_insert_rapidquery),
"SQLAlchemy": benchmark(bench_insert_sqlalchemy),
"PyPika": benchmark(bench_insert_pypika),
}
print(format_results(results))
print("\n📊 UPDATE Query Benchmark")
results = {
"RapidQuery": benchmark(bench_update_rapidquery),
"SQLAlchemy": benchmark(bench_update_sqlalchemy),
"PyPika": benchmark(bench_update_pypika),
}
print(format_results(results))
print("\n📊 DELETE Query Benchmark")
results = {
"RapidQuery": benchmark(bench_delete_rapidquery),
"SQLAlchemy": benchmark(bench_delete_sqlalchemy),
"PyPika": benchmark(bench_delete_pypika),
}
print(format_results(results))
if __name__ == "__main__":
run_benchmarks()