Is it possible to track https status codes for requests in k6 output?

Hello!

Please, clarify if it’s possible to show output statistics which is grouped by http status codes for each request (200 OK, 40* errors, 50* errors) and how ?

For example:

I have a load test for one specific method which is grouped by group function.
I need to see how many requests for the method were executed correctly (200 OK) and how many errors produced - what type (40*, 50* ) if they were occured.

 █ Events

  ✓ status is 200 OK
  ✓ content-type is application/json

GetEvents..................: avg=6640.0475 min=6624.679 med=6643.218 max=6647.889 p(90)=6646.6551 p(95)=6647.27205
checks.....................: 100.00% ✓ 20   ✗ 0   
data_received..............: 97 kB   1.6 kB/s
data_sent..................: 10 kB   173 B/s
errors.....................: 0.00%   ✓ 0    ✗ 10  
group_duration.............: avg=7.14s     min=7.13s    med=7.15s    max=7.15s    p(90)=7.15s     p(95)=7.15s     
http_req_blocked...........: avg=494.26ms  min=491.84ms med=494.62ms max=496.55ms p(90)=496.21ms  p(95)=496.38ms  
http_req_connecting........: avg=45.31ms   min=40.64ms  med=47.47ms  max=48ms     p(90)=47.95ms   p(95)=47.97ms   
http_req_duration..........: avg=6.64s     min=6.62s    med=6.64s    max=6.64s    p(90)=6.64s     p(95)=6.64s     
http_req_receiving.........: avg=10.95ms   min=1.62ms   med=5.85ms   max=23.74ms  p(90)=23.16ms   p(95)=23.45ms   
http_req_sending...........: avg=1.45ms    min=220µs    med=312µs    max=3.24ms   p(90)=3.22ms    p(95)=3.23ms    
http_req_tls_handshaking...: avg=356.47ms  min=350.72ms med=356.17ms max=363.35ms p(90)=363.32ms  p(95)=363.33ms  
http_req_waiting...........: avg=6.62s     min=6.61s    med=6.62s    max=6.64s    p(90)=6.64s     p(95)=6.64s     
http_reqs..................: 10      0.166391/s
iteration_duration.........: avg=7.14s     min=7.13s    med=7.15s    max=7.15s    p(90)=7.15s     p(95)=7.15s     
iterations.................: 10      0.166391/s
vus........................: 10      min=10 max=10
vus_max....................: 10      min=10 max=10
1 Like

It is somewhat possible, but it requires a bit of a hack until Add explicit tracking and ignoring of metrics and sub-metrics · Issue #1321 · grafana/k6 · GitHub is implemented. There are some examples linked in this docs issue: Add example for sub-metrics by scenario (or other tags) in summary · Issue #205 · grafana/k6-docs · GitHub

Here’s how this can look like in your case:

import http from 'k6/http'

export let options = {
    vus: 5,
    iterations: 10,
    thresholds: {
        // Some dummy thresholds that are always going to pass.
        'http_req_duration{status:200}': ['max>=0'],
        'http_req_duration{status:403}': ['max>=0'],
        'http_req_duration{method:POST}': ['max>=0'],
    },
    'summaryTrendStats': ['min', 'med', 'avg', 'p(90)', 'p(95)', 'max', 'count'],
}

export default function () {
    http.get('https://httpbin.test.k6.io/status/200');
    http.get('https://httpbin.test.k6.io/anything');
    http.get('https://httpbin.test.k6.io/status/403');
    http.post('https://httpbin.test.k6.io/anything', 'some body');
}

will result in something like this:

...
     ✓ { method:POST }..............: min=123.86ms med=127.57ms avg=128.64ms p(90)=134.27ms p(95)=134.38ms max=134.5ms  count=10
     ✓ { status:200 }...............: min=121.81ms med=127.8ms  avg=128.43ms p(90)=133.77ms p(95)=134.35ms max=134.5ms  count=30
     ✓ { status:403 }...............: min=122.05ms med=128.74ms avg=129.24ms p(90)=134.14ms p(95)=135.2ms  max=136.26ms count=10
...

Notice that you currently can’t specify status ranges, only single status values in the sub-metrics.

2 Likes

Faced the same problem recently. If you only need count / distribution of status codes, then I worked it around with this

  check(res, {
    'status was 200': (r) => r.status == 200,
    'status was 422': (r) => r.status == 422,
    'status was 423': (r) => r.status == 422,
    'status was 500': (r) => r.status == 500,
    'no response': (r) => !r.status,
  })

It gives you an output like

     ✗ status was 200
      ↳  68% — ✓ 5103 / ✗ 2330
     ✗ status was 422
      ↳  0% — ✓ 0 / ✗ 7433
     ✗ status was 423
      ↳  0% — ✓ 0 / ✗ 7433
     ✗ status was 500
      ↳  0% — ✓ 0 / ✗ 7433
     ✗ no response
      ↳  31% — ✓ 2330 / ✗ 5103

As it’s just a JS you can also match by wildcard, i.e. 4xx / 5xx

2 Likes