K6
基本上,它是一个免费和开源的负载测试工具,用于测试API的性能。K6 有一个面向目标的测试模式,用户可以在构建测试时使用Thresholds定义目标。
测试的不同类型
有许多类型的测试属于性能测试,每种类型的测试都有不同的目的。不同的测试类型定义和优化性能。有许多性能测试是:
K6中的负载测试
负载测试是指我们通过应用一些负载来检查应用程序的性能,这些负载要么小于要么等于所需的负载。
例如:
假设,你正在从互联网上下载一系列大文件。在这种情况下,它被称为负载测试。
在K6中写一个脚本,并将其保存为Load.js。
import http from 'k6/http';
import { check, group, sleep } from 'k6';
export const options = {
stages: [
{ duration: '5m', target: 100 }, // simulate ramp-up of traffic from 1 to 100 users over 5 minutes.
{ duration: '10m', target: 100 }, // stay at 100 users for 10 minutes
{ duration: '5m', target: 0 }, // ramp-down to 0 users
],
thresholds: {
'http_req_duration': ['p(99)<1500'], // 99% of requests must complete below 1.5s
'logged in successfully': ['p(99)<1500'], // 99% of requests must complete below 1.5s
},
};
const BASE_URL = 'https://test-api.k6.io';
const USERNAME = 'TestUser';
const PASSWORD = 'SuperCroc2020';
export default () => {
const loginRes = http.post(`${BASE_URL}/auth/token/login/`, {
username: USERNAME,
password: PASSWORD,
});
check(loginRes, {
'logged in successfully': (resp) => resp.json('access') !== '',
});
const authHeaders = {
headers: {
Authorization: `Bearer ${loginRes.json('access')}`,
},
};
const myObjects = http.get(`${BASE_URL}/my/crocodiles/`, authHeaders).json();
check(myObjects, { 'retrieved crocodiles': (obj) => obj.length > 0 });
sleep(1);
};
烟雾测试
烟雾测试是以最小的负载来配置的,这意味着它是一个正常和定期的测试负载。你想在每次编写新的脚本或修改现有的脚本时运行烟雾测试作为理智的检查。
例如:
验证你的系统在最小负载下没有出现任何错误。
在K6中写一个脚本,并将其保存为Smoke.js。
import http from 'k6/http';
import { check, group, sleep, fail } from 'k6';
export const options = {
vus: 1, // 1 user looping for 1 minute
duration: '1m',
thresholds: {
http_req_duration: ['p(99)<1500'], // 99% of requests must complete below 1.5s
},
};
const BASE_URL = 'https://test-api.k6.io';
const USERNAME = 'TestUser';
const PASSWORD = 'SuperCroc2020';
export default () => {
const loginRes = http.post(`${BASE_URL}/auth/token/login/`, {
username: USERNAME,
password: PASSWORD,
});
check(loginRes, {
'logged in successfully': (resp) => resp.json('access') !== '',
});
const authHeaders = {
headers: {
Authorization: `Bearer ${loginRes.json('access')}`,
},
};
const myObjects = http.get(`${BASE_URL}/my/crocodiles/`, authHeaders).json();
check(myObjects, { 'retrieved crocodiles': (obj) => obj.length > 0 });
sleep(1);
};
压力测试的目的是在极端条件下验证系统的稳定性和可靠性。压力测试包括运行模拟,以确定隐藏的漏洞。压力测试是测试应用程序、软件或网站在承受极端压力--意外负载时的表现。
例如:
压力测试是非常有必要的,因为在一些结果出来的那天,大量的用户/申请人/学生会登录到应用程序来检查他们的结果。
在k6中写一个脚本,并将其保存为Stress.js
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
stages: [
{ duration: '2m', target: 100 }, // below normal load
{ duration: '5m', target: 100 },
{ duration: '2m', target: 200 }, // normal load
{ duration: '5m', target: 200 },
{ duration: '2m', target: 300 }, // around the breaking point
{ duration: '5m', target: 300 },
{ duration: '2m', target: 400 }, // beyond the breaking point
{ duration: '5m', target: 400 },
{ duration: '10m', target: 0 }, // scale down. Recovery stage.
],
};
export default function () {
const BASE_URL = 'https://test-api.k6.io'; // make sure this is not production
const responses = http.batch([
['GET', `${BASE_URL}/public/crocodiles/1/`, null, { tags: { name: 'PublicCrocs' } }],
['GET', `${BASE_URL}/public/crocodiles/2/`, null, { tags: { name: 'PublicCrocs' } }],
['GET', `${BASE_URL}/public/crocodiles/3/`, null, { tags: { name: 'PublicCrocs' } }],
['GET', `${BASE_URL}/public/crocodiles/4/`, null, { tags: { name: 'PublicCrocs' } }],
]);
sleep(1);
}
泡水测试
基本上,Soak测试被称为耐力和能力测试。Soak测试是一种软件测试,在这种测试中,系统在一个连续可用的时期内,在巨大的负载下进行测试,以检查系统在生产使用中的行为。
例如:
浸泡测试非常适合于面临巨大交易率的网站或应用程序,比方说一小时内有1000个交易。
在k6中写一个脚本并保存为Soak.js
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
stages: [
{ duration: '2m', target: 400 }, // ramp up to 400 users
{ duration: '3h56m', target: 400 }, // stay at 400 for ~4 hours
{ duration: '2m', target: 0 }, // scale down. (optional)
],
};
const API_BASE_URL = 'https://test-api.k6.io';
export default function () {
http.batch([
['GET', `${API_BASE_URL}/public/crocodiles/1/`],
['GET', `${API_BASE_URL}/public/crocodiles/2/`],
['GET', `${API_BASE_URL}/public/crocodiles/3/`],
['GET', `${API_BASE_URL}/public/crocodiles/4/`],
]);
sleep(1);
}
I hope you enjoyed it and it helped you!! stay connected for more future blogs.
Thank you!!
References:
https://k6.io/docs/results-visualization/cloud/
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理