tspace-spear
Version:
tspace-spear is a lightweight API framework for Node.js that is fast and highly focused on providing the best developer experience. It utilizes the native HTTP server
115 lines • 3.66 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const autocannon_1 = __importDefault(require("autocannon"));
const fastify_1 = __importDefault(require("fastify"));
const express_1 = __importDefault(require("express"));
const http_1 = __importDefault(require("http"));
const yargs_1 = __importDefault(require("yargs"));
const lib_1 = __importDefault(require("../lib"));
const MESSAGE = 'Hello world!';
const { argv } = (0, yargs_1.default)(process.argv.slice(2));
let duration = argv.d || argv.duration;
let connections = argv.c || argv.connections;
let pipelining = argv.p || argv.pipelining;
connections = connections == null ? 100 : Number(connections);
pipelining = pipelining == null ? 10 : Number(pipelining);
duration = duration == null ? 40 : Number(duration);
function runExpress() {
const port = 3000;
(0, express_1.default)()
.get('/', (req, res) => {
res.setHeader('Content-Type', 'text/plain');
return res.send(MESSAGE);
})
.listen(port, () => {
console.log(`Server 'Express' running at http://localhost:${port}`);
});
}
function runHttp() {
const port = 3001;
const server = http_1.default.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end(MESSAGE);
});
server.listen(port, () => {
console.log(`Server 'Http' running at http://localhost:${port}`);
});
}
function runFastify() {
const port = 3002;
(0, fastify_1.default)()
.get('/', (request, reply) => {
return reply
.header('Content-Type', 'text/plain')
.send(MESSAGE);
})
.listen({ port }, (err, address) => {
if (err)
throw err;
console.log(`server 'Fastify' running at : http://localhost:${port}`);
});
}
function runSpear() {
const port = 3999;
new lib_1.default()
.get('/', () => MESSAGE)
.listen(port, () => console.log(`server 'Spear' running at : http://localhost:${port}`));
}
const url = (port) => `http://localhost:${port}`;
const urls = [
{ name: 'express', url: url(3000) },
{ name: 'http', url: url(3001) },
{ name: 'fastify', url: url(3002) },
{ name: 'tspace-spear', url: url(3999) }
];
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms, null));
};
const runBenchmark = async () => {
const results = [];
for (const { name, url } of urls) {
const result = await new Promise((resolve, reject) => {
(0, autocannon_1.default)({
url,
connections,
duration,
pipelining
}, (err, result) => {
if (err)
return reject(err);
return resolve(result);
});
});
results.push({
name,
url,
requests: result.requests.total,
['req/sec']: result.requests.total / duration,
average: result.latency.average
});
}
console.log({
connections,
duration,
pipelining
});
console.table(results);
};
async function runApps() {
await Promise.all([
runSpear,
runFastify,
runExpress,
runHttp
].map(v => v()));
await sleep(3000);
console.log('benchmarking !!');
await runBenchmark();
console.log('benchmarking done !!');
}
runApps();
//# sourceMappingURL=benchmark.test.js.map