-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathload-test.js
executable file
·179 lines (162 loc) · 5.92 KB
/
load-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
/*
* Copyright 2023 Mia s.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import http from 'k6/http'
import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.2/index.js'
import { check, sleep } from 'k6'
import {
randomIntBetween,
randomString,
} from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'
import { is200, is200or404, is204or404, CRUD_BASE_URL } from './utils.js'
//
// Test on collection "items"
// Type of test: load test
//
// We have a first stage where we insert documents for 1 minute
// When we're done, then we execute a round GET (list), GET by _q, PATCH by _q and DELETE by _q
// The following for 100 VUs for a minute
//
export const options = {
scenarios: {
'initialLoad': {
executor: 'constant-vus',
exec: 'initialLoad',
vus: 10,
duration: '1m',
tags: { test_type: 'initialLoad' },
},
'loadTest': {
executor: 'constant-vus',
exec: 'loadTest',
vus: 100,
startTime: '1m',
duration: '1m',
tags: { test_type: 'loadTest' },
},
},
thresholds: {
checks: ['rate==1'],
http_req_failed: ['rate<0.01'],
'http_req_duration{type:post}': ['p(90)<100'],
'http_req_duration{type:getList}': ['p(90)<500'],
'http_req_duration{type:getById}': ['p(90)<500'],
'http_req_duration{type:patchByQuery}': ['p(90)<500'],
'http_req_duration{type:patchById}': ['p(90)<500'],
'http_req_duration{type:deleteByQuery}': ['p(90)<500'],
'http_req_duration{type:deleteById}': ['p(90)<500'],
},
}
// #region helper fns
let counter = 0
const generateItem = () => {
const array = []
const len = randomIntBetween(0, 10)
for (let i = 0; i < len; i++) {
array.push({
string: `array-${i}-${randomString(5)}`,
number: randomIntBetween(1, 10),
boolean: randomIntBetween(0, 1) === 1,
})
}
counter += 1
return {
string: randomString(5),
number: randomIntBetween(1, 10),
boolean: randomIntBetween(0, 1) === 1,
date: new Date(randomIntBetween(0, 1705414020030)),
object: {
string: `object-${randomString(5)}`,
number: randomIntBetween(1, 10),
boolean: randomIntBetween(0, 1) === 1,
counter,
},
array,
}
}
// #endregion
export function initialLoad() {
const post = http.post(
`${CRUD_BASE_URL}/items`,
JSON.stringify(generateItem()),
{
headers: { 'Content-Type': 'application/json' },
tags: { type: 'post' },
}
)
check(post, { 'POST / returns status 200': is200or404 })
sleep(0.01)
}
export function loadTest() {
// GET / request
const getList = http.get(`${CRUD_BASE_URL}/items?number=${randomIntBetween(1, 10)}`, { tags: { type: 'getList' } })
check(getList, { 'GET / returns status 200': is200 })
sleep(1)
// Fetch for the seventh document from the getList request to get an id to use for a getById request
const getListResults = JSON.parse(getList.body)
const count = getListResults.length
if (count === 0) {
return
}
// GET /{id} request
const documentIdToFetch = getListResults[randomIntBetween(0, count - 1)]._id
const getById = http.get(`${CRUD_BASE_URL}/items/${documentIdToFetch}`, { tags: { type: 'getById' } })
const isGetByIdValid = check(getById, { 'GET /{id} returns status 200 or 404': is200or404 })
if (!isGetByIdValid) { console.log({ failed: 'getById', error: getById.error, status: getById.status, documentId: documentIdToFetch }) }
sleep(1)
// PATCH /{id} request
const documentIdToPatch = getListResults[randomIntBetween(0, count - 1)]._id
const patchById = http.patch(
`${CRUD_BASE_URL}/items/${documentIdToPatch}`,
JSON.stringify({ $set: generateItem() }),
{
headers: { 'Content-Type': 'application/json' },
tags: { type: 'patchById' },
}
)
const isPatchByIdValid = check(patchById, { 'PATCH /{id} returns status 200 or 404': is200or404 })
if (!isPatchByIdValid) { console.log({ failed: 'patchById', error: patchById.error, status: patchById.status, documentId: documentIdToFetch }) }
sleep(1)
// DELETE /{id} request
const documentIdToDelete = getListResults[randomIntBetween(0, count - 1)]._id
const deleteById = http.del(`${CRUD_BASE_URL}/items/${documentIdToDelete}`, null, { tags: { type: 'deleteById' } })
const isDeleteByIdValid = check(deleteById, { 'DELETE /{id} returns status 204 or 404': is204or404 })
if (!isDeleteByIdValid) { console.log({ failed: 'deleteById', error: deleteById.error, status: deleteById.status, documentId: documentIdToFetch }) }
sleep(1)
// PATCH /?_q=... request
const counterValueForPatch = getListResults[randomIntBetween(0, count - 1)].object.counter
const patchQuery = JSON.stringify({ 'object.counter': counterValueForPatch })
const patch = http.patch(
`${CRUD_BASE_URL}/items?_q=${patchQuery}`,
JSON.stringify({ $set: generateItem() }),
{
headers: { 'Content-Type': 'application/json' },
tags: { type: 'patchByQuery' },
}
)
check(patch, { 'PATCH / returns status 200': is200 })
sleep(1)
// DELETE /?_q=... request
const counterValueForDelete = getListResults[randomIntBetween(0, count - 1)].object.counter
const deleteQuery = JSON.stringify({ 'object.counter': counterValueForDelete })
const deleteReq = http.del(`${CRUD_BASE_URL}/items?_q=${deleteQuery}`, null, { tags: { type: 'deleteByQuery' } })
check(deleteReq, { 'DELETE / returns status 200': is200 })
sleep(1)
}
export function handleSummary(data) {
return {
stdout: textSummary(data, { enableColors: true }),
}
}