Skip to main content
Module

x/fastro/benchmarks/benchmark.js

Fast and simple web application framework for deno
Go to Latest
File
const path = require("path");const { execSync } = require("child_process");
const oak_version = "5.2.0";const express_version = "4.17.1";const fastro_version = "0.13.8";const abc_version = "1.0.0";const deno_version = "1.1.3";const node_version = "14.3.0";const fastify_version = "2.15.1";const php_version = "7.3.11";const flask_version = "1.1.2";
const cwd = process.cwd();
const start = () => { const express = `node express &`; const fastify = `node fastify &`; const node = `node node_http &`; const oak = `deno run --allow-net oak.ts &`; const abc = `deno run --allow-net abc.ts &`; const fastro = `deno run --allow-net fastro.ts &`; const deno_http = `deno run --allow-net deno_http.ts &`;
execSync(express, { stdio: [0, 1, 2], cwd }); execSync(fastify, { stdio: [0, 1, 2], cwd }); execSync(oak, { stdio: [0, 1, 2], cwd }); execSync(fastro, { stdio: [0, 1, 2], cwd }); execSync(abc, { stdio: [0, 1, 2], cwd }); execSync(deno_http, { stdio: [0, 1, 2], cwd }); execSync(node, { stdio: [0, 1, 2], cwd });};
const compare = async () => { const abc = `npx autocannon -c100 -j localhost:3004 > benchmark_abc.json`; const deno = `npx autocannon -c100 -j localhost:3005 > benchmark_deno.json`; const express = `npx autocannon -c100 -j localhost:3001 > benchmark_express.json`; const fastify = `npx autocannon -c100 -j localhost:3002 > benchmark_fastify.json`; const fastro = `npx autocannon -c100 -j localhost:3000 > benchmark_fastro.json`; const oak = `npx autocannon -c100 -j localhost:3003 > benchmark_oak.json`; const node = `npx autocannon -c100 -j localhost:3006 > benchmark_node.json`; const php = `npx autocannon -c100 -j localhost:80/index.php > benchmark_php.json`; const flask = `npx autocannon -c100 -j localhost:5000 > benchmark_flask.json`;
await execSync(php, { stdio: [0, 1, 2], cwd }); await execSync(flask, { stdio: [0, 1, 2], cwd }); await execSync(abc, { stdio: [0, 1, 2], cwd }); await execSync(deno, { stdio: [0, 1, 2], cwd }); await execSync(express, { stdio: [0, 1, 2], cwd }); await execSync(fastify, { stdio: [0, 1, 2], cwd }); await execSync(fastro, { stdio: [0, 1, 2], cwd }); await execSync(oak, { stdio: [0, 1, 2], cwd }); await execSync(node, { stdio: [0, 1, 2], cwd });};
const kill = () => { const kill_server = `lsof -ti tcp:3000 | xargs kill &lsof -ti tcp:3001 | xargs kill &lsof -ti tcp:3002 | xargs kill &lsof -ti tcp:3003 | xargs kill &lsof -ti tcp:3004 | xargs kill &lsof -ti tcp:3005 | xargs kill &lsof -ti tcp:3006 | xargs kill &lsof -ti tcp:5000 | xargs kill &`; execSync(kill_server, { stdio: [0, 1, 2], cwd });};
function escapeRegExp(str) { return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");}
function replaceAll(str, term, replacement) { return str.replace(new RegExp(escapeRegExp(term), "g"), replacement);}
function generateExampleTable() { const examples = [ { title: "Create hello world app", file: "hello.ts", version: fastro_version, }, { title: "Use the basics", file: "basic.ts", version: fastro_version }, { title: "Get url parameter", file: "basic.ts", version: fastro_version }, { title: "Get a payload", file: "basic.ts", version: fastro_version }, { title: "Send results as a file", file: "basic.ts", version: fastro_version, }, { title: "Serve static files", file: "static.ts", version: fastro_version }, { title: "Create a middleware", file: "middleware.ts", version: fastro_version, }, { title: "Create plugins & routers", file: "plugin.ts", version: fastro_version, }, ];
let text = "";
examples.forEach((example) => { const title = example.title; const version = example.version; const file = example.file;
const content = `|[${title}](https://github.com/fastrodev/fastro/blob/v${version}/examples/${file})|\`${file}\`|`;
text = text.concat(content).concat("\n"); });
return text;}
function generateFwTable() { const { requests: { average: abc } } = require("./benchmark_abc.json"); const { requests: { average: deno } } = require("./benchmark_deno.json"); const { requests: { average: fastify } } = require( "./benchmark_fastify.json", ); const { requests: { average: express } } = require( "./benchmark_express.json", ); const { requests: { average: fastro } } = require("./benchmark_fastro.json"); const { requests: { average: oak } } = require("./benchmark_oak.json"); const { requests: { average: node } } = require("./benchmark_node.json"); const { requests: { average: php } } = require("./benchmark_php.json"); const { requests: { average: flask } } = require("./benchmark_flask.json");
const abc_item = { value: abc, name: "Abc", json: "benchmark_abc.json", file: "abc.js", route: true, version: abc_version, }; const deno_item = { value: deno, name: "Deno http", json: "benchmark_deno.json", file: "deno_http.ts", route: false, version: deno_version, }; const express_item = { value: express, name: "Express", json: "benchmark_express.json", file: "express.js", route: true, version: express_version, }; const fastify_item = { value: fastify, name: "Fastify", json: "benchmark_fastify.json", file: "fastify.js", route: true, version: fastify_version, }; const fastro_item = { value: fastro, name: "Fastro", json: "benchmark_fastro.json", file: "fastro.ts", route: true, version: fastro_version, }; const node_item = { value: node, name: "Node http", json: "benchmark_node.json", file: "node_http.js", route: false, version: node_version, }; const oak_item = { value: oak, name: "Oak", json: "benchmark_oak.json", file: "oak.ts", route: true, version: oak_version, }; const php_item = { value: php, name: "PHP", json: "benchmark_php.json", file: "index.php", route: false, version: php_version, }; const flask_item = { value: flask, name: "Python Flask", json: "benchmark_flask.json", file: "flask_app.py", route: true, version: flask_version, };
const dataList = [ express_item, abc_item, flask_item, deno_item, fastify_item, fastro_item, node_item, oak_item, php_item, ]; let text = "";
const max = Math.max.apply(null, dataList.map((item) => item.value));
const p = dataList.map((i) => { const p = (i.value / max) * 100; return p.toLocaleString(); });
dataList.sort((a, b) => { return b.value - a.value; }).map((i) => { const p = (i.value / max) * 100; i.p = p.toFixed(1); return i; }).forEach((item) => { const name = item.name; const file = item.file; const json = item.json; const version = item.version; const route = item.route ? "✓" : "✗"; const value = item.value.toFixed(1); const percentage = item.p + "%";
fastro_version;
const content = `|[${name}](https://github.com/fastrodev/fastro/blob/v${fastro_version}/benchmarks/${file})|${version}|[${value}](https://github.com/fastrodev/fastro/blob/v${fastro_version}/benchmarks/${json})|${percentage}|\n`;
text = text.concat(content); });
return text;}
/** compile benchmark data and display them to readme */const compile = () => { const fs = require("fs"); const text = generateFwTable(); const example = generateExampleTable();
const data = fs.readFileSync( "./_template.md", { encoding: "utf8", flag: "r" }, );
const tableText = data .replace("${table}", text) .replace("${example_table}", example);
const final = replaceAll(tableText, "${fastro_version}", fastro_version); fs.writeFile("../readme.md", final, function (err) { if (err) throw err; console.log("Saved!"); });};
const [node, cmd, arg] = process.argv;
if (!arg) { console.log("benchmark");}
if (arg === "start") start();if (arg === "kill") kill();if (arg === "benchmark") compare();if (arg === "compile") compile();