Press "Enter" to skip to content

使用K6对接口进行压测

使用K6对接口进行压测

背景

在实际开发中,常常需要对接口进行性能测试,因此,少不了压测工具

  • 在网上找了几款压测工具,感觉k6安装和使用都比较方便

step1:安装

# 在windows命令行工具输入
winget install k6
# 安装完成后查看版本
k6 --version
k6 v0.45.0 (2023-06-19T08:41:01+0000/v0.45.0-0-gc3b4587b, go1.20.5, windows/amd64)

Step2:使用

# 在项目根目录下创建文件夹pressureTest自定义
# 在该文件夹下创建js文件文件内容为压测的内容
# k6 run xxx.js

Step3:示例

dataReceive.js

import http from 'k6/http';
import { check, sleep, group } from 'k6';

export let options = {
  vus: 500, // 虚拟用户数
  duration: '30s', // 压测持续时间
};

export default function () {
  group('Example API', function () {
    // 定义请求的payload数据
    let payload = JSON.stringify({
      sn: '3500000200000001',
      timestamp: 1719904084,
      data: [
        {
          name_en: 'temp_p0',
          name_zh: '上层温度',
          unit: C',
          value: 20.1,
          status: {
            name_en: 'p0_temp_status',
            name_zh: '上层温度传感器状态',
            value: 0,
            status_en: 'normal',
            status_zh: '正常',
          },
        },
        {
          name_en: 'temp_p1',
          name_zh: '中层温度',
          unit: C',
          value: 20.2,
          status: {
            name_en: 'p1_temp_status',
            name_zh: '中层温度传感器状态',
            value: 0,
            status_en: 'normal',
            status_zh: '正常',
          },
        },
        {
          name_en: 'temp_p2',
          name_zh: '下层温度',
          unit: C',
          value: 20.3,
          status: {
            name_en: 'p2_temp_status',
            name_zh: '下次温度传感器状态',
            value: 0,
            status_en: 'normal',
            status_zh: '正常',
          },
        },
        {
          name_en: 'temp_in',
          name_zh: '空气温度',
          unit: C',
          value: 20.6,
          status: {
            name_en: 'p0_temp_status',
            name_zh: '空气温度传感器状态',
            value: 0,
            status_en: 'normal',
            status_zh: '正常',
          },
        },
      ],
    });

    // 发送POST请求
    let res = http.post('https://brewing-sass.nongbotech.com/api/device/data/receive', payload, {
      headers: {
        'Content-Type': 'application/json',
      },
    });

    // 检查响应状态码
    check(res, {
      'status is 200': (r) => r.status === 200,
    });

    // 添加自定义指标
    let responseTime = res.timings.duration;
    let apiName = 'Example API';
    let tags = { endpoint: '/device/data/receive' };
    let data = {
      responseTime: responseTime,
    };
    let sampleRate = 0.5; // 采样率,用于控制报告中指标的采样数量
    let customMetrics = {
      [apiName]: {
        tags,
        data,
        sampleRate,
      },
    };
    // 将自定义指标发送到k6的输出
    // 根据需要可以将指标发送到InfluxDB等其他目标
    let customMetricPayload = JSON.stringify(customMetrics);
    console.log(customMetricPayload);

    sleep(1); // 每个虚拟用户执行完后等待1秒钟
  });
}

Step4: 查看报告

进入dataReceive.js目录,执行 k6 run dataReceive.js

 Example API

        status is 200
          0%   0 /  8513

     checks.....................: 0.00%    0           8513
     data_received..............: 4.4 MB  96 kB/s
     data_sent..................: 9.2 MB  203 kB/s
     group_duration.............: avg=1.96s    min=1.01s med=1.03s   max=24.81s p(90)=4.56s    p(95)=6.01s
     http_req_blocked...........: avg=194.11ms min=0s    med=0s      max=19.29s p(90)=0s       p(95)=450.07ms
     http_req_connecting........: avg=4.33ms   min=0s    med=0s      max=5.46s  p(90)=0s       p(95)=47.26ms
     http_req_duration..........: avg=676.84ms min=0s    med=16.98ms max=23.81s p(90)=2.39s    p(95)=4.33s
     http_req_failed............: 100.00%  8513        0
     http_req_receiving.........: avg=102.33µs min=0s    med=0s      max=4.53ms p(90)=504.29µs p(95)=780.59µs
     http_req_sending...........: avg=27.07µs  min=0s    med=0s      max=2.52ms p(90)=0s       p(95)=55.85µs
     http_req_tls_handshaking...: avg=174.71ms min=0s    med=0s      max=18.95s p(90)=0s       p(95)=112.85ms
     http_req_waiting...........: avg=676.71ms min=0s    med=16.82ms max=23.81s p(90)=2.39s    p(95)=4.33s
     http_reqs..................: 8513    187.091907/s
     iteration_duration.........: avg=1.96s    min=1.01s med=1.03s   max=24.81s p(90)=4.56s    p(95)=6.01s
     iterations.................: 8513    187.091907/s
     vus........................: 1       min=1        max=500
     vus_max....................: 500     min=500      max=500

1722937145315