/ justapi / benchmarks / workloads_crud.py
swadhinbiswas p4: Rust-native SELECT/UPDATE/DELETE CRUD (ADR-056 Step C) · 7/16/2026
e1af474
45 lines · 1.4 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
"""Step C CRUD benchmark: Rust-native INSERT/SELECT/UPDATE/DELETE vs Python.

Run the Rust-native version with the route methods carrying crud_table /
crud_columns (served by crud_dispatch_bytes), and the Python version with
explicit handlers using the db_pool. Both target the same SQLite table.

Usage (Rust-native):
    python benchmarks/workloads_crud.py native 8080
Usage (Python):
    python benchmarks/workloads_crud.py python 8080
"""
import sys
import json

from justapi import JustAPIApp, Database

MODE = sys.argv[1] if len(sys.argv) > 1 else "native"
PORT = sys.argv[2] if len(sys.argv) > 2 else "8080"

DB_URL = "sqlite://:memory:"
INIT_SQL = (
    "CREATE TABLE items ("
    "id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, qty INTEGER NOT NULL)"
)

app = JustAPIApp()
app.set_database(Database(DB_URL, max_connections=1), init_sql=INIT_SQL)

COLUMNS = ["name", "qty"]


if MODE != "native":
    raise SystemExit("only 'native' mode is supported for Step C (no Python DB-exec API yet)")


# Rust-native: op inferred from HTTP method, served entirely in Rust.
app.post("/items", crud_table="items", crud_columns=COLUMNS)
app.get("/items/{id}", crud_table="items", crud_columns=COLUMNS)
app.put("/items/{id}", crud_table="items", crud_columns=COLUMNS)
app.delete("/items/{id}", crud_table="items", crud_columns=COLUMNS)


if __name__ == "__main__":
    app.run(f"127.0.0.1:{PORT}")