协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
k6【开发与工程】Implements best practices for load testing web applications using K6, a modern performance testing tool optimized for developers.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: k6 description: Implements best practices for load testing web applications using K6, a modern performance testing tool optimized for developers. license: MIT compatibility: opencode metadata: version: "1.1.1" domain: performance triggers: load testing, performance testing, K6, web applications, system performance archetypes: [implementation, reference] anti_triggers: [neglecting script maintenance, ignoring load test results] response_profile: verbosity: medium directive_strength: high abstraction_level: operational
role: implementation scope: implementation output-format: code
Load testing ensures that your applications can handle expected usage patterns and helps identify performance bottlenecks. K6 is an excellent tool for developers seeking to implement modern performance testing efficiently. Below are strategies for effective load testing using K6:
Here’s a basic example of loading testing with K6:
import http from 'k6/http';
import { sleep } from 'k6';
export default function () {
http.get('https://test.k6.io');
sleep(1);
}
Monitor key performance indicators such as response time, throughput, and error rates during load tests to evaluate performance thoroughly.
By implementing effective load testing with K6, teams can ensure their applications perform optimally under expected workloads, thus enhancing application reliability and user satisfaction over time.
A more realistic load test with parameterized options, multi-stage ramp profiles, thresholds, and response checks:
import http from 'k6/http';
import { check, sleep } from 'k6';
import { Rate, Trend } from 'k6/metrics';
export const options = {
stages: [
{ duration: '2m', target: 50 }, // ramp up to 50 VUs
{ duration: '5m', target: 50 }, // sustain at 50 VUs
{ duration: '2m', target: 100 }, // ramp up to 100 VUs
{ duration: '5m', target: 100 }, // sustain at 100 VUs
{ duration: '2m', target: 0 }, // ramp down to 0 VUs
],
thresholds: {
http_req_duration: ['p(95)<2000', 'p(99)<5000'],
http_req_failed: ['rate<0.05'],
},
};
export default function () {
const params = {
headers: { 'Content-Type': 'application/json' },
};
const loginRes = http.post(
'https://test-api.example.com/login',
JSON.stringify({ username: `user_${__VU}`, password: 'test' }),
params,
);
check(loginRes, {
'login succeeded': (r) => r.status === 200,
'token received': (r) => r.json('token') !== undefined,
});
sleep(__ITER % 3 === 0 ? 2 : 1);
const searchRes = http.get(
`https://test-api.example.com/search?q=product_${__ITER % 10}`,
params,
);
check(searchRes, {
'search returned results': (r) => r.status === 200,
'response under 500ms': (r) => r.timings.duration < 500,
});
sleep(1);
}
stages or scenarios to simulate real user traffic patternsthresholds for request duration, error rate, and throughput to define pass/fail criteria--env flags