docs: updated benchmark

This commit is contained in:
arthurfiorette 2022-02-01 13:36:06 -03:00
parent f235fd4c60
commit 27b9a4972c
No known key found for this signature in database
GPG Key ID: 9D190CD53C53C555
6 changed files with 1853 additions and 2908 deletions

83
docs/benchmark.js Normal file
View File

@ -0,0 +1,83 @@
//@ts-check
const Benny = require('benny');
const { execSync } = require('child_process');
const { writeFileSync } = require('fs');
const Axios = require('axios').default;
const AxiosInstance = Axios.create();
const AxiosCacheInterceptor = require('../cjs');
const InterceptorInstance = AxiosCacheInterceptor.setupCache(Axios.create());
const AxiosCacheAdapter = require('axios-cache-adapter');
const AdapterInstance = AxiosCacheAdapter.setup({});
const config = {
port: 8734,
host: '0.0.0.0'
};
const data = {};
const runs = {};
const app = require('express')();
app.get('/:name', ({ params }, res) => {
data[params.name] ? data[params.name]++ : (data[params.name] = 1);
return res.json({
computation: Math.random(),
name: params.name
});
});
const server = app.listen(config.port, config.host);
Benny.suite(
'Benchmark Result',
Benny.add('axios', async () => {
const name = 'axios';
runs[name] ? runs[name]++ : (runs[name] = 1);
await AxiosInstance.get(`http://${config.host}:${config.port}/${name}`);
}),
Benny.add('cache-interceptor', async () => {
const name = 'cache-interceptor';
runs[name] ? runs[name]++ : (runs[name] = 1);
await InterceptorInstance.get(`http://${config.host}:${config.port}/${name}`);
}),
Benny.add('cache-adapter', async () => {
const name = 'cache-adapter';
runs[name] ? runs[name]++ : (runs[name] = 1);
await AdapterInstance.get(`http://${config.host}:${config.port}/${name}`);
}),
Benny.cycle(),
Benny.complete((summary) => {
server.close();
writeFileSync(
'pages/_comparison-benchmark.md',
`# Result
Run at ${new Date().toUTCString()}
Commit: ${execSync('git rev-parse HEAD').toString()}
${summary.results
.sort((a, b) => a.percentSlower - b.percentSlower)
.map(
(options) => `
${options.name.split('-').join(' ').toUpperCase()}
Operations: ${options.ops}/s
Network requests: ${data[options.name]} of ${runs[options.name]}
Performance: ${100 - Math.round(options.percentSlower)}%`
)
.join('\n')}
`
);
})
);

View File

@ -1,78 +0,0 @@
const Axios = require('axios').default;
const express = require('express');
const { execSync } = require('child_process');
const interceptor = require('../../cjs');
const adapter = require('axios-cache-adapter');
const TIMES = 1_000_000;
const port = 8734;
const host = '0.0.0.0';
async function bench(axios) {
const init = Date.now();
let times = TIMES;
while (times--) {
await axios.get(`http://${host}:${port}/`);
}
return Date.now() - init;
}
function printResult(name, milliseconds, networkRequests, axiosTime) {
const seconds = milliseconds / 1000;
const axiosSeconds = axiosTime ? axiosTime / 1000 : seconds;
console.log();
console.log(`# ${name}`);
console.log(`Time: ${seconds}s`);
console.log(`Requests per second: ${(TIMES / seconds).toFixed(3)}/s`);
console.log(`Network requests: ${networkRequests} of ${TIMES}`);
console.log(
`Increase from pure axios: ${((100 * axiosSeconds) / seconds).toFixed(3)}%`
);
console.log();
}
(async () => {
const counter = { name: 'none' };
const app = express();
app.get('/', (_, res) => {
counter[counter.name] = counter[counter.name] + 1;
return res.json({ rnd: Math.random(), text: 'Hello World' });
});
const server = app.listen(port, host);
console.log(`Simulating ${TIMES} requests...`);
console.log(`Run at ${new Date().toUTCString()}`);
console.log(`Commit: ${execSync('git rev-parse HEAD').toString()}`);
counter.name = 'axios';
counter.axios = 0;
const withAxios = Axios.create();
const axiosTime = await bench(withAxios);
printResult('Raw axios', axiosTime, counter.axios);
counter.name = 'interceptor';
counter.interceptor = 0;
const withInterceptor = interceptor.setupCache(Axios.create(), { ttl: 1000 });
const interceptorTime = await bench(withInterceptor);
printResult('Axios Cache Interceptor', interceptorTime, counter.interceptor, axiosTime);
counter.name = 'adapter';
counter.adapter = 0;
const withAdapter = adapter.setup({
cache: { maxAge: 1000 }
});
const adapterTime = await bench(withAdapter);
printResult('Axios Cache Adapter', adapterTime, counter.adapter, axiosTime);
server.close();
})().catch(console.error);

View File

@ -2,12 +2,13 @@
"private": true,
"scripts": {
"serve": "docsify serve",
"bench": "node js/benchmark.js > pages/_comparison-benchmark.log"
"bench": "node benchmark.js"
},
"devDependencies": {
"axios": "^0.25.0",
"axios-cache-adapter": "^2.7.3",
"docsify-cli": "^4.4.3",
"express": "^4.17.2"
"express": "^4.17.2",
"benny": "^3.7.1"
}
}

View File

@ -1,25 +0,0 @@
Simulating 1000000 requests...
Run at Mon, 17 Jan 2022 19:08:25 GMT
Commit: 99226504f4dc623c76b7543a6cdb9794cfabcb67
# Raw axios
Time: 435.211s
Requests per second: 2297.736/s
Network requests: 1000000 of 1000000
Increase from pure axios: 100.000%
# Axios Cache Interceptor
Time: 21.653s
Requests per second: 46182.977/s
Network requests: 22 of 1000000
Increase from pure axios: 2009.934%
# Axios Cache Adapter
Time: 28.533s
Requests per second: 35047.138/s
Network requests: 29 of 1000000
Increase from pure axios: 1525.290%

View File

@ -18,8 +18,8 @@
## Benchmark
There's an simple
[benchmark](https://github.com/arthurfiorette/axios-cache-interceptor/blob/main/docs/js/benchmark.js)
[benchmark](https://github.com/arthurfiorette/axios-cache-interceptor/blob/main/docs/benchmark.js)
in form of a stress test to compare the performance of this library, `axios-cache-adapter`
and raw axios (without cache).
[Comparison benchmark](_comparison-benchmark.log ':include :type=code')
[Results](_comparison-benchmark.md ':include :type=code')

File diff suppressed because it is too large Load Diff