forked from elastic/kibana-capacity-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kibana-test.js
258 lines (226 loc) · 7.87 KB
/
kibana-test.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { TEST_FILES, BASE_PATH } from './test-data.js';
import axios from 'axios';
const INTERVAL_LENGTH_MINS = 0.5;
const REQUESTS_PER_BATCH = 10;
const LATENCY_MAX_FACTOR = 200;
const TEST_RPMS = [200, 500, 1000, 3000, 5000, 7500, 10000, 15000, 20000, 25000, 30000, 40000, 50000];
const TOP_LEVEL_TIMEOUT = 'top level timeout';
/**
* Use axios middleware to calculate response duration
*/
axios.interceptors.request.use(function (config) {
config.metadata = { startTime: new Date() }
return config;
}, function (error) {
return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
response.config.metadata.endTime = new Date()
response.duration = response.config.metadata.endTime - response.config.metadata.startTime
return response;
}, function (error) {
if (error.config && error.config.metadata) {
error.config.metadata.endTime = new Date();
error.duration = error.config.metadata.endTime - error.config.metadata.startTime;
}
return Promise.reject(error);
});
async function doLogin() {
console.log('logging in...')
const configObject = TEST_FILES['login'];
const reqObj = {
baseURL: BASE_PATH,
method: configObject.method,
url: configObject.url,
headers: configObject.headers || {},
data: JSON.stringify(configObject.params || {}),
};
const loginRes = await axios.request(reqObj);
const cookie = loginRes.headers['set-cookie'][0].split(';')[0];
console.log(`logged in!`)
return cookie;
}
function getRequestPromise(configObject, abortController) {
const headers = {
...configObject.headers || {},
};
const reqObj = {
rejectUnauthorized: false,
baseURL: BASE_PATH,
method: configObject.method,
url: configObject.url,
signal: abortController.signal,
headers,
...(configObject.method === 'GET' ? {
params: configObject.params || {},
} : {
data: JSON.stringify(configObject.params || {}),
})
};
// console.log(reqObj)
return axios.request(reqObj).then(r => {
// console.log(r)
const { status, duration } = r;
return {
status,
duration,
}
}).catch(e => {
// console.log(e)
return {
status: e.response?.status,
duration: e.duration,
}
});
}
/**
* Awaits a list of promises and formats them into an object with average execution times per staus code
* @param {*} responses
* @returns
*/
async function getStatusCodesFromResponses(timingsPromises) {
const responses = await Promise.all(timingsPromises);
let statusCodes = {}
responses.map(r => {
if (!statusCodes[r.status]) {
statusCodes[r.status] = {
count: 0,
totalTime: 0,
avg: 0,
}
}
statusCodes[r.status].count++;
statusCodes[r.status].totalTime += r.duration;
});
Object.keys(statusCodes).forEach(key => {
statusCodes[key].avg = statusCodes[key].totalTime / statusCodes[key].count
});
return statusCodes;
}
/**
*
* @param {*} rpm
* @param {*} configObject
* @throws A TOP_LEVEL_TIMEOUT error if responses didn't return within INTERVAL_LENGTH_MINS * LATENCY_MAX_FACTOR
* @returns An object with average response times per status code
*/
function runIntervalsForDuration(rpm, configObject) {
// target requests per second
const rps = rpm / 60;
// interval duration in seconds
const intervalDurationSec = REQUESTS_PER_BATCH / rps;
// Overall number of requests to run
const requestsToRun = INTERVAL_LENGTH_MINS * rpm;
// console.log(`batch ${REQUESTS_PER_BATCH}, rpm ${rpm}, interval duration ${intervalDurationSec}`);
return new Promise(async (resolve, reject) => {
let intervalCount = 0;
const timingsPromises = [];
const abortControllers = [];
let intervalHandle = undefined;
const intervalHandler = async () => {
process.stdout.write(`Interval ${intervalCount}: ${Math.floor(intervalCount * intervalDurationSec* 10) / 10}s, ${REQUESTS_PER_BATCH*intervalCount} requests\r`);
intervalCount++;
for (let i = 0; i < REQUESTS_PER_BATCH; i++) {
const abortController = new AbortController();
abortControllers.push(abortController);
timingsPromises.push(getRequestPromise(configObject, abortController));
}
if (timingsPromises.length >= requestsToRun) {
process.stdout.write(`\n`);
intervalHandle && clearInterval(intervalHandle);
// setup a timer to abort after 60 seconds
const abortTimeoutHandler = setTimeout(() => {
console.error('GLOBAL TIMEOUT')
abortControllers.map(abortController => abortController.abort());
// reject(new Error(TOP_LEVEL_TIMEOUT));
},
60 * 1000
);
const res = await getStatusCodesFromResponses(timingsPromises);
// clear the abort timer if all have returned
clearTimeout(abortTimeoutHandler);
resolve(res);
} else if (!intervalHandle) {
intervalHandle = setInterval(intervalHandler, intervalDurationSec * 1000);
}
};
intervalHandler();
})
}
/**
* Logs result table
* @param {*} benchmarks
*/
function showTable(benchmarks) {
const keys = Object.keys(benchmarks);
const tableData = [];
keys.forEach(rpm => {
const statusCodes = Object.keys(benchmarks[rpm]);
statusCodes.forEach(statusCode => {
const b = benchmarks[rpm][statusCode];
tableData.push({
rpm,
statusCode,
'total time': b.totalTime,
'total runs': b.count,
'avg runtime': b.avg,
});
})
})
console.table(tableData)
}
/**
* Run test
*/
async function initTest(testName = 'login') {
const testData = TEST_FILES[testName];
if (!testData) {
console.error(`Unknown test ${testName}`)
return;
}
console.log(`Test fetching ${testData.url}`)
const benchmarks = {};
let i = 0;
let rpm = TEST_RPMS[i++];
let latestBenchMark = undefined;
let errorBreak = false;
// login if needed
if (testData.auth) {
let authCookie = await doLogin();
testData.headers = testData.headers || {};
testData.headers.cookie = authCookie.trim();
}
// warm up
console.log('Doing warm up')
await runIntervalsForDuration(100, testData);
do {
try {
console.log(`Running test for ${rpm} rpm, interval duration is ${INTERVAL_LENGTH_MINS} mins`)
latestBenchMark = benchmarks[rpm] = await runIntervalsForDuration(rpm, testData);
console.log(`${rpm} rpm, ${latestBenchMark[200].avg} ms`)
rpm = TEST_RPMS[i++];
if (!rpm) {
break;
}
await new Promise(r => setTimeout(r, 2000));
} catch (e) {
if (e.message === TOP_LEVEL_TIMEOUT) {
errorBreak = true;
console.error(`Timeout at ${rpm}`)
break;
}
}
} while (
// Top level timeout
!errorBreak &&
// The initial batch failed
benchmarks[TEST_RPMS[0]][200] !== undefined &&
// The last benchmark had any successful responses
latestBenchMark[200] &&
// Latency increased by LATENCY_MAX_FACTOR
(latestBenchMark[200].avg / LATENCY_MAX_FACTOR < benchmarks[TEST_RPMS[0]][200].avg))
showTable(benchmarks);
}
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
const testName = process.argv[2];
initTest(testName);