Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 08:23:21 +08:00
commit 2e4c7302a4
11 changed files with 852 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
# Assets
Bundled resources for performance-test-suite skill
- [ ] test_template.js: Template file for creating k6 performance tests.
- [ ] report_template.html: HTML template for generating performance test reports.
- [ ] example_test_configurations.json: Example configurations for different types of performance tests.

View File

@@ -0,0 +1,121 @@
[
{
"_comment": "Example configuration for a basic load test with gradual ramp-up",
"test_name": "Basic Load Test - Ramp Up",
"test_type": "load",
"description": "Simulates a gradual increase in users to test system responsiveness under increasing load.",
"target_url": "https://example.com/api/users",
"http_method": "GET",
"num_users": 100,
"ramp_up_time": 60,
"duration": 300,
"request_body": null,
"headers": {
"Content-Type": "application/json",
"Accept": "application/json"
},
"assertions": [
{
"type": "status_code",
"value": 200
},
{
"type": "response_time",
"max_ms": 500
}
],
"metrics": [
"response_time",
"requests_per_second",
"error_rate"
]
},
{
"_comment": "Example configuration for a stress test to find the breaking point",
"test_name": "Stress Test - Breaking Point",
"test_type": "stress",
"description": "Gradually increases load until the system reaches its breaking point.",
"target_url": "https://example.com/api/products",
"http_method": "POST",
"num_users": 500,
"ramp_up_time": 120,
"duration": 600,
"request_body": {
"product_name": "Test Product",
"price": 99.99
},
"headers": {
"Content-Type": "application/json"
},
"assertions": [
{
"type": "status_code",
"value": 201
}
],
"metrics": [
"cpu_usage",
"memory_usage",
"database_connections",
"error_rate"
]
},
{
"_comment": "Example configuration for a spike test simulating a flash sale",
"test_name": "Spike Test - Flash Sale",
"test_type": "spike",
"description": "Simulates a sudden surge in traffic to test system resilience.",
"target_url": "https://example.com/api/orders",
"http_method": "POST",
"num_users": 2000,
"ramp_up_time": 10,
"duration": 60,
"request_body": {
"user_id": 123,
"product_id": 456,
"quantity": 1
},
"headers": {
"Content-Type": "application/json"
},
"assertions": [
{
"type": "status_code",
"value": 201
}
],
"metrics": [
"response_time",
"requests_per_second",
"error_rate",
"database_queue_length"
]
},
{
"_comment": "Example configuration for an endurance test to check for memory leaks",
"test_name": "Endurance Test - Memory Leak Check",
"test_type": "endurance",
"description": "Runs a sustained load for an extended period to detect memory leaks and performance degradation.",
"target_url": "https://example.com/api/data",
"http_method": "GET",
"num_users": 50,
"ramp_up_time": 30,
"duration": 3600,
"request_body": null,
"headers": {
"Authorization": "Bearer <your_api_key>"
},
"assertions": [
{
"type": "status_code",
"value": 200
}
],
"metrics": [
"memory_usage",
"cpu_usage",
"response_time",
"garbage_collection_count"
]
}
]

View File

@@ -0,0 +1,203 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Performance Test Report</title>
<style>
/* Inline CSS for styling */
body {
font-family: sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
color: #333;
}
.container {
width: 90%;
max-width: 1200px;
margin: 20px auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1, h2, h3 {
color: #0056b3;
margin-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
padding: 10px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}
.summary {
margin-top: 20px;
padding: 15px;
background-color: #e9ecef;
border-radius: 5px;
}
.summary p {
margin: 5px 0;
}
.charts {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
margin-top: 20px;
}
.chart {
width: 45%;
min-width: 300px;
margin-bottom: 20px;
border: 1px solid #ddd;
padding: 10px;
border-radius: 5px;
box-sizing: border-box; /* Important for padding */
}
.chart img {
max-width: 100%;
height: auto;
}
@media (max-width: 768px) {
.container {
width: 95%;
}
.chart {
width: 100%;
}
}
.bottlenecks {
margin-top: 20px;
padding: 15px;
background-color: #ffebee;
border-radius: 5px;
border: 1px solid #ef9a9a;
}
.bottlenecks h3 {
color: #d32f2f;
}
.recommendations {
margin-top: 20px;
padding: 15px;
background-color: #e8f5e9;
border-radius: 5px;
border: 1px solid #a5d6a7;
}
.recommendations h3 {
color: #388e3c;
}
</style>
</head>
<body>
<!-- Main Container -->
<div class="container">
<!-- Report Header -->
<h1>Performance Test Report</h1>
<p>Test Run: {{test_run_id}}</p>
<p>Date: {{report_date}}</p>
<!-- Test Summary -->
<div class="summary">
<h2>Test Summary</h2>
<p><strong>Test Type:</strong> {{test_type}}</p>
<p><strong>Target URL:</strong> {{target_url}}</p>
<p><strong>Number of Users:</strong> {{number_of_users}}</p>
<p><strong>Duration:</strong> {{duration}}</p>
<p><strong>Status:</strong> {{test_status}}</p>
</div>
<!-- Key Metrics Table -->
<h2>Key Metrics</h2>
<table>
<thead>
<tr>
<th>Metric</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Average Response Time</td>
<td>{{average_response_time}} ms</td>
</tr>
<tr>
<td>Throughput</td>
<td>{{throughput}} requests/second</td>
</tr>
<tr>
<td>Error Rate</td>
<td>{{error_rate}} %</td>
</tr>
<tr>
<td>95th Percentile Response Time</td>
<td>{{percentile_95_response_time}} ms</td>
</tr>
</tbody>
</table>
<!-- Charts -->
<div class="charts">
<div class="chart">
<h3>Response Time Over Time</h3>
<img src="{{response_time_chart_url}}" alt="Response Time Chart">
</div>
<div class="chart">
<h3>Throughput Over Time</h3>
<img src="{{throughput_chart_url}}" alt="Throughput Chart">
</div>
<!-- Add more charts as needed -->
</div>
<!-- Bottleneck Identification -->
<div class="bottlenecks">
<h3>Bottleneck Identification</h3>
<p>{{bottlenecks_summary}}</p>
<ul>
{{#bottlenecks}}
<li>{{.}}</li>
{{/bottlenecks}}
</ul>
</div>
<!-- Recommendations -->
<div class="recommendations">
<h3>Recommendations</h3>
<p>{{recommendations_summary}}</p>
<ul>
{{#recommendations}}
<li>{{.}}</li>
{{/recommendations}}
</ul>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,65 @@
// test_template.js - Template for K6 performance tests
// Import necessary modules from K6
import http from 'k6/http';
import { check, sleep } from 'k6';
// Configuration options
export const options = {
// Stages define the load pattern
stages: [
// Example: Ramp-up to 10 virtual users (VUs) over 10 seconds
{ duration: '10s', target: 10 },
// Example: Maintain 10 VUs for 30 seconds
{ duration: '30s', target: 10 },
// Example: Ramp-down to 0 VUs over 10 seconds
{ duration: '10s', target: 0 },
],
// Thresholds define pass/fail criteria
thresholds: {
// Example: 95th percentile response time should be below 200ms
http_req_duration: ['p95<200'],
// Example: 99% of requests should be successful
http_req_failed: ['rate<0.01'], // <1% failure rate
},
};
// Define the virtual user (VU) function
export default function () {
// Replace with your target URL
const url = 'YOUR_TARGET_URL_HERE';
// Replace with your request parameters (optional)
const params = {
headers: {
'Content-Type': 'application/json',
},
};
// Replace with your request body (optional)
const payload = JSON.stringify({
key1: 'value1',
key2: 'value2',
});
// Make an HTTP request
const res = http.get(url, params); // or use http.post, http.put, http.delete, etc.
// Check the response status code
check(res, {
'status is 200': (r) => r.status === 200,
});
// Add more checks as needed
// Example: check(res, { 'response time < 500ms': (r) => r.timings.duration < 500 });
// Introduce a delay between requests (optional)
sleep(1); // Sleep for 1 second
// Log response data for debugging (optional - REMOVE IN PRODUCTION!)
// console.log(res.body);
}