-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
106 lines (82 loc) 路 2.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const Benny = require('benny');
const { execSync } = require('child_process');
const { writeFileSync } = require('fs');
const path = require('path');
// Outputs into the documentation folder
const output = path.resolve(__dirname, '../docs/src/generated/benchmark.md');
const Axios = require('axios').default;
const AxiosInstance = Axios.create();
const AxiosCacheInterceptor = require('../dist/index.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) => {
if (data[params.name] === undefined) {
data[params.name] = 1;
} else {
data[params.name]++;
}
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';
if (runs[name] === undefined) {
runs[name] = 1;
} else {
runs[name]++;
}
await AxiosInstance.get(`http://${config.host}:${config.port}/${name}`);
}),
Benny.add('cache-interceptor', async () => {
const name = 'cache-interceptor';
if (runs[name] === undefined) {
runs[name] = 1;
} else {
runs[name]++;
}
await InterceptorInstance.get(`http://${config.host}:${config.port}/${name}`);
}),
Benny.add('cache-adapter', async () => {
const name = 'cache-adapter';
if (runs[name] === undefined) {
runs[name] = 1;
} else {
runs[name]++;
}
await AdapterInstance.get(`http://${config.host}:${config.port}/${name}`);
}),
Benny.cycle(),
Benny.complete((summary) => {
server.close();
writeFileSync(
output,
`# 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 - options.percentSlower).toFixed(2)}%`
)
.join('\n')}
`
);
})
);