Skip to content

Commit 979f375

Browse files
committed
Merge branch 'master' of github.com:krausest/js-framework-benchmark
2 parents a2eebbb + 5ff9f08 commit 979f375

File tree

14 files changed

+153
-27
lines changed

14 files changed

+153
-27
lines changed

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
20.9.0

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ There are currently ~60 framework entries in this repository. Installing (and ma
6060

6161
## 1.1 Prerequisites
6262

63-
Have _node.js (>=v20.8.0)_ installed. If you want to do yourself a favour use nvm for that. The benchmark has been tested with node v20.8.0.
63+
Have _node.js (>=v20.9.0)_ installed. If you want to do yourself a favour use nvm for that. The benchmark has been tested with node v20.9.0.
6464
Please make sure that the following command work before trying to build:
6565

6666
```
6767
> npm
6868
npm -version
6969
10.1.0
7070
> node --version
71-
v20.8.0
71+
v20.9.0
7272
```
7373

7474
## 1.2 Downloading the pre-built binaries and starting the server

server/app.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import staticRouter from "./src/static/staticRouter.js";
55
import * as ejs from "ejs";
66
import * as fastifyView from "@fastify/view";
77
import minifier from "html-minifier";
8+
import { Stream } from "stream";
9+
import toArray from "stream-to-array";
10+
import zlib from "node:zlib";
11+
import { pipeline } from "node:stream";
12+
import { promisify } from "node:util";
13+
import { getSizeRouter } from "./src/responseSize/responseSizeRouter.js";
814

915
/**
1016
* Builds the server but does not start it. Need it for testing API
@@ -23,8 +29,92 @@ function buildServer(options = {}) {
2329
},
2430
});
2531

32+
fastify.decorate("responseSize", {
33+
use_compression: false,
34+
size_uncompressed: 0,
35+
size_compressed: 0,
36+
get: function() {
37+
return {
38+
use_compression: this.use_compression,
39+
size_uncompressed: this.size_uncompressed,
40+
size_compressed: this.size_compressed,
41+
};
42+
},
43+
reset: function() {
44+
this.size_uncompressed = 0;
45+
this.size_compressed = 0;
46+
},
47+
enableCompression: function(val) {
48+
this.use_compression = val;
49+
},
50+
add: function(uncompressed, compressed) {
51+
this.size_uncompressed += uncompressed;
52+
this.size_compressed += compressed;
53+
},
54+
});
55+
56+
fastify.register(getSizeRouter);
57+
58+
fastify.addHook("onSend", async (request, reply, payload) => {
59+
// const MISSING_HEADERS_AND_HTTP = 99;
60+
let getSizeInfo = (original, compressed) => {
61+
if (!compressed) {
62+
compressed = original;
63+
} else {
64+
reply.header("Content-Length", compressed.length);
65+
reply.header("Content-Encoding", "br");
66+
}
67+
let headers = Object.entries(reply.getHeaders()).reduce((p, [e, v]) => p + `${e}: ${v} \n`, "");
68+
// console.log(request.url, reply.statusCode, "\n", headers);
69+
return {
70+
compressed: compressed.length, // + headers.length + MISSING_HEADERS_AND_HTTP,
71+
uncompressed: original.length, // + headers.length + MISSING_HEADERS_AND_HTTP
72+
};
73+
};
74+
75+
if (request.url.startsWith("/css") || reply.statusCode != 200 || !fastify.responseSize.use_compression) {
76+
return payload;
77+
} else {
78+
if (typeof payload == "string") {
79+
let { uncompressed, compressed } = getSizeInfo(payload);
80+
fastify.responseSize.add(uncompressed, compressed);
81+
console.log(
82+
`onSend: ${request.url} as string with uncompressed size ${uncompressed} sum uncompressed ${fastify.responseSize.size_uncompressed}, compressed ${fastify.responseSize.size_compressed}`
83+
);
84+
} else if (payload instanceof Stream) {
85+
return toArray(payload)
86+
.then((chunks) => {
87+
const buffer = Buffer.concat(chunks);
88+
if (buffer.length >= 1024) {
89+
let out = zlib.brotliCompressSync(buffer);
90+
let { uncompressed, compressed } = getSizeInfo(buffer, out);
91+
fastify.responseSize.add(uncompressed, compressed);
92+
console.log(
93+
`onSend: ${request.url} as stream with uncompressed size ${uncompressed} compressed ${compressed} sum uncompressed ${fastify.responseSize.size_uncompressed}, compressed ${fastify.responseSize.size_compressed}`
94+
);
95+
return out;
96+
} else {
97+
let { uncompressed, compressed } = getSizeInfo(buffer);
98+
fastify.responseSize.add(uncompressed, compressed);
99+
console.log(
100+
`onSend: ${request.url} as stream with uncompressed size ${uncompressed} (not compressed since below threshold) sum uncompressed ${fastify.responseSize.size_uncompressed}, compressed ${fastify.responseSize.size_compressed}`
101+
);
102+
return buffer;
103+
}
104+
})
105+
.catch((err) => {
106+
console.log("onSend: Error", err);
107+
});
108+
} else {
109+
console.log("onSend: Unknown payload type", typeof payload, payload);
110+
}
111+
return payload;
112+
}
113+
});
114+
26115
fastify.addHook("onRequest", (request, reply, done) => {
27116
if (request.url.endsWith("index.html")) {
117+
fastify.responseSize.reset();
28118
reply.header("Cross-Origin-Embedder-Policy", "require-corp");
29119
reply.header("Cross-Origin-Opener-Policy", "same-origin");
30120
}

server/package-lock.json

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"@fastify/view": "^8.2.0",
1919
"ejs": "^3.1.9",
2020
"fastify": "^4.23.2",
21-
"html-minifier": "^4.0.0"
21+
"html-minifier": "^4.0.0",
22+
"stream-to-array": "^2.3.0"
2223
},
2324
"devDependencies": {
2425
"@types/ejs": "^3.1.2",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function getSize(request, reply) {
2+
console.log("/getSize");
3+
reply.send({
4+
size_uncompressed: request.server.responseSize.size_uncompressed,
5+
size_compressed: request.server.responseSize.size_compressed,
6+
});
7+
}
8+
9+
export function enableCompression(request, reply) {
10+
console.log("/enableCompression");
11+
request.server.responseSize.enableCompression(true);
12+
reply.send("OK");
13+
}
14+
15+
export function disableCompression(request, reply) {
16+
console.log("/disableCompression");
17+
request.server.responseSize.enableCompression(false);
18+
reply.send("OK");
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { disableCompression, enableCompression, getSize } from "./responseSizeController.js";
2+
3+
export async function getSizeRouter(fastify) {
4+
fastify.get("/enableCompression", enableCompression);
5+
fastify.get("/disableCompression", disableCompression);
6+
fastify.get("/getSize", getSize);
7+
}

webdriver-ts-results/src/results.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,9 +2103,9 @@ export const results: RawResult[]=[
21032103
{"f":"udomsay-esx-v0.4.9-keyed","b":"23_update5-memory","v":{"DEFAULT":[3.17490291595459]}},
21042104
{"f":"udomsay-esx-v0.4.9-keyed","b":"25_run-clear-memory","v":{"DEFAULT":[0.8099527359008789]}},
21052105
{"f":"udomsay-esx-v0.4.9-keyed","b":"26_run-10k-memory","v":{"DEFAULT":[24.8864803314209]}},
2106-
{"f":"udomsay-esx-v0.4.9-keyed","b":"31_startup-ci","v":{"DEFAULT":[]}},
2107-
{"f":"udomsay-esx-v0.4.9-keyed","b":"32_startup-bt","v":{"DEFAULT":[0]}},
2108-
{"f":"udomsay-esx-v0.4.9-keyed","b":"33_startup-mainthreadcost","v":{"DEFAULT":[0]}},
2106+
{"f":"udomsay-esx-v0.4.9-keyed","b":"31_startup-ci","v":{"DEFAULT":[1849.8919999999998]}},
2107+
{"f":"udomsay-esx-v0.4.9-keyed","b":"32_startup-bt","v":{"DEFAULT":[12.147999999999998]}},
2108+
{"f":"udomsay-esx-v0.4.9-keyed","b":"33_startup-mainthreadcost","v":{"DEFAULT":[223.244]}},
21092109
{"f":"udomsay-esx-v0.4.9-keyed","b":"34_startup-totalbytes","v":{"DEFAULT":[151.1396484375]}},
21102110
{"f":"udomsay-tpl-v0.4.9-keyed","b":"01_run1k","v":{"total":[42.307,42.526,43.371,43.111,43.169,41.48,40.778,42.393,44.337,42.076,41.465,41.738,42.366,43.567,42.307],"script":[5.801,6.002,5.879,5.974,5.883,6.046,5.492,6.281,5.633,5.958,5.83,5.698,5.65,6.076,6.06]}},
21112111
{"f":"udomsay-tpl-v0.4.9-keyed","b":"02_replace1k","v":{"total":[45.404,44.837,45.462,45.554,46.45,46.729,45.908,45.422,45.583,44.532,45.543,45.656,45.865,45.98,48.726],"script":[9.889,10.136,10.638,10.269,10.8,11.206,11.231,10.271,10.726,9.845,10.583,11.128,10.717,10.977,11.7]}},

webdriver-ts/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"dependencies": {
3737
"chromedriver": "112.0.0",
3838
"cross-env": "7.0.3",
39-
"jstat": "1.9.6",
4039
"lighthouse": "10.1.1",
4140
"playwright": "1.33.0",
4241
"playwright-firefox": "1.33.0",

webdriver-ts/results.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)