101*_*101 6 load-testing performance-testing k6
如官方的loadimpact / k6文档所示,我们能够执行一个k6脚本,如下所示:
k6 run ../tests/http_get.js
Run Code Online (Sandbox Code Playgroud)
如何单次执行多个脚本文件?特别是驻留在给定本地目录中的所有脚本。就像是:
k6 run ../tests/
Run Code Online (Sandbox Code Playgroud)
k6是否支持开箱即用?
小智 9
Depending on your setup there are a couple different ways you can solve this. A pretty straight forward way is to fork the k6 run command inside bash.
#!/bin/sh
k6 run test1_spec.js &
k6 run test2_spec.js &
k6 run test3_spec.js
Run Code Online (Sandbox Code Playgroud)
You could easily write some more complicated bash scripting to read in everything from the /tests/ directory and run them like that. I chose to do it like this though because I had some custom input params to give to each specific test.
Another way would be to write a docker compose script to do pretty much the same thing. This would start up a docker container for each test and run it inside there. The k6 docker image is nothing more than a tiny linux image with the k6 binary added to it.
version: '3'
services:
k6_test:
image: loadimpact/k6
container_name: test_k6
volumes:
- ./:/specs
command: run /tests/test_spec.js
ports:
- "6565:6565"
k6_test2:
image: loadimpact/k6
container_name: test2_k6
volumes:
- ./:/specs
command: run /tests/test2_spec.js
ports:
- "6566:6566"
Run Code Online (Sandbox Code Playgroud)
Both of these methods should allow you to run multiple tests at the same time in a CI environment as well as on your local machine.
目前,k6 只接受一个脚本文件,并且运行导出的默认函数。
import {sleep} from "k6";
import http from "k6/http";
export default function() {
http.get("http://test.loadimpact.com/");
sleep(2);
}
Run Code Online (Sandbox Code Playgroud)
也许,您可以通过使用模块来实现您的目标。将逻辑拆分为模块有助于组织代码并允许在不同的测试中重用常见用例。
查看k6 模块文档
import {sleep} from "k6";
import mainPageUserFlow from "../cases/main-page";
import billingUserFlow from "../cases/billing";
export default function() {
mainPageUserFlow();
billingUserFlow();
sleep(2);
}
Run Code Online (Sandbox Code Playgroud)
此外,您还可以更改脚本上不同虚拟用户的执行,例如https://community.k6.io/t/how-to-distribute-vus-across- different-scenarios-with-k6/49