Open source load testing artifact K6

Introduction: K6 is a powerful open source load and performance testing tool for testing the performance and reliability of software systems. The use of K6 is mainly to write and run test scripts. These scripts are mainly written in JavaScript and can be tested using various protocols such as HTTP and WebSocket. And it is easy to install and run, and has rich functions, capable of complex load testing. The test script is written in JavaScript, which can write complex test logic, simulate real user behavior, and support viewing and modifying source code.

Compared:

JMeter: K6更轻量,更易于自动化。而JMeter虽然功能强大,但是界面复杂,学习曲线较陡峭。
Locust: K6支持更多的协议,如gRPC和WebSocket。
Gatling: K6的测试脚本使用JavaScript编写,比Gatling的Scala更通用,更易学。

github project installation address:

https://github.com/grafana/k6/releases

Install:

# Debian的Linux系统
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
echo "deb https://dl.bintray.com/loadimpact/deb stable main" | sudo tee -a /etc/apt/sources.list
sudo apt-get update
sudo apt-get install k6

# Mac
brew install k6

# Windows的 MSI 安装程序
https://github.com/grafana/k6/releases

Case source code: sc.js

import http from 'k6/http';
import { sleep } from 'k6';

export default function () {
  http.get('http://test.k6.io');
  sleep(1);
}

run:

# case-1
k6 run sc.js

# case-2
k6 run sc.js -u 100 -d 60s

Detailed parameter explanation:

-u, --vus:同时运行的虚拟用户(VUs)的数量。例如,k6 run --vus 10 script.js将会在执行script.js时使用10个VUs。

-d, --duration:测试的持续时间。例如,k6 run --duration 60s script.js将会运行测试60秒。

-s, --stages:阶段性的负载配置。这允许你在一个测试中改变虚拟用户的数量。例如,k6 run --stages 30s:20,1m:15,30s:10 script.js 将会在前30秒中使用20个VUs,在接下来的1分钟中使用15个VUs,然后在最后的30秒中使用10个VUs。

-i, --iterations:测试执行的总迭代次数。例如,k6 run --iterations 100 script.js将会运行测试直到达到100次迭代。

--paused:开始一个已经暂停的测试。这个选项允许你从K6的HTTP API开始测试。

--no-teardown:禁用teardown阶段。

--no-setup:禁用setup阶段。

-e, --env:设置环境变量。例如,k6 run -e MYVAR=myvalue script.js将会设置一个名为MYVAR的环境变量。

--http-debug:打印调试信息。例如,k6 run --http-debug=full script.js将会打印出所有HTTP请求和响应。

Running result 1:

 Running result 2:

The running status of the service under test:

Precautions:

1. The execution environment of the K6 script is not a complete browser or Node.js environment, so some APIs specific to these environments cannot be used.

2. It should simulate real user behavior as much as possible. This may require generating different user credentials, using appropriate intervals and delays, etc.

3. Pay attention to observe and analyze the test results. The detailed reports generated by K6 can help to understand the behavior of the system under different loads.

Finally: The complete software testing video tutorial below has been organized and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

Guess you like

Origin blog.csdn.net/wx17343624830/article/details/131918167