使用 k6 进行性能测试
什么是性能和负载测试?
性能测试是分析产品质量和能力的过程。负载测试是性能测试的一个子集,我们在其中分析应用程序在正常或峰值负载条件下的行为。
k6是什么?
k6 是一个开源负载测试工具,用于测试 API、微服务和网站的性能。
先决条件:安装 k6 工具。
编写你的性能测试脚本:脚本必须至少包含一个默认函数——它定义了你的 VU 的入口点,类似于许多其他语言中的 main()函数。默认函数中的代码称为“VU 代码”,只要测试运行,它就会一遍又一遍地运行。
k6 使用运行脚本的虚拟用户 (VU) 的概念。 Duration 是一个字符串,指定测试应该运行的总持续时间。
创建新的负载测试时,您经常要做的第一件事是定义将用于测试系统的 HTTP 请求。一个只执行 GET 请求的简单示例如下所示:
Javascript
import{ sleep } from 'k6';
import http from 'k6/http';
export let options = {
duration : '1m',
vus : 50,
};
export default function() {
http.get('http://test.k6.io/contacts.php');
sleep(3);
}
您可以使用以下命令在本地运行测试。只要确保先安装k6 。
$ k6 run performance-test.js
这会产生以下输出:
/\ |‾‾| /‾‾/ /‾/
/\ / \ | |_/ / / /
/ \/ \ | | / ‾‾\
/ \ | |‾\ \ | (_) |
/ __________ \ |__| \__\ \___/ .io
execution: local
script: performance-test.js
output: -
scenarios: (100.00%) 1 executors, 50 max VUs, 1m30s max duration (incl. graceful stop):
* default: 50 looping VUs for 1m0s (gracefulStop: 30s)
running (1m02.5s), 00/50 VUs, 1000 complete and 0 interrupted iterations
default ✓ [======================================] 50 VUs 1m0s
data_received..............: 711 kB 11 kB/s
data_sent..................: 88 kB 1.4 kB/s
http_req_blocked...........: avg=8.97ms min=1.37µs med=2.77µs max=186.58ms p(90)=9.39µs p(95)=8.85ms
http_req_connecting........: avg=5.44ms min=0s med=0s max=115.8ms p(90)=0s p(95)=5.16ms
http_req_duration..........: avg=109.39ms min=100.73ms med=108.59ms max=148.3ms p(90)=114.59ms p(95)=119.62ms
http_req_receiving.........: avg=55.89µs min=16.15µs med=37.92µs max=9.67ms p(90)=80.07µs p(95)=100.34µs
http_req_sending...........: avg=15.69µs min=4.94µs med=10.05µs max=109.1µs p(90)=30.32µs p(95)=45.83µs
http_req_tls_handshaking...: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s
http_req_waiting...........: avg=109.31ms min=100.69ms med=108.49ms max=148.22ms p(90)=114.54ms p(95)=119.56ms
http_reqs..................: 1000 15.987698/s
iteration_duration.........: avg=3.11s min=3.1s med=3.1s max=3.3s p(90)=3.12s p(95)=3.15s
iterations.................: 1000 15.987698/s
vus........................: 50 min=50 max=50
vus_max....................: 50 min=50 max=50
HTTP 特定的内置指标:METRIC NAME DESCRIPTION http_reqs How many HTTP requests has k6 generated, in total. http_req_blocked Time spent blocked (waiting for a free TCP connection slot) before initiating the request. float http_req_connecting Time spent establishing TCP connection to the remote host. float http_req_tls_handshaking Time spent handshaking TLS session with remote host http_req_sending Time spent sending data to the remote host. float http_req_waiting Time spent waiting for response from remote host (a.k.a. \”time to first byte\”, or \”TTFB\”). float http_req_receiving Time spent receiving response data from the remote host. float http_req_duration Total time for the request. It’s equal to http_req_sending + http_req_waiting + http_req_receiving. float
摘要导出:您还可以使用 k6 运行的 –out/-o 选项以 CSV 格式输出 k6 详细统计信息,如下所示:
$ k6 run --out csv=my_test_result.csv script.js