Skip to content

Commit 4e63366

Browse files
authored
feat: add initial framework template (#1)
* feat: add initial framework template * fixup! feat: add initial framework template
1 parent e0da785 commit 4e63366

7 files changed

Lines changed: 948 additions & 0 deletions

File tree

.gitignore

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
tags
2+
3+
# Logs
4+
logs
5+
*.log
6+
npm-debug.log*
7+
yarn-debug.log*
8+
yarn-error.log*
9+
lerna-debug.log*
10+
.pnpm-debug.log*
11+
12+
# Diagnostic reports (https://nodejs.org/api/report.html)
13+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
14+
15+
# Runtime data
16+
pids
17+
*.pid
18+
*.seed
19+
*.pid.lock
20+
21+
# Directory for instrumented libs generated by jscoverage/JSCover
22+
lib-cov
23+
24+
# Coverage directory used by tools like istanbul
25+
coverage
26+
*.lcov
27+
28+
# nyc test coverage
29+
.nyc_output
30+
31+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
32+
.grunt
33+
34+
# Bower dependency directory (https://bower.io/)
35+
bower_components
36+
37+
# node-waf configuration
38+
.lock-wscript
39+
40+
# Compiled binary addons (https://nodejs.org/api/addons.html)
41+
build/Release
42+
43+
# Dependency directories
44+
node_modules/
45+
jspm_packages/
46+
47+
# Snowpack dependency directory (https://snowpack.dev/)
48+
web_modules/
49+
50+
# TypeScript cache
51+
*.tsbuildinfo
52+
53+
# Optional npm cache directory
54+
.npm
55+
56+
# Optional eslint cache
57+
.eslintcache
58+
59+
# Optional stylelint cache
60+
.stylelintcache
61+
62+
# Microbundle cache
63+
.rpt2_cache/
64+
.rts2_cache_cjs/
65+
.rts2_cache_es/
66+
.rts2_cache_umd/
67+
68+
# Optional REPL history
69+
.node_repl_history
70+
71+
# Output of 'npm pack'
72+
*.tgz
73+
74+
# Yarn Integrity file
75+
.yarn-integrity
76+
77+
# dotenv environment variable files
78+
.env
79+
.env.development.local
80+
.env.test.local
81+
.env.production.local
82+
.env.local
83+
84+
# parcel-bundler cache (https://parceljs.org/)
85+
.cache
86+
.parcel-cache
87+
88+
# Next.js build output
89+
.next
90+
out
91+
92+
# Nuxt.js build / generate output
93+
.nuxt
94+
dist
95+
96+
# Gatsby files
97+
.cache/
98+
# Comment in the public line in if your project uses Gatsby and not Next.js
99+
# https://nextjs.org/blog/next-9-1#public-directory-support
100+
# public
101+
102+
# vuepress build output
103+
.vuepress/dist
104+
105+
# vuepress v2.x temp and cache directory
106+
.temp
107+
.cache
108+
109+
# Docusaurus cache and generated files
110+
.docusaurus
111+
112+
# Serverless directories
113+
.serverless/
114+
115+
# FuseBox cache
116+
.fusebox/
117+
118+
# DynamoDB Local files
119+
.dynamodb/
120+
121+
# TernJS port file
122+
.tern-port
123+
124+
# Stores VSCode versions used for testing VSCode extensions
125+
.vscode-test
126+
127+
# yarn v2
128+
.yarn/cache
129+
.yarn/unplugged
130+
.yarn/build-state.yml
131+
.yarn/install-state.gz
132+
.pnp.*

.npmrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# We use save-exact because the purpose of this
2+
# repository is to find regressions, if we pull
3+
# a new version of each package the results will not be consistent
4+
save-exact=true

index.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const fs = require('node:fs/promises');
2+
const { setTimeout: delay } = require('node:timers/promises');
3+
const path = require('node:path');
4+
const { spawn } = require('node:child_process');
5+
const assert = require('node:assert');
6+
const autocannon = require('autocannon');
7+
8+
const runner = {
9+
autocannon: (opts) => {
10+
return autocannon({
11+
url: `http://localhost:${opts.http.serverPort}`,
12+
connections: 100,
13+
pipelining: 1,
14+
duration: 10 * opts.http.routes.length,
15+
requests: opts.http.routes,
16+
})
17+
},
18+
}
19+
20+
const parser = {
21+
autocannon: (settings, result) => {
22+
return {
23+
name: settings.name,
24+
method: 'autocannon',
25+
http: {
26+
totalReq: asNumber(result.requests),
27+
duration: result.duration,
28+
errors: result.errors,
29+
}
30+
};
31+
}
32+
}
33+
34+
const ALLOWED_BENCHMARKER = ['autocannon', 'benchmarkjs'];
35+
36+
function asNumber (stat) {
37+
const result = Object.create(null)
38+
for (const k of Object.keys(stat)) {
39+
result[k] = stat[k].toLocaleString(undefined, {
40+
// to show all digits
41+
maximumFractionDigits: 20
42+
})
43+
}
44+
return result
45+
}
46+
47+
function spawnServer(settings) {
48+
const server = spawn(
49+
process.execPath,
50+
[settings.http.server],
51+
{ stdio: 'inherit' },
52+
);
53+
return server;
54+
}
55+
56+
async function runBenchmark(settings) {
57+
assert.ok(settings.http.server, 'HTTP Benchmark must have a server to be spawned');
58+
assert.ok(ALLOWED_BENCHMARKER.includes(settings.benchmarker), 'Invalid settings.benchmarker');
59+
60+
let server = undefined;
61+
if (settings.type === 'http') {
62+
server = spawnServer(settings);
63+
// TODO: replace this workaround to use IPC to know when server is up
64+
await delay(1000);
65+
}
66+
67+
const benchRunner = runner[settings.benchmarker];
68+
const benchParser = parser[settings.benchmarker];
69+
const results = await benchRunner(settings);
70+
if (server) {
71+
server.kill('SIGINT');
72+
}
73+
return benchParser(settings, results);
74+
}
75+
76+
async function main() {
77+
const files = await fs.readdir(path.join(__dirname, './src'));
78+
for (const file of files) {
79+
if (file.match(/.*-benchmark\.js$/)) {
80+
const bench = require(path.join(__dirname, './src/', file));
81+
console.log(bench.name, 'results', await runBenchmark(bench));
82+
}
83+
}
84+
}
85+
86+
main();

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"dependencies": {
3+
"fastify": "4.26.1"
4+
},
5+
"devDependencies": {
6+
"autocannon": "7.15.0"
7+
}
8+
}

0 commit comments

Comments
 (0)