Checks and http metrics

Hi all
I have a website here that has extremely long waiting times in some cases. Now I would like to know how many requests take longer than 2 seconds. As you can see below, my current script doesn’t work. The average duration is under 500ms. And some requests take up to 16 seconds.

 ✓ Status is 200
 ✗ Request duration is below 2s
 ↳  0% — ✓ 0 / ✗ 51

 checks.........................: 50.00% ✓ 51  ✗ 51 
 data_received..................: 2.1 MB 30 kB/s
 data_sent......................: 12 kB  172 B/s
 group_duration.................: avg=397.39ms min=45.02ms med=58.36ms max=16.15s   p(90)=75.16ms  p(95)=146.28ms
 http_req_blocked...............: avg=1.93ms   min=188ns   med=532ns   max=98.66ms  p(90)=1.07µs   p(95)=1.14µs  
 http_req_connecting............: avg=317.15µs min=0s      med=0s      max=16.17ms  p(90)=0s       p(95)=0s      
 http_req_duration..............: avg=395.13ms min=44.69ms med=58.02ms max=16.15s   p(90)=74.88ms  p(95)=145.96ms
   { expected_response:true }...: avg=395.13ms min=44.69ms med=58.02ms max=16.15s   p(90)=74.88ms  p(95)=145.96ms
 http_req_failed................: 0.00%  ✓ 0   ✗ 51 
 http_req_receiving.............: avg=16.95ms  min=1.98ms  med=14.89ms max=89.45ms  p(90)=28.72ms  p(95)=29.52ms 
 http_req_sending...............: avg=98.76µs  min=38.06µs med=99.33µs max=147.64µs p(90)=114.39µs p(95)=116.33µs
 http_req_tls_handshaking.......: avg=1.31ms   min=0s      med=0s      max=67.17ms  p(90)=0s       p(95)=0s      
 http_req_waiting...............: avg=378.07ms min=28.23ms med=43.45ms max=16.13s   p(90)=58.63ms  p(95)=93.04ms 
 http_reqs......................: 51     0.721248/s
 iteration_duration.............: avg=1.4s     min=1.04s   med=1.05s   max=17.15s   p(90)=1.07s    p(95)=1.14s   
 iterations.....................: 50     0.707106/s
 vus............................: 1      min=1 max=1
 vus_max........................: 1      min=1 max=1

In my current script I use the check function like this:

check(response, {
      'Status is 200': (r) => r.status === 200,
      'Request duration is below 2s': (r) => r.http_req_duration < '2s'
      })

I don’t want to use thresholds because there you can’t see how many requests took longer.
Has anybody an idea on how to do this?

http_req_duration is the name of the metric, but in the Response object, the value is saved under r.timings.duration, see https://k6.io/docs/javascript-api/k6-http/response/

So, your check has to be something like this:

check(response, {
      'Status is 200': (r) => r.status === 200,
      'Request duration is below 2s': (r) => r.timings.duration < 2000,
})

Hi @ned
Thanks for your answer! :smiley:
I needed to change the value also from ‘2s’ to ‘2000’ to make it work.
One follow up question:
Is it possible to get the duration from the requests that took longer than 2 seconds?

You can add a custom Trend metric:

And if you want the details for every single request, you’d probably have to pipe the metrics to an external output or a JSON/CSV file: https://k6.io/docs/getting-started/results-output/#external-outputs

2 Likes