-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_random.js
50 lines (43 loc) · 1.24 KB
/
custom_random.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
import http from 'k6/http';
import { sleep, check } from 'k6';
import { Counter, Rate } from 'k6/metrics';
export const options = {
// A number specifying the number of VUs to run concurrently.
vus: 20,
// A string specifying the total duration of the test run.
duration: '15m',
};
// Custom Metrics
const chacheHit = new Counter('cache_hit');
const cacheMiss = new Counter('cache_miss');
const cacheUnknown = new Counter('cache_unknown');
const cacheRate = new Rate('cache_rate');
// The function that defines VU logic.
//
// See https://grafana.com/docs/k6/latest/examples/get-started-with-k6/ to learn more
// about authoring k6 scripts.
//
export default function() {
const res = http.get('https://example.com');
switch(res.headers['Cache-Status']) {
case 'hit':
chacheHit.add(1);
cacheRate.add(true);
break;
case 'miss':
cacheMiss.add(1);
cacheRate.add(false);
break;
default:
cacheUnknown.add(1);
cacheRate.add(false);
}
console.log(res.headers['Cache-Status'] + " in " + res.headers['Cache_id']);
sleep(randomNumber(120,300));
check(res, {
'is status 200': (r) => r.status === 200,
});
}
function randomNumber(min, max) {
return Math.random() * (max - min) + min;
}