/ justapi / benchmarks / workloads_justapi.py
swadhinbiswas WIP save-point: production hardening, H3 removal, fmt gate, and accumulated phase work · 7/15/2026
a293d8c
64 lines · 1.5 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
from justapi import JustAPIApp
import json

app = JustAPIApp()

def hello(request):
    return {"message": "hello, world"}

app.get("/hello", hello)

async def hello_async(request):
    import asyncio
    await asyncio.sleep(0.001)
    return {"message": "hello, world async"}

app.get("/hello_async", hello_async)
def echo(request):
    try:
        data = json.loads(request["body"].decode("utf-8"))
        return data
    except Exception:
        return {
            "status": 400, 
            "headers": [(b"content-type", b"application/json")],
            "body": b'{"error":"invalid JSON"}'
        }

app.post("/echo", echo)

def validate(request):
    try:
        data = json.loads(request["body"].decode("utf-8"))
        return data
    except Exception:
        return {
            "status": 400,
            "headers": [(b"content-type", b"application/json")],
            "body": b'{"error":"invalid JSON"}',
        }

app.post("/validate", validate)

# Native fast path: same validated echo, served entirely in Rust (no Python
# handler call). Uses a real justapi Schema so the Rust validator runs.
from justapi import Schema

class NativeItemSchema(Schema):
    id: int
    name: str
    price: float

@app.post("/validate_native", schema=NativeItemSchema, native=True)
def validate_native(request):
    raise AssertionError("handler must not run in native mode")

if __name__ == "__main__":
    import sys
    port = sys.argv[1] if len(sys.argv) > 1 else "8080"
    app.run(f"127.0.0.1:{port}")

@app.post("/noop")
def noop(request):
    return {"ok": True}