json-server
json-server (75k stars, MIT, commit 89a34a4,
1.0.0-beta.15) ஒரு JSON file-ல இருந்து REST API serve பண்ணுது: Node, tinyhttp மற்றும் lowdb.
whole-app flow (app record, front-door, wire, lift) வெச்சு முழுசா end-to-end port பண்ணப்பட்ட
முதல் app இதுதான்: இதன் விளைவா Node server என்ன பதில் சொல்லுதோ அதையே சொல்ற ஒரே ஒரு Rust binary
கிடைச்சிருக்கு. எல்லாமே
demo/real-world/json-server-rs-ல இருக்கு.
| Step | Result |
|---|---|
analyze --whole-app |
ஒரு server, 9 units (7 ready, 2 caution), replace பண்ண வேண்டிய 13 dependencies (tinyhttp, lowdb, sirv, eta, milliparsec, dot-prop, sort-on, inflection...) |
app record --http |
514 cases: --inputs மூலம் கொடுத்த 268 requests (எல்லா route மற்றும் method, where operators, sort, pagination, embeds, CORS preflight, static files மற்றும் ranges, 29 malformed JSON bodies, /__proto__ மற்றும் மத்த prototype names, bad escapes) மற்றும் 245 traps; ஒவ்வொண்ணும் ரெண்டு தடவை record ஆச்சு, random ids [..]-ஆ விடப்பட்டிருக்கு |
front-door |
99.8% fallback-ல 513/514; தவறினது URL-ல இருந்த raw ü மட்டும், இதுக்கு Node 400 பதில் சொல்லும் ஆனா proxy அதை re-encode பண்ணிடுது |
| units | where-operators 207, parse-where 289, matches-where 1,346, paginate 853 cases (json-server-ஓட சொந்த tests-ல இருந்து வந்த calls உட்பட); service 461 மற்றும் app 243 (Service calls மற்றும் real HTTP requests run பண்ற ரெண்டு சின்ன adapters வழியா); ஆறுலயும் gate pass ஆகுது |
wire |
முதல்ல reads: 22.6% fallback-ல 514/514, writes-க்கு Node பதில் சொல்லும் மற்றும் Node எழுதின file-ஐ Rust reload பண்ணும்; அப்புறம் எல்லா routes-உம்: 0%-ல 514/514 |
lift |
ஒரு pure Rust binary (5.3 MB), 514/514; bin.ts-ஓட command line கையால port பண்ணப்பட்டது (help, version, file checks, startup banner) |
lift பண்ணதுக்கு அப்புறம் units-க்கு bindings இருக்காது, அதனால ஒவ்வொரு crate-ஓட tests/recorded.rs-உம்
அதோட record ஆன cases-ஐ port மேல replay பண்ணுது: மொத்த 3,399 cases-உம் cargo test-ல run ஆகுது.
Record ஆன cases மூலமா பிடிச்சு, port சரியா செய்ய வேண்டியிருந்த விஷயங்கள்:
- tinyhttp-ஓட router ஒரே தடவை dispatch பண்றதில்ல: ஒவ்வொரு
next()-க்கு அப்புறமும் மறுபடியும் எல்லா middleware மற்றும் routes-ஐயும் கடைசியா middleware rewrite பண்ண URL மேல match பண்ணுது, மேலும்HEAD-க்கு எல்லா route-ஐயும் run பண்ணுது. அதனாலHEAD /postsமுதல்ல GET handler-ஐயும் அப்புறம் POST handler-ஐயும் run பண்ணி POST-ஓட 400 பதிலைத் தரும்;//postsCORS headers-ஐயும் body parser-ஐயும் skip பண்ணிடும். Port அதே loop-ஐ அப்படியே run பண்ணுது. - milliparsec ஒவ்வொரு POST, PUT, PATCH மற்றும் DELETE body-ஐயும் அது எந்த type-ஆ இருந்தாலும் JSON-ஆ parse பண்ணுது, தப்பான
ஒண்ணு வந்தா V8-ஓட message-ஓட 500 தரும்:
Expected double-quoted property name in JSON at position 7 (line 1 column 8),Unexpected token 'x', ..."bbbbbbb": x}" is not valid JSON. இந்த port V8-ஓட parser errors-ஐ, UTF-16 units-ல இருக்குற positions உட்பட reproduce பண்ணுது; 3,806 malformed inputs வெச்சு Node கூட check பண்ணப்பட்டது. - Sorting
localeCompare(ICU) பயன்படுத்துது, இது ICU4X-ஓட collator வெச்சு செய்யப்பட்டு 3,000 pairs-ல check பண்ணப்பட்டது;Number(),JSON.stringify(1e+21, integer keys முதல்ல), Node-ஓட ரெண்டு query-string parsers (இவைé%E9-ஐ WHATWG-ல இருந்து வேற மாதிரி decode பண்ணுது), அதோட error messages-ஓட dot-prop, மற்றும் embeds-க்கான inflection (personமற்றும்people) ஒவ்வொண்ணும்json-server-js-ல Node கூட check பண்ணப்பட்டிருக்கு. db.data[name]Object.prototype-ஐப் படிக்குது:/__proto__{}-னு பதில் சொல்லும்,/constructorஒரு empty 200 தரும், மற்றும்PATCH /__proto__database-ஓட prototype-ஐ மாத்திடும், அதைப் பின்னாடி வர lookups பார்க்கும்.- Raw non-ASCII targets மற்றும் unknown methods வந்தா Node-ஓட HTTP parser 400 பதில் சொல்லி connection-ஐ close பண்ணிடும்.
json-server-ல கண்டுபிடிச்சது: அதோட ஒவ்வொரு write-க்கு அப்புறமும், அதோட file watcher அந்த file-ஐ reload பண்ணும். அந்த
reload முடியறதுக்குள்ள அடுத்த request write பண்ணா, அந்த reload புது write-ஐ அழிச்சிடும்: server பதில் சொன்ன வேகத்துலயே
அனுப்பின முதல் recording-ல, 201 பதில் கொடுத்த ஒரு POST-ஐப் பின்னாடி வந்த reads காட்டல. app record --pace 50
requests நடுவுல wait பண்ணுது, அப்படித்தான் cases record ஆச்சு. வேற யாராவது file-ஐ மாத்தினா மட்டுமே இந்த port reload பண்ணும்,
அதனால அந்த write இழப்பு இருக்காது: இது தெரிஞ்சே வெச்ச ஒரு வித்தியாசம்.
Benchmark: Apple M4 (10 cores, 16 GB), macOS 27.3, npm-க்காக tsc build பண்ணின lib/bin.js-ஐ run பண்ணுற
Node 24.15.0, மற்றும் --release வெச்சு build பண்ணப்பட்ட Rust binary. ரெண்டுமே loopback-ல ஒரே database-ஓட
ஒரு copy-ஐ serve பண்ணுது; warm-up-க்கு அப்புறம் ab -k அதே requests-ஐ row-க்கு 20,000 தடவை (பெரிய database-ல மெதுவான
embed-க்கு 40 தடவை) அனுப்புது. ரெண்டு runs, ஒவ்வொண்ணும் 1-minute load average 3-க்கு கீழ இருக்கும்போது start ஆச்சு;
cells-ல run 1 / run 2 values requests per second-ல கொடுத்திருக்கு. bench/bench.py
தான் script.
Fixture database (6 posts, 3 comments):
| Request | Node, 1 connection | Rust, 1 connection | Node, 50 connections | Rust, 50 connections |
|---|---|---|---|---|
GET /posts |
11.1k / 10.8k | 27.3k / 23.9k | 19.7k / 20.4k | 115.0k / 110.7k |
GET /posts/1 |
11.0k / 11.3k | 27.7k / 25.4k | 21.9k / 22.6k | 119.9k / 99.0k |
| filter, sort, page | 10.5k / 10.0k | 25.2k / 21.5k | 18.3k / 16.4k | 107.5k / 96.3k |
?_embed=comments |
8.7k / 6.6k | 22.5k / 21.8k | 13.8k / 11.0k | 98.3k / 100.6k |
GET / (index page) |
7.3k / 6.3k | 27.6k / 21.7k | 10.2k / 9.5k | 135.1k / 119.3k |
GET /test.html (static) |
12.0k / 11.0k | 25.8k / 25.9k | 30.3k / 26.1k | 103.9k / 104.4k |
POST /comments, 10 connections |
2,973 / 2,467 | 5,821 / 5,976 |
Generate பண்ணப்பட்ட database (10,000 posts, 20,000 comments, 3.6 MB):
| Request | Node, 1 connection | Rust, 1 connection | Node, 50 (10) connections | Rust, 50 (10) connections |
|---|---|---|---|---|
GET /posts/5000 |
8.4k / 9.2k | 14.6k / 14.8k | 15.5k / 16.1k | 69.5k / 67.4k |
?_page=3&_per_page=10 |
4.6k / 4.2k | 8.8k / 8.4k | 6.5k / 6.8k | 47.4k / 46.2k |
title:contains, sorted |
431 / 396 | 600 / 560 | 465 / 453 | 2,516 / 2,329 |
?_embed=comments, 5 கொண்ட ஒரு page (10 connections) |
2.4 / 2.4 | 81 / 103 | 2.4 / 2.2 | 253 / 216 |
GET /posts, முழு list (10 connections) |
328 / 319 | 399 / 407 | 381 / 387 | 1,514 / 1,657 |
POST /comments (10 connections) |
236 / 241 | 292 / 264 |
| Node | Rust | |
|---|---|---|
| Start-ல இருந்து முதல் response, fixture database | 85 / 79 ms | 9.9 / 7.9 ms |
| Start-ல இருந்து முதல் response, 10,000 posts | 95 / 91 ms | 48 / 47 ms |
| Memory (RSS) idle, fixture database | 74 MB | 3.5 MB |
| Memory (RSS) idle, 10,000 posts | 94 MB | 41 MB |
| Runs நடக்கும்போது peak Memory (RSS), fixture database | 424 / 390 MB | 18 MB |
| Runs நடக்கும்போது peak Memory (RSS), 10,000 posts | 462 / 485 MB | 289 / 291 MB |
எங்க வித்தியாசம் கம்மியா இருக்கு: ஒரே connection-ல முழு 10,000-post list-ஐப் படிக்கும்போது (1.2x: ரெண்டுமே 1.5 MB JSON-ஐ print பண்ணுறதுல தான் நேரத்தைச் செலவிடுது), பெரிய database-ல writes (1.1 முதல் 1.2x: ரெண்டுமே ஒவ்வொரு write-லயும் முழு 3.6 MB file-ஐயும் திருப்பி எழுதுது, lowdb மாதிரி Rust-உம் queue ஆன writes-ஐ ஒண்ணா சேர்த்துடுது), மற்றும் ஒரு connection-ல sorted title search (1.4x). பெரிய லாபம் concurrency-ல இருந்து வருது — அது Node process-ல இல்ல — மற்றும் embed-ல இருந்து வருது, அங்க json-server ஒவ்வொரு post-க்கும் எல்லா comments-ஐயும் filter பண்ணுது (port ஒரு request-க்கு ஒரு தடவை comments-ஐ index பண்ணிடுது, அதே result தான்).
முதல் lift ஆன binary பெரிய database-ல Node-ஐ விட மெதுவா இருந்துச்சு (முழு list-க்கு 298-க்கு எதிரா 33 requests per second,
writes-க்கு 217-க்கு எதிரா 39). Profiling-ல வரிசையா கண்டுபிடிச்சது: TCP_NODELAY இல்ல (ஒவ்வொரு பெரிய response-உம் delayed ACK-க்காக
சுமார் 40 ms wait பண்ணுச்சு), ஒவ்வொரு write-க்கு முன்னாடியும் $schema சேர்க்க முழு database-உம் clone ஆச்சு, print பண்றதுக்கு முன்னாடி
list ஆன ஒவ்வொரு item-உம் clone ஆச்சு, எல்லா requests-க்கும் ஒரே ஒரு lock, மற்றும் 10,000 items-ல ஒவ்வொண்ணுக்கும் item.id SipHash
வெச்சு hash ஆச்சு. எல்லாமே fix ஆயிடுச்சு; ஒவ்வொரு மாற்றத்துக்கு அப்புறமும் 3,399 unit cases மற்றும் 514 HTTP cases pass ஆகுது.
Tool-ல கண்டுபிடிச்சு, fix பண்ணப்பட்டது:
- தன்னோட state-ஐ write பண்ற ஒரு server-ஐ ஒரே தொடக்கத்துல இருந்து ரெண்டு தடவை record பண்ண முடியாம இருந்துச்சு:
--fresh <path>இப்போ ஒவ்வொரு start-க்கும் cases கூட வெச்சிருக்கிற snapshot-ல இருந்து database-ஐயும் (public/-ஐயும்) ஒரு புது working directory-ல copy பண்ணுது;--argஒவ்வொரு build-க்கும் original-ஓட arguments-ஐக் கொடுக்குது;--paceமேல சொல்லியிருக்கு. bridgepaginate<T>(items: T[])-ஐ TS-லயே வெச்சிருந்துச்சு (எதுவா இருந்தாலும் generic, functions உட்பட);--as-json Tஅதை JSON values-ஆ bridge பண்ணுது.- Parameters கொண்ட front door routes
/posts/%E0-க்கு unit அந்த request-ஐப் பார்க்கிறதுக்கு முன்னாடியே axum-ஓட சொந்த 400 பதிலைத் தந்துச்சு; இப்போ parameters URI-ல இருந்து படிக்கப்படுது. liftwire ஆன unit-ஐ மட்டும் தான் stage பண்ணுச்சு, path வழியா அது depend ஆகுற crates-ஐ இல்ல, மேலும்cargo fmt --checkreject பண்ற மாதிரி ஒருlib.rsஎழுதிச்சு; ரெண்டும் fix ஆயிடுச்சு.- அது generate பண்ற servers
TCP_NODELAYset பண்ணல: ரெண்டு parts-ஆ எழுதப்பட்ட response client-ஓட delayed ACK-க்காக சுமார் 40 ms wait பண்ணுச்சு. இப்போ அது set பண்ணுது.
இது எதை cover பண்ணல: JSON5 databases (binary அப்படி சொல்லிட்டு exit ஆயிடும்), 100 KiB body limit
(Node-ஓட limit body 64 KiB reads-ல எப்படி வருதுங்கிறதைப் பொறுத்தது; port முழு reads-ஆ எடுத்துக்குது), lone surrogates உள்ள
strings (அவை U+FFFD-ஆ மாறிடும்), மற்றும் 1e400 database value (அடுத்த reload வரை Node Infinity-ஐ memory-ல வெச்சிருக்கும்;
port null store பண்ணுது, file-லயும் அதுதான் போகும்).