-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathbenchmarks_vs_libs.py
More file actions
219 lines (165 loc) · 6.25 KB
/
Copy pathbenchmarks_vs_libs.py
File metadata and controls
219 lines (165 loc) · 6.25 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
"""Compare aiochclient against the popular Python ClickHouse clients.
Real-world-ish workload: a wide-ish, mixed-type table; fully materialize every
row on SELECT and bulk-insert rows on INSERT. Throughput is reported as
rows/sec (best of N sequential runs, single connection, no async fan-out).
Run: python benchmarks_vs_libs.py
Needs a ClickHouse server with HTTP (8123) and native (9000) ports.
"""
import asyncio
import time
from datetime import date, datetime
from aiohttp import ClientSession
from aiochclient import ChClient
HTTP_PORT = 8123
NATIVE_PORT = 9000
ROWS = 50_000
RETRIES = 8
DDL = """
CREATE TABLE bench_libs (
id UInt32,
value Int64,
score Float64,
name String,
created Date,
ts DateTime,
tags Array(String),
opt Nullable(Int32)
) ENGINE = Memory
"""
COLUMNS = ["id", "value", "score", "name", "created", "ts", "tags", "opt"]
_DATE = date(2021, 6, 1)
_TS = datetime(2021, 6, 1, 12, 30, 0)
def make_rows(n):
return [
(
i,
i * 1000,
3.14159,
f"name_{i}",
_DATE,
_TS,
["alpha", "beta", "gamma"],
(i if i % 2 else None),
)
for i in range(n)
]
ROWS_DATA = make_rows(ROWS)
def speed(seconds):
return int(ROWS / seconds)
async def best(coro_factory):
await coro_factory() # warmup
best_time = float("inf")
for _ in range(RETRIES):
start = time.perf_counter()
await coro_factory()
best_time = min(best_time, time.perf_counter() - start)
return speed(best_time)
def best_sync(fn):
fn() # warmup
best_time = float("inf")
for _ in range(RETRIES):
start = time.perf_counter()
fn()
best_time = min(best_time, time.perf_counter() - start)
return speed(best_time)
# ---------------------------------------------------------------- aiochclient
async def bench_aiochclient(engine):
async with ClientSession() as session:
prep = ChClient(session)
await prep.execute("DROP TABLE IF EXISTS bench_libs")
await prep.execute(DDL)
client = ChClient(
session,
binary=(engine == "rowbinary"),
native=(engine == "native"),
)
# INSERT
async def do_insert():
await prep.execute("TRUNCATE TABLE bench_libs")
await client.execute("INSERT INTO bench_libs VALUES", *ROWS_DATA)
ins = await best(do_insert)
# SELECT (materialize every row)
await prep.execute("TRUNCATE TABLE bench_libs")
await client.execute("INSERT INTO bench_libs VALUES", *ROWS_DATA)
async def do_select():
rows = await client.fetch("SELECT * FROM bench_libs")
return [row[:] for row in rows]
sel = await best(do_select)
return sel, ins
# ----------------------------------------------------------- clickhouse-connect
async def bench_clickhouse_connect():
import clickhouse_connect
client = await clickhouse_connect.get_async_client(host="localhost", port=HTTP_PORT)
await client.command("DROP TABLE IF EXISTS bench_libs")
await client.command(DDL)
async def do_insert():
await client.command("TRUNCATE TABLE bench_libs")
await client.insert("bench_libs", ROWS_DATA, column_names=COLUMNS)
ins = await best(do_insert)
await client.command("TRUNCATE TABLE bench_libs")
await client.insert("bench_libs", ROWS_DATA, column_names=COLUMNS)
async def do_select():
return (await client.query("SELECT * FROM bench_libs")).result_rows
sel = await best(do_select)
await client.close()
return sel, ins
# ----------------------------------------------------------------------- asynch
async def bench_asynch():
import asynch
conn = asynch.Connection(host="localhost", port=NATIVE_PORT)
await conn.connect()
async with conn.cursor() as cur:
await cur.execute("DROP TABLE IF EXISTS bench_libs")
await cur.execute(DDL)
async def do_insert():
await cur.execute("TRUNCATE TABLE bench_libs")
await cur.execute("INSERT INTO bench_libs VALUES", ROWS_DATA)
ins = await best(do_insert)
await cur.execute("TRUNCATE TABLE bench_libs")
await cur.execute("INSERT INTO bench_libs VALUES", ROWS_DATA)
async def do_select():
await cur.execute("SELECT * FROM bench_libs")
return await cur.fetchall()
sel = await best(do_select)
await conn.close()
return sel, ins
# ------------------------------------------------------------- clickhouse-driver
def bench_clickhouse_driver():
from clickhouse_driver import Client
client = Client(host="localhost", port=NATIVE_PORT)
client.execute("DROP TABLE IF EXISTS bench_libs")
client.execute(DDL)
def do_insert():
client.execute("TRUNCATE TABLE bench_libs")
client.execute("INSERT INTO bench_libs VALUES", ROWS_DATA)
ins = best_sync(do_insert)
client.execute("TRUNCATE TABLE bench_libs")
client.execute("INSERT INTO bench_libs VALUES", ROWS_DATA)
def do_select():
return client.execute("SELECT * FROM bench_libs")
sel = best_sync(do_select)
client.disconnect()
return sel, ins
async def main():
results = []
async def run(label, factory):
try:
sel, ins = await factory()
results.append((label, sel, ins))
print(f" {label:<32} select {sel:>8} rows/sec | insert {ins:>8} rows/sec")
except Exception as exc: # noqa: BLE001
results.append((label, None, None))
print(f" {label:<32} ERROR: {type(exc).__name__}: {exc}")
print(f"Benchmark: {ROWS} rows, best of {RETRIES} runs\n")
await run("aiochclient (HTTP, TSV)", lambda: bench_aiochclient("tsv"))
await run("aiochclient (HTTP, RowBinary)", lambda: bench_aiochclient("rowbinary"))
await run("aiochclient (HTTP, Native)", lambda: bench_aiochclient("native"))
await run("clickhouse-connect (HTTP)", bench_clickhouse_connect)
await run("asynch (native, async)", bench_asynch)
await run(
"clickhouse-driver (native, sync)",
lambda: asyncio.get_event_loop().run_in_executor(None, bench_clickhouse_driver),
)
return results
if __name__ == "__main__":
asyncio.run(main())