forked from grafana/xk6-loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite-scenario.js
99 lines (88 loc) · 2.07 KB
/
write-scenario.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
import { check, fail } from 'k6';
import loki from 'k6/x/loki';
/*
* Host name with port
* @constant {string}
*/
const HOST = "xxx.grafana.net:3100";
/**
* Name of the Loki tenant
* @constant {string}
*/
const TENANT_ID = __ENV.LOKI_TENANT_ID || fail("provide LOKI_TENANT_ID when starting k6");
/**
* Access token of the Loki tenant with logs:write and logs:read permissions
* @constant {string}
*/
const ACCESS_TOKEN = __ENV.LOKI_ACCESS_TOKEN || fail("provide LOKI_ACCESS_TOKEN when starting k6");
/**
* URL used for push and query requests
* Path is automatically appended by the client
* @constant {string}
*/
const BASE_URL = `https://${TENANT_ID}:${ACCESS_TOKEN}@${HOST}`;
/**
* Minimum amount of virtual users (VUs)
* @constant {number}
*/
const MIN_VUS = 10
/**
* Maximum amount of virtual users (VUs)
* @constant {number}
*/
const MAX_VUS = 100;
/**
* Constants for byte values
* @constant {number}
*/
const KB = 1024;
const MB = KB * KB;
/**
* Definition of test scenario
*/
export const options = {
thresholds: {
'http_req_failed': [{ threshold: 'rate<=0.01', abortOnFail: true }],
},
scenarios: {
write: {
executor: 'ramping-vus',
exec: 'write',
startVUs: MIN_VUS,
stages: [
{ duration: '5m', target: MAX_VUS },
{ duration: '30m', target: MAX_VUS },
],
gracefulRampDown: '1m',
},
},
};
const labelCardinality = {
"app": 5,
"namespace": 1,
"pod": 10,
};
const conf = new loki.Config(BASE_URL, 10000, 0.9, labelCardinality);
const client = new loki.Client(conf);
/**
* Entrypoint for write scenario
*/
export function write() {
let streams = randomInt(4, 8);
let res = client.pushParameterized(streams, 800 * KB, 1 * MB);
check(res,
{
'successful write': (res) => {
let success = res.status === 204;
if (!success) console.log(res.status, res.body);
return success;
},
}
);
}
/**
* Return a random integer between min and max including min and max
*/
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}