You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/data/markdown/docs/05 Examples/02 Tutorials/01 Get started with k6/100 Test-for-functional-behavior.md
+19-3Lines changed: 19 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,13 +66,23 @@ After the test finishes, k6 reports the [default result summary](/results-output
66
66
...
67
67
```
68
68
69
-
To make sure you're getting the right response, you could log the response body to the console as follows:
69
+
As an optional step, you can log the response body to the console to make sure you're getting the right response.
http_req_failed: ['rate<0.01'], // http errors should be less than 1%
56
57
http_req_duration: ["p(99)<1000"], // 99% of requests should be below 1s
@@ -75,21 +76,20 @@ export default function () {
75
76
76
77
// check that response is 200
77
78
check(res, {
78
-
"login response was 200": (res) =>res.status==200,
79
+
"response code was 200": (res) =>res.status==200,
79
80
});
80
81
}
81
82
82
83
```
83
84
84
85
</CodeGroup>
85
86
86
-
Run the test as usual:
87
+
Run the test.
87
88
88
89
```bash
89
90
k6 run api-test.js
90
91
```
91
92
92
-
93
93
Inspect the console output to determine whether performance crossed a threshold.
94
94
95
95
```
@@ -98,7 +98,7 @@ Inspect the console output to determine whether performance crossed a threshold.
98
98
✗ http_req_failed................: 0.00% ✓ 0 ✗ 1
99
99
```
100
100
101
-
101
+
The ✓ and ✗ symbols indicate whether the performance thresholds passed or failed.
102
102
103
103
## Test performance under increasing load
104
104
@@ -110,7 +110,7 @@ Scenarios schedule load according to the number of VUs, number of iterations, VU
110
110
111
111
### Run a smoke test
112
112
113
-
Start small. Run a [smoke test](/test-types/smoke-testing"a small test to confirm the script works properly") to see your script can handle minimal load.
113
+
Start small. Run a [smoke test](/test-types/smoke-testing"a small test to confirm the script works properly") to check that your script can handle a minimal load.
114
114
115
115
To do so, use the [`--iterations`](/using-k6/k6-options/reference/#iterations) flag with an argument of 10 or fewer.
116
116
@@ -123,19 +123,22 @@ Good thing you ran the test early!
123
123
124
124
### Run a test against an average load
125
125
126
-
Now increase the load.
127
-
128
126
Generally, traffic doesn't arrive all at once.
129
127
Rather, it gradually increases to a peak load.
130
128
To simulate this, testers increase the load in _stages_.
131
129
132
-
Since this is a learning environment, the stages are still quite short.
133
-
Add the following _scenario_ to your options `object` and rerun the test.
134
-
Where the smoke test defined the load in terms of iterations, this configuration uses the [`ramping-vus` executor](/using-k6/scenarios/executors/ramping-vus/) to express load through virtual users and duration.
130
+
Add the following `scenario` property to your `options` object and rerun the test.
135
131
136
-
```json
132
+
```javascript
133
+
exportconstoptions= {
134
+
// define thresholds
135
+
thresholds: {
136
+
http_req_failed: ['rate<0.01'], // http errors should be less than 1%
137
+
http_req_duration: ['p(99)<1000'], // 99% of requests should be below 1s
138
+
},
139
+
// define scenarios
137
140
scenarios: {
138
-
//arbitrary name of scenario
141
+
//arbitrary name of scenario
139
142
average_load: {
140
143
executor:"ramping-vus",
141
144
stages: [
@@ -148,8 +151,12 @@ Where the smoke test defined the load in terms of iterations, this configuration
148
151
],
149
152
},
150
153
}
154
+
};
151
155
```
152
156
157
+
Since this is a learning environment, the stages are still quite short.
158
+
Where the smoke test defined the load in terms of iterations, this configuration uses the [`ramping-vus` executor](/using-k6/scenarios/executors/ramping-vus/) to express load through virtual users and duration.
159
+
153
160
Run the test with no command-line flags:
154
161
155
162
```bash
@@ -159,7 +166,6 @@ k6 run api-test.js
159
166
The load is small, so the server should perform within thresholds.
160
167
However, this test server may be under load by many k6 learners, so the results are unpredictable.
http_req_duration: ["p(99)<1000"], // Latency threshold for percentile
247
-
},
248
253
};
249
254
250
255
exportdefaultfunction () {
@@ -265,21 +270,26 @@ export default function () {
265
270
266
271
// check that response is 200
267
272
check(res, {
268
-
"login response was 200": (res) =>res.status==200,
273
+
"response code was 200": (res) =>res.status==200,
269
274
});
270
275
}
271
276
272
277
```
273
278
274
279
</CodeGroup>
275
280
281
+
Run the test.
282
+
283
+
```bash
284
+
k6 run api-test.js
285
+
```
286
+
276
287
Did the threshold fail? If not, add another stage with a higher target and try again. Repeat until the threshold aborts the test:
277
288
278
289
```bash
279
290
ERRO[0010] thresholds on metrics 'http_req_duration, http_req_failed' were breached; at least one has abortOnFail enabled, stopping test prematurely
280
291
```
281
292
282
-
283
293
## Next steps
284
294
285
295
In this tutorial, you used [thresholds](/using-k6/thresholds/) to assert performance and [Scenarios](/using-k6/scenarios) to schedule different load patterns. To learn more about the usual load patterns and their goals, read [Load Test Types](/test-types/load-test-types/)
0 commit comments