File tree Expand file tree Collapse file tree 4 files changed +83
-0
lines changed Expand file tree Collapse file tree 4 files changed +83
-0
lines changed Original file line number Diff line number Diff line change 1+ To run:
2+
3+ ``` bash
4+ $ bazel run --config=release @workerd//src/workerd/server:workerd -- serve $PWD /deps/workerd/samples/python-benchmark/config.capnp
5+
6+ $ wrk -t4 -c64 -d30s --latency -s bench.lua http://127.0.0.1:8080
7+
8+ $ curl -v -X POST http://127.0.0.1:8080
9+ ```
Original file line number Diff line number Diff line change 1+ wrk .method = " POST"
2+ wrk .body = ' {"iters": 500}'
3+ wrk .headers [" Content-Type" ] = " application/json"
Original file line number Diff line number Diff line change 1+ using Workerd = import "/workerd/workerd. capnp" ;
2+
3+ const config :Workerd.Config = (
4+ services = [
5+ (name = "main" , worker = .mainWorker),
6+ ],
7+
8+ sockets = [
9+ # Serve HTTP on port 8080.
10+ ( name = "http" ,
11+ address = "*:8080" ,
12+ http = (),
13+ service = "main"
14+ ),
15+ ],
16+ );
17+
18+ const mainWorker :Workerd.Worker = (
19+ modules = [
20+ (name = "worker. py" , pythonModule = embed ". /worker. py" ),
21+ ],
22+ compatibilityDate = "2025-11-15" ,
23+ compatibilityFlags = ["python_workers" ],
24+ );
Original file line number Diff line number Diff line change 1+ import json
2+
3+ from js import Response
4+ from workers import WorkerEntrypoint
5+
6+ DEFAULT_ITERATIONS = 500
7+
8+
9+ def json_loop (n : int ) -> int :
10+ count = 0
11+ for i in range (n ):
12+ obj = {
13+ "id" : i ,
14+ "name" : "item-" + str (i ),
15+ "values" : [i , i * 2 , i * 3 ],
16+ "nested" : {"flag" : (i % 2 == 0 ), "index" : i },
17+ }
18+ s = json .dumps (obj )
19+ parsed = json .loads (s )
20+ count += parsed ["id" ]
21+ return count
22+
23+
24+ class Default (WorkerEntrypoint ):
25+ async def fetch (self , request ):
26+ iters = DEFAULT_ITERATIONS
27+
28+ try :
29+ body_text = await request .text ()
30+ if body_text :
31+ data = json .loads (body_text )
32+ iters = int (data .get ("iters" , iters ))
33+ except Exception :
34+ iters = DEFAULT_ITERATIONS
35+
36+ result = json_loop (iters )
37+
38+ return Response .new (
39+ json .dumps ({"result" : result , "iters" : iters }),
40+ {"content-type" : "application/json" },
41+ )
42+
43+
44+ def test ():
45+ iters = DEFAULT_ITERATIONS
46+ result = json_loop (iters )
47+ print (f"Ran json_loop({ iters } ) -> { result } " )
You can’t perform that action at this time.
0 commit comments