问题描述
我正在尝试使用 k6 开源平台在 jenkins 管道中进行负载测试。 下面给出的代码是我用来运行负载测试的 Jenkinsfile。 当我尝试“k6 run loadtests/performance-test.js”时,我在 jenkins 控制台中得到了完美的输出。但是“k6 cloud oadtests/performance-test.js”会抛出错误。我也在 jenkins 中添加了 k6 凭据。
pipeline {
agent any
environment {
K6_API_TOKEN=credentials("K6_API_TOKEN")
K6_CLOUD_PROJECT_ID=credentials("K6_CLOUD_PROJECT_ID")
}
stages {
stage('Performance Testing') {
steps {
echo 'Running K6 performance tests...'
echo 'Let us login into k6'
sh 'k6 login cloud --token ${K6_API_TOKEN}'
echo 'login successful'
sh 'k6 cloud loadtests/performance-test.js'
echo 'Completed Running K6 performance tests!'
}
}
}
}
但是当我运行这个时,
Let us login into k6
[Pipeline] sh
+ k6 login cloud --token ****
token: ****
[Pipeline] echo
login successful
[Pipeline] sh
+ k6 cloud loadtests/performance-test.js
/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io
Init [ 0% ] Parsing script
Init [ 0% ] Getting script options
Init [ 0% ] Consolidating options
Init [ 0% ] Building the archive
Init [ 0% ] Validating script options
time="2021-03-23T17:45:55Z" level=error msg="(500) Internal error"
ERROR: script returned exit code 255
Finished: FAILURE
下面是我的 performance-test.js 文件
import { sleep } from"k6";
import http from "k6/http";
export let options = {
duration: "0.30m",vus: 50,};
export default function() {
http.get('http://test.k6.io/contacts.PHP');
sleep(3);
};
解决方法
我可以使用 k6 cloud
自己运行脚本,所以我怀疑问题与您的 ${K6_API_TOKEN}
有关。我会在 k6 login cloud --token
命令中使用它之前尝试回显该值,以便您确认它确实设置正确并且它是一个有效的 API 密钥。
如果我将令牌设置为某个意外值(例如 .
),我可以自己重现 HTTP 500 响应。也许我们可以在使用 k6 cloud login --token
设置令牌时使用一些验证。
附带说明,如果您打算运行脚本 30 分钟,则应使用 duration: 30m
而不使用前面的 0.