菜单
文档面包屑箭头 Grafana k6面包屑箭头 示例面包屑箭头 获取 HTTP 指标的时间信息
开源

获取 HTTP 指标的时间信息

要访问单个 HTTP 请求的时间信息,`Response.timings` 对象提供了各个阶段花费的时间(以毫秒为单位)。其中一个用例是将这些时间信息用于自定义指标,以便为特定端点创建趋势。

时间信息如下:

  • blocked: 等同于 `http_req_blocked`。
  • connecting: 等同于 `http_req_connecting`。
  • tls_handshaking: 等同于 `http_req_tls_handshaking`。
  • sending: 等同于 `http_req_sending`。
  • waiting: 等同于 `http_req_waiting`。
  • receiving: 等同于 `http_req_receiving`。
  • duration: 等同于 `http_req_duration`。

此脚本获取特定 GET 请求的请求持续时间时间信息并将其记录到控制台。

JavaScript
import http from 'k6/http';

export default function () {
  const res = http.get('https://quickpizza.grafana.com/');
  console.log('Response time was ' + String(res.timings.duration) + ' ms');
}

预期的(部分)输出如下所示:

bash
$ k6 run script.js

  INFO[0001] Response time was 337.962473 ms               source=console