-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add benchmarking script, additional benchmarks (#407)
- Loading branch information
Showing
17 changed files
with
210 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict' | ||
|
||
const { readdirSync } = require('fs') | ||
const { spawn } = require('child_process') | ||
const { promisify } = require('node:util') | ||
const sget = promisify(require('simple-get').concat) | ||
const autocannon = require('autocannon') | ||
const delay = require('delay') | ||
const { join } = require('node:path') | ||
|
||
const benchmarkDir = join(__dirname, 'benchmark') | ||
|
||
// Keep track of spawned processes and kill if runner killed | ||
const processes = [] | ||
|
||
process.on('SIGINT', () => { | ||
for (const p of processes) { | ||
p.kill('SIGKILL') | ||
} | ||
process.exit() | ||
}) | ||
|
||
;(async function () { | ||
const benchmarkFiles = readdirSync(benchmarkDir) | ||
// don't include setup file as a benchmark | ||
.filter((fileName) => fileName !== 'setup.js') | ||
// sort by filename length to ensure base benchmarks run first | ||
.sort((a, b) => a.length - b.length) | ||
|
||
for (const benchmarkFile of benchmarkFiles) { | ||
let benchmarkProcess | ||
|
||
try { | ||
// Spawn benchmark process | ||
benchmarkProcess = spawn('node', [benchmarkFile], { | ||
detached: true, | ||
cwd: benchmarkDir | ||
}) | ||
|
||
processes.push(benchmarkProcess) | ||
|
||
// wait for `server listening` from benchmark | ||
await Promise.race([ | ||
new Promise((resolve) => { | ||
const stdOutCb = (d) => { | ||
if (d.toString().includes('server listening')) { | ||
benchmarkProcess.stdout.removeListener('data', stdOutCb) | ||
resolve() | ||
} | ||
} | ||
benchmarkProcess.stdout.on('data', stdOutCb) | ||
}), | ||
delay(5000).then(() => Promise.reject(new Error('timed out waiting for server listening'))) | ||
]) | ||
|
||
// fire single initial request as warmup | ||
await sget('http://localhost:3000/') | ||
|
||
// run autocannon | ||
const result = await autocannon({ | ||
url: 'http://localhost:3000/', | ||
connections: 100, | ||
duration: 5, | ||
pipelining: 10 | ||
}) | ||
if (result.non2xx > 0) { | ||
throw Object.assign(new Error('Some requests did not return 200'), { | ||
statusCodeStats: result.statusCodeStats | ||
}) | ||
} | ||
console.log(`${benchmarkFile}: ${result.requests.average} req/s`) | ||
} catch (err) { | ||
console.error(`${benchmarkFile}:`, err) | ||
} finally { | ||
if (benchmarkProcess) { | ||
benchmarkProcess.kill('SIGKILL') | ||
processes.pop() | ||
} | ||
} | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict' | ||
|
||
require('./setup.js')({ | ||
engine: { 'art-template': require('art-template') }, | ||
route: (req, reply) => { reply.view('index.art', { text: 'text' }) } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict' | ||
|
||
require('./setup.js')({ | ||
engine: { dot: require('dot') }, | ||
route: (req, reply) => { reply.view('testdot', { text: 'text' }) } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
require('./setup.js')({ | ||
engine: { ejs: require('ejs') }, | ||
route: (req, reply) => { reply.view('ejs-async.ejs', { text: 'text' }) }, | ||
options: { async: true } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict' | ||
|
||
require('./setup.js')({ | ||
engine: { ejs: require('ejs') }, | ||
route: (req, reply) => { reply.view('index-for-layout.ejs', { text: 'text' }) }, | ||
pluginOptions: { | ||
layout: 'layout.html' | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict' | ||
|
||
require('./setup.js')({ | ||
engine: { ejs: require('ejs') }, | ||
route: (req, reply) => { reply.view('index-for-layout.ejs', { text: 'text' }, { layout: 'layout.html' }) } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict' | ||
|
||
require('./setup.js')({ | ||
engine: { ejs: require('ejs') }, | ||
route: (req, reply) => { reply.view('index.ejs', { text: 'text' }) }, | ||
options: { | ||
useHtmlMinifier: require('html-minifier'), | ||
htmlMinifierOptions: { | ||
removeComments: true, | ||
removeCommentsFromCDATA: true, | ||
collapseWhitespace: true, | ||
collapseBooleanAttributes: true, | ||
removeAttributeQuotes: true, | ||
removeEmptyAttributes: true | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict' | ||
|
||
const { Eta } = require('eta') | ||
const eta = new Eta() | ||
|
||
require('./setup.js')({ | ||
engine: { eta }, | ||
route: (req, reply) => { reply.view('index.eta', { text: 'text' }) } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict' | ||
|
||
require('./setup.js')({ | ||
engine: { handlebars: require('handlebars') }, | ||
route: (req, reply) => { reply.view('index.html', { text: 'text' }) } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict' | ||
|
||
const { Liquid } = require('liquidjs') | ||
const liquid = new Liquid() | ||
|
||
require('./setup.js')({ | ||
engine: { liquid }, | ||
route: (req, reply) => { reply.view('index.liquid', { text: 'text' }) } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict' | ||
|
||
require('./setup.js')({ | ||
engine: { mustache: require('mustache') }, | ||
route: (req, reply) => { reply.view('index.html', { text: 'text' }) } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict' | ||
|
||
require('./setup.js')({ | ||
engine: { nunjucks: require('nunjucks') }, | ||
route: (req, reply) => { reply.view('index.njk', { text: 'text' }) } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict' | ||
|
||
require('./setup.js')({ | ||
engine: { pug: require('pug') }, | ||
route: (req, reply) => { reply.view('index.pug', { text: 'text' }) } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict' | ||
|
||
require('./setup.js')({ | ||
engine: { twig: require('twig') }, | ||
route: (req, reply) => { reply.view('index.twig', { title: 'fastify', text: 'text' }) } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,6 @@ | ||
'use strict' | ||
|
||
process.env.NODE_ENV = 'production' | ||
|
||
const fastify = require('fastify')() | ||
|
||
fastify.register(require('../index'), { | ||
engine: { | ||
ejs: require('ejs') | ||
} | ||
}) | ||
|
||
fastify.get('/', (req, reply) => { | ||
reply.view('../templates/index.ejs', { text: 'text' }) | ||
}) | ||
|
||
fastify.listen({ port: 3000 }, err => { | ||
if (err) throw err | ||
console.log(`server listening on ${fastify.server.address().port}`) | ||
require('./setup.js')({ | ||
engine: { ejs: require('ejs') }, | ||
route: (req, reply) => { reply.view('index.ejs', { text: 'text' }) } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict' | ||
|
||
process.env.NODE_ENV = 'production' | ||
|
||
const fastify = require('fastify')() | ||
const path = require('node:path') | ||
|
||
module.exports = function ({ engine, route, options = {}, pluginOptions }) { | ||
fastify.register(require('../index'), { | ||
engine, | ||
options, | ||
root: path.join(__dirname, '../templates'), | ||
...pluginOptions | ||
}) | ||
|
||
fastify.get('/', route) | ||
|
||
fastify.listen({ port: 3000 }, err => { | ||
if (err) throw err | ||
console.log(`server listening on ${fastify.server.address().port}`) | ||
}) | ||
|
||
return fastify | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters