Repository navigation
Expand file tree
/
Copy path14_lifecycle_timing.py
More file actions
427 lines (373 loc) · 14.8 KB
/
Copy path14_lifecycle_timing.py
File metadata and controls
427 lines (373 loc) · 14.8 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env python3
"""
Example 14: Lifecycle Timing Benchmark
Measures elapsed time for:
1) JVM start
2) Database create + open
3) Mixed transaction workload (table + graph + vector data)
4) Mixed query workload (table + graph + vector queries)
5) Database close + reopen checks
Usage:
python3 14_lifecycle_timing.py
python3 14_lifecycle_timing.py --runs 5 --table-records 10000 --graph-vertices 1000
"""
from __future__ import annotations
import argparse
import random
import shutil
import tempfile
import time
import uuid
from pathlib import Path
from typing import Dict, List
import arcadedb
from arcadedb.jvm import start_jvm
def server_open_close_timing(root: str, rounds: int = 3) -> None:
"""The same question on the bundled server: what does closing and reopening
a database cost when it lives in a running server? Timed over HTTP with the
server's own commands (`close database` / `open database`), standard-library
HTTP only. There is no JVM start to pay here; the server is already up."""
import base64
import json
import shutil
import socket
import urllib.request
shutil.rmtree(root, ignore_errors=True)
with socket.socket() as sock:
sock.bind(("127.0.0.1", 0))
port = int(sock.getsockname()[1])
password = "password123" # nosec B105 - example-only local server
server = arcadedb.create_server(
root_path=root,
root_password=password,
config={"host": "127.0.0.1", "http_port": port, "mode": "development"},
)
server.start()
time.sleep(1)
base = f"http://127.0.0.1:{server.get_http_port()}"
auth = "Basic " + base64.b64encode(f"root:{password}".encode()).decode()
def cmd(command: str) -> float:
req = urllib.request.Request(
f"{base}/api/v1/server",
data=json.dumps({"command": command}).encode(),
method="POST",
)
req.add_header("Authorization", auth)
req.add_header("Content-Type", "application/json")
t0 = time.perf_counter()
with urllib.request.urlopen(
req, timeout=60
) as resp: # nosec B310 - local URL built above
resp.read()
return time.perf_counter() - t0
try:
server.create_database("lifecycle")
opens, closes = [], []
for _ in range(rounds):
closes.append(cmd("close database lifecycle"))
opens.append(cmd("open database lifecycle"))
print()
print("Server mode, the same database closed and reopened on a running server:")
print(f" close database: {_avg(closes):.6f}s avg over {rounds}")
print(f" open database: {_avg(opens):.6f}s avg over {rounds}")
finally:
server.stop()
shutil.rmtree(root, ignore_errors=True)
def _avg(values: List[float]) -> float:
return sum(values) / len(values) if values else 0.0
def _build_dummy_vector(index: int, dimensions: int) -> List[float]:
rng = random.Random(index * 7919 + dimensions * 104729)
return [rng.uniform(-1.0, 1.0) for _ in range(dimensions)]
def create_schema(db, vector_dimensions: int) -> None:
db.command("sql", "CREATE DOCUMENT TYPE TableRecord")
db.command("sql", "CREATE PROPERTY TableRecord.name STRING")
db.command("sql", "CREATE PROPERTY TableRecord.value INTEGER")
db.command("sql", "CREATE INDEX ON TableRecord (value) NOTUNIQUE")
db.command("sql", "CREATE VERTEX TYPE Node")
db.command("sql", "CREATE PROPERTY Node.node_id INTEGER")
db.command("sql", "CREATE PROPERTY Node.group_name STRING")
db.command("sql", "CREATE INDEX ON Node (node_id) UNIQUE_HASH")
db.command("sql", "CREATE EDGE TYPE CONNECTED_TO")
db.command("sql", "CREATE PROPERTY CONNECTED_TO.weight INTEGER")
db.command("sql", "CREATE VERTEX TYPE VectorDoc")
db.command("sql", "CREATE PROPERTY VectorDoc.doc_id STRING")
db.command("sql", "CREATE PROPERTY VectorDoc.embedding ARRAY_OF_FLOATS")
db.command("sql", "CREATE INDEX ON VectorDoc (doc_id) UNIQUE_HASH")
def run_single_iteration(
db_path: Path,
table_records: int,
graph_vertices: int,
vector_records: int,
vector_dimensions: int,
query_runs: int,
jvm_kwargs: Dict[str, str],
) -> Dict[str, float]:
if db_path.exists():
shutil.rmtree(db_path)
create_start = time.perf_counter()
created_db = arcadedb.create_database(str(db_path), jvm_kwargs=jvm_kwargs)
create_time_s = time.perf_counter() - create_start
schema_start = time.perf_counter()
create_schema(created_db, vector_dimensions=vector_dimensions)
schema_time_s = time.perf_counter() - schema_start
created_db.close()
open_start = time.perf_counter()
db = arcadedb.open_database(str(db_path), jvm_kwargs=jvm_kwargs)
open_time_s = time.perf_counter() - open_start
transaction_start = time.perf_counter()
transaction_breakdown_start = time.perf_counter()
with db.transaction():
for index in range(table_records):
db.command(
"sql",
"INSERT INTO TableRecord SET name = ?, value = ?",
f"record-{index}",
index,
)
for index in range(graph_vertices):
db.command(
"sql",
"INSERT INTO Node SET node_id = ?, group_name = ?",
index,
f"group-{index % 10}",
)
for index in range(1, graph_vertices):
db.command(
"sql",
"CREATE EDGE CONNECTED_TO "
"FROM (SELECT FROM Node WHERE node_id = ?) "
"TO (SELECT FROM Node WHERE node_id = ?) "
"SET weight = ?",
index - 1,
index,
(index % 100),
)
for index in range(vector_records):
db.command(
"sql",
"INSERT INTO VectorDoc SET doc_id = ?, embedding = ?",
f"vec-{index}",
arcadedb.to_java_float_array(
_build_dummy_vector(index=index, dimensions=vector_dimensions)
),
)
transaction_time_s = time.perf_counter() - transaction_start
db.command(
"sql",
f"""
CREATE INDEX ON VectorDoc (embedding)
LSM_VECTOR
METADATA {{
"dimensions": {int(vector_dimensions)},
"similarity": "COSINE"
}}
""",
)
load_time_s = time.perf_counter() - transaction_breakdown_start
query_start = time.perf_counter()
for query_index in range(query_runs):
db.query(
"sql",
"SELECT count(*) AS count FROM TableRecord WHERE value >= ?",
query_index % max(1, table_records),
).first()
db.query(
"sql",
"SELECT count(*) AS count FROM Node",
).first()
db.query(
"sql",
"SELECT count(*) AS count FROM CONNECTED_TO",
).first()
query_vector = _build_dummy_vector(index=999_999, dimensions=vector_dimensions)
query_vector_sql = "[" + ", ".join(str(v) for v in query_vector) + "]"
for _ in range(query_runs):
db.query(
"sql",
f"SELECT vectorNeighbors('VectorDoc[embedding]', {query_vector_sql}, 5) AS res",
).first()
query_time_s = time.perf_counter() - query_start
close_start = time.perf_counter()
db.close()
close_time_s = time.perf_counter() - close_start
reopen_start = time.perf_counter()
reopened_db = arcadedb.open_database(str(db_path), jvm_kwargs=jvm_kwargs)
reopen_time_s = time.perf_counter() - reopen_start
reopen_query_start = time.perf_counter()
for query_index in range(query_runs):
reopened_db.query(
"sql",
"SELECT count(*) AS count FROM TableRecord WHERE value >= ?",
query_index % max(1, table_records),
).first()
reopened_db.query("sql", "SELECT count(*) AS count FROM Node").first()
reopened_db.query("sql", "SELECT count(*) AS count FROM CONNECTED_TO").first()
for _ in range(query_runs):
reopened_db.query(
"sql",
f"SELECT vectorNeighbors('VectorDoc[embedding]', {query_vector_sql}, 5) AS res",
).first()
reopen_query_time_s = time.perf_counter() - reopen_query_start
reopen_close_start = time.perf_counter()
reopened_db.close()
reopen_close_time_s = time.perf_counter() - reopen_close_start
return {
"create_time_s": create_time_s,
"schema_time_s": schema_time_s,
"open_time_s": open_time_s,
"transaction_time_s": transaction_time_s,
"load_time_s": load_time_s,
"query_time_s": query_time_s,
"close_time_s": close_time_s,
"reopen_time_s": reopen_time_s,
"reopen_query_time_s": reopen_query_time_s,
"reopen_close_time_s": reopen_close_time_s,
}
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Example 14: Lifecycle Timing Benchmark"
)
parser.add_argument(
"--db-path",
default="",
help="Optional benchmark database directory path. If omitted, uses random /tmp path.",
)
parser.add_argument(
"--runs",
type=int,
default=5,
help="Number of repeated runs (default: 5)",
)
parser.add_argument(
"--table-records",
type=int,
default=50000,
help="Number of table records inserted in transaction (default: 50000)",
)
parser.add_argument(
"--graph-vertices",
type=int,
default=10000,
help="Number of graph vertices inserted in transaction (default: 10000)",
)
parser.add_argument(
"--vector-records",
type=int,
default=10000,
help="Number of vector vertices inserted in transaction (default: 10000)",
)
parser.add_argument(
"--vector-dimensions",
type=int,
default=64,
help="Dummy vector dimensions (default: 64)",
)
parser.add_argument(
"--query-runs",
type=int,
default=100,
help="Number of repeated table/vector queries per run (default: 100)",
)
parser.add_argument(
"--jvm-heap",
default="2g",
help="Heap size passed to JVM startup (default: 2g)",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
if args.db_path:
db_path = Path(args.db_path)
else:
db_path = Path(tempfile.gettempdir()) / f"arcadedb_lifecycle_{uuid.uuid4().hex}"
if args.runs < 1:
raise ValueError("--runs must be >= 1")
if args.table_records < 1:
raise ValueError("--table-records must be >= 1")
if args.graph_vertices < 2:
raise ValueError("--graph-vertices must be >= 2")
if args.vector_records < 1:
raise ValueError("--vector-records must be >= 1")
if args.vector_dimensions < 2:
raise ValueError("--vector-dimensions must be >= 2")
if args.query_runs < 1:
raise ValueError("--query-runs must be >= 1")
try:
print("=" * 70)
print("🎮 ArcadeDB Python - Example 14: Lifecycle Timing Benchmark")
print("=" * 70)
print(f"DB path: {db_path}")
print(f"Runs: {args.runs}")
print(f"Table records: {args.table_records}")
print(f"Graph vertices: {args.graph_vertices}")
print(f"Vector records: {args.vector_records}")
print(f"Vector dimensions: {args.vector_dimensions}")
print(f"Query runs: {args.query_runs}")
print()
jvm_start_begin = time.perf_counter()
start_jvm(heap_size=args.jvm_heap)
jvm_start_time_s = time.perf_counter() - jvm_start_begin
print(f"JVM start time: {jvm_start_time_s:.6f}s")
print()
runs: List[Dict[str, float]] = []
jvm_kwargs = {"heap_size": args.jvm_heap}
for run_index in range(1, args.runs + 1):
timings = run_single_iteration(
db_path=db_path,
table_records=args.table_records,
graph_vertices=args.graph_vertices,
vector_records=args.vector_records,
vector_dimensions=args.vector_dimensions,
query_runs=args.query_runs,
jvm_kwargs=jvm_kwargs,
)
runs.append(timings)
print(
f"Run {run_index:02d} | "
f"create: {timings['create_time_s']:.6f}s | "
f"schema: {timings['schema_time_s']:.6f}s | "
f"open: {timings['open_time_s']:.6f}s | "
f"transaction: {timings['transaction_time_s']:.6f}s | "
f"query: {timings['query_time_s']:.6f}s | "
f"close: {timings['close_time_s']:.6f}s | "
f"reopen: {timings['reopen_time_s']:.6f}s | "
f"reopen_query: {timings['reopen_query_time_s']:.6f}s | "
f"reopen_close: {timings['reopen_close_time_s']:.6f}s"
)
server_open_close_timing(str(db_path) + "_server")
summary = {
"jvm_start_time_s": jvm_start_time_s,
"create_time_s_avg": _avg([row["create_time_s"] for row in runs]),
"schema_time_s_avg": _avg([row["schema_time_s"] for row in runs]),
"open_time_s_avg": _avg([row["open_time_s"] for row in runs]),
"transaction_time_s_avg": _avg([row["transaction_time_s"] for row in runs]),
"load_time_s_avg": _avg([row["load_time_s"] for row in runs]),
"query_time_s_avg": _avg([row["query_time_s"] for row in runs]),
"close_time_s_avg": _avg([row["close_time_s"] for row in runs]),
"reopen_time_s_avg": _avg([row["reopen_time_s"] for row in runs]),
"reopen_query_time_s_avg": _avg(
[row["reopen_query_time_s"] for row in runs]
),
"reopen_close_time_s_avg": _avg(
[row["reopen_close_time_s"] for row in runs]
),
}
print()
print("Averages")
print(f" jvm start: {summary['jvm_start_time_s']:.6f}s")
print(f" create: {summary['create_time_s_avg']:.6f}s")
print(f" schema: {summary['schema_time_s_avg']:.6f}s")
print(f" open: {summary['open_time_s_avg']:.6f}s")
print(f" transaction: {summary['transaction_time_s_avg']:.6f}s")
print(f" load: {summary['load_time_s_avg']:.6f}s")
print(f" query: {summary['query_time_s_avg']:.6f}s")
print(f" close: {summary['close_time_s_avg']:.6f}s")
print(f" reopen: {summary['reopen_time_s_avg']:.6f}s")
print(f" reopen query:{summary['reopen_query_time_s_avg']:.6f}s")
print(f" reopen close:{summary['reopen_close_time_s_avg']:.6f}s")
finally:
if db_path.exists():
shutil.rmtree(db_path)
print(f"Removed benchmark database: {db_path}")
if __name__ == "__main__":
main()