feat(bench): green-tea/express/fastify/raw-http servers with parity controls
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
4 files changed
+101
No files matched your search
@@ -0,0 +1,19 @@
|
||||
import express from 'express';
|
||||
|
||||
const app = express();
|
||||
app.disable('x-powered-by');
|
||||
app.set('etag', false);
|
||||
app.use(express.json());
|
||||
app.get('/hello', (_req, res) => { res.json({ hello: 'world' }); });
|
||||
app.get('/users/:id', (req, res) => { res.json({ id: req.params.id }); });
|
||||
const mw = (n: number) => (_req: any, res: any, next: any) => { res.locals[`s${n}`] = n; next(); };
|
||||
app.get('/pipeline', mw(1), mw(2), mw(3), (_req, res) => { res.json({ hello: 'world', s3: res.locals.s3 }); });
|
||||
app.post('/validate', (req: any, res) => {
|
||||
const b = req.body;
|
||||
if (!(b && typeof b.email === 'string' && b.email.includes('@'))) { res.status(422).json({ error: 'invalid' }); return; }
|
||||
res.json({ email: b.email });
|
||||
});
|
||||
const server = app.listen(0, () => {
|
||||
server.keepAliveTimeout = 5000;
|
||||
console.log(`READY ${(server.address() as any).port}`);
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
import Fastify from 'fastify';
|
||||
|
||||
const app = Fastify({ logger: false });
|
||||
app.get('/hello', async () => ({ hello: 'world' }));
|
||||
app.get('/users/:id', async (req: any) => ({ id: req.params.id }));
|
||||
const hook = (n: number) => (req: any, _res: any, done: any) => { req[`s${n}`] = n; done(); };
|
||||
app.get('/pipeline', { preHandler: [hook(1), hook(2), hook(3)] }, async (req: any) => ({ hello: 'world', s3: req.s3 }));
|
||||
app.post('/validate', {
|
||||
schema: { body: { type: 'object', required: ['email'], properties: { email: { type: 'string' } } } },
|
||||
}, async (req: any, reply) => {
|
||||
const b = req.body;
|
||||
if (!b.email.includes('@')) { reply.code(422); return { error: 'invalid' }; }
|
||||
return { email: b.email };
|
||||
});
|
||||
app.listen({ port: 0 }, (err, addr) => {
|
||||
if (err) { console.error(err); process.exit(1); }
|
||||
(app.server as any).keepAliveTimeout = 5000;
|
||||
console.log(`READY ${Number(new URL(addr).port)}`);
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import 'reflect-metadata';
|
||||
import { createApp, Step, Route, Get, Post, Module, needs, param, body } from '../../src/index';
|
||||
|
||||
// email schema (Standard Schema, hand-rolled — same field check the others do)
|
||||
const emailSchema = {
|
||||
'~standard': { version: 1 as const, vendor: 'bench', validate: (v: any) =>
|
||||
(v && typeof v.email === 'string' && v.email.includes('@')) ? { value: v } : { issues: [{ message: 'invalid email' }] } },
|
||||
};
|
||||
|
||||
// 5 chained steps: s1..s5, each needs the previous. Routes @needs the Nth to pull exactly N.
|
||||
function makeStep(n: number) {
|
||||
@Step({ provides: `s${n}`, needs: n > 1 ? [`s${n - 1}`] : [] })
|
||||
class S { run() { return { [`s${n}`]: n }; } }
|
||||
return S;
|
||||
}
|
||||
const steps = [1, 2, 3, 4, 5].map(makeStep);
|
||||
|
||||
@Route('/')
|
||||
class Bench {
|
||||
@Get('/hello') hello() { return { hello: 'world' }; }
|
||||
@Get('/users/:id') user(@param('id') id: string) { return { id }; }
|
||||
@Get('/pipeline') pipeline(@needs('s3') s3: number) { return { hello: 'world', s3 }; }
|
||||
@Post('/validate') validate(@body(emailSchema) b: any) { return { email: b.email }; }
|
||||
@Get('/steps/0') steps0() { return { hello: 'world' }; }
|
||||
@Get('/steps/3') steps3(@needs('s3') s3: number) { return { hello: 'world', s3 }; }
|
||||
@Get('/steps/5') steps5(@needs('s5') s5: number) { return { hello: 'world', s5 }; }
|
||||
}
|
||||
|
||||
@Module({ mountpoint: '/', steps, controllers: [Bench] })
|
||||
class BenchModule {}
|
||||
|
||||
// PARITY: security OFF for the cross-framework table. GT_SECURE=1 flips it on for the secure-cost micro-bench.
|
||||
const secure = process.env.GT_SECURE === '1';
|
||||
const app = createApp({ modules: [BenchModule], security: secure });
|
||||
|
||||
async function main() {
|
||||
// fail loudly if decorators didn't apply (inspect throws "no route" on empty plan, so wrap)
|
||||
try { if (app.inspect('/hello').length === 0) throw new Error('empty'); }
|
||||
catch { console.error('DECORATORS_NOT_APPLIED'); process.exit(1); }
|
||||
const server = await app.listen(0);
|
||||
server.keepAliveTimeout = 5000; // parity value (all servers use 5000)
|
||||
console.log(`READY ${(server.address() as any).port}`);
|
||||
}
|
||||
main().catch((e) => { console.error(e); process.exit(1); });
|
||||
@@ -0,0 +1,19 @@
|
||||
import http from 'http';
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
const url = (req.url ?? '/').split('?')[0];
|
||||
const send = (obj: unknown, code = 200) => { res.writeHead(code, { 'content-type': 'application/json' }); res.end(JSON.stringify(obj)); };
|
||||
if (req.method === 'GET' && url === '/hello') return send({ hello: 'world' });
|
||||
if (req.method === 'GET' && url.startsWith('/users/')) return send({ id: url.slice('/users/'.length) });
|
||||
if (req.method === 'GET' && url === '/pipeline') { const s: any = {}; [1, 2, 3].forEach((n) => (s[`s${n}`] = n)); return send({ hello: 'world', s3: s.s3 }); }
|
||||
if (req.method === 'GET' && url === '/steps/0') return send({ hello: 'world' });
|
||||
if (req.method === 'POST' && url === '/validate') {
|
||||
let raw = ''; req.on('data', (c) => (raw += c)); req.on('end', () => {
|
||||
try { const b = JSON.parse(raw); if (typeof b.email === 'string' && b.email.includes('@')) return send({ email: b.email }); return send({ error: 'invalid' }, 422); }
|
||||
catch { return send({ error: 'bad json' }, 400); }
|
||||
}); return;
|
||||
}
|
||||
send({ error: 'not found' }, 404);
|
||||
});
|
||||
server.keepAliveTimeout = 5000;
|
||||
server.listen(0, () => console.log(`READY ${(server.address() as any).port}`));
|
||||
Reference in new issue
Block a user