การตรวจสอบ เกณฑ์ และตัวชี้วัด
ทำความเข้าใจการตรวจสอบของ k6 สำหรับยืนยันการตอบกลับ และเกณฑ์สำหรับกำหนดเป้าหมายด้านประสิทธิภาพ
การตรวจสอบ เกณฑ์ และตัวชี้วัด เป็นบทเรียน Load Testing & Performance Benchmarking (JMeter & k6) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Load Testing & Performance Benchmarking (JMeter & k6) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Load Testing & Performance Benchmarking (JMeter & k6) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Validate Test Responses
When performance testing, simply sending requests isn't enough. We need to confirm that the server is responding correctly, not just quickly.
This is where checks come in. Checks allow you to validate the content and status of server responses.
Simple k6 Checks
A basic check in k6 confirms a condition about the response. If the condition is false, the check fails, but the test continues.
Here's how to check if an HTTP response status is 200 (OK):
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const res = http.get('https://test.k6.io');
check(res, {
'status is 200': (r) => r.status === 200,
});
console.log(`Status: ${res.status}`);
}Validate Response Content
You can also check for specific text or data within the response body. This is crucial for ensuring the server provides the expected information.
Let's check if the response body contains a specific string:
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const res = http.get('https://test.k6.io');
check(res, {
'body contains "k6.io"': (r) => r.body.includes('k6.io'),
});
console.log(`Body includes 'k6.io': ${res.body.includes('k6.io')}`);
}Combine Multiple Checks
It's common to validate several aspects of a single response. You can add multiple conditions within one check() call.
Each condition is a separate check, and k6 will report on each one individually.
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const res = http.get('https://test.k6.io');
check(res, {
'status is 200': (r) => r.status === 200,
'body size is > 0': (r) => r.body.length > 0,
'header Content-Type exists': (r) => r.headers['Content-Type'] !== undefined,
});
}Understanding k6 Metrics
k6 automatically collects various performance metrics during a test run. These metrics provide insights into your system's behavior under load.
Metrics include things like response times, data transferred, and the number of virtual users (VUs).
- Metrics are quantitative measures of performance.
- k6 aggregates these into summaries at the end of a test.
- They help identify bottlenecks and performance trends.
Common Built-in Metrics
k6 provides several built-in metrics that are always collected. Some key ones include:
http_req_duration: Total time for HTTP requests.http_req_failed: Rate of failed HTTP requests (e.g., non-2xx status).vus: Current number of active virtual users.iterations: Number of times thedefaultfunction has run.
import http from 'k6/http';
export default function () {
http.get('https://test.k6.io');
// k6 automatically collects metrics like http_req_duration
// and http_req_failed for this request.
}Create Custom Metrics
Beyond built-in metrics, you can define your own custom metrics to track specific application logic or business processes.
k6 offers various custom metric types like Counter, Gauge, Trend, and Rate.
import http from 'k6/http';
import { Trend } from 'k6/metrics';
const myCustomTrend = new Trend('my_custom_processing_time');
export default function () {
const start = Date.now();
const res = http.get('https://test.k6.io');
const end = Date.now();
myCustomTrend.add(end - start); // Add value to custom trend metric
console.log(`Request took ${end - start}ms`);
}Set Performance Goals
While checks validate individual responses, thresholds define performance goals for your entire test run, based on aggregated metrics.
If a threshold is breached, the k6 test run will fail, indicating a performance regression or unmet SLA (Service Level Agreement).
Add Thresholds to Your Test
Thresholds are defined in the options object of your k6 script. You specify a metric and a condition it must meet.
Here, we ensure the average response time is below 200ms and less than 1% of requests fail.
import http from 'k6/http';
export const options = {
thresholds: {
'http_req_duration{expected_response:true}': ['p(95)<200'], // 95th percentile response time < 200ms
'http_req_failed': ['rate<0.01'], // less than 1% of requests failed
},
};
export default function () {
http.get('https://test.k6.io');
}Quick Check: k6 Concepts
Which of the following statements about k6 checks and thresholds are TRUE?
Recap: Checks, Thresholds, Metrics
Great job! You've learned how to make your k6 tests more robust and goal-oriented.
- Checks validate individual server responses for correctness.
- Metrics are quantitative measurements collected during a test run (e.g., response time, error rate).
- Thresholds define performance goals based on aggregated metrics, failing the test if conditions aren't met.
เรียนรู้ Load Testing & Performance Benchmarking (JMeter & k6) ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การตรวจสอบ เกณฑ์ และตัวชี้วัด” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตรวจสอบ เกณฑ์ และตัวชี้วัด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Load Testing & Performance Benchmarking (JMeter & k6) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Load Testing & Performance Benchmarking (JMeter & k6) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตรวจสอบ เกณฑ์ และตัวชี้วัด”
ทำความเข้าใจการตรวจสอบของ k6 สำหรับยืนยันการตอบกลับ และเกณฑ์สำหรับกำหนดเป้าหมายด้านประสิทธิภาพ คุณปฏิบัติ Load Testing & Performance Benchmarking (JMeter & k6) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Load Testing & Performance Benchmarking (JMeter & k6) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Load Testing & Performance Benchmarking (JMeter & k6) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การตรวจสอบ เกณฑ์ และตัวชี้วัด” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Load Testing & Performance Benchmarking (JMeter & k6) นี้ได้ไหม
ได้ บทเรียน Load Testing & Performance Benchmarking (JMeter & k6) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การติดตั้ง k6 และ CLI
- การเขียนสคริปต์ k6 เบื้องต้น
- การตรวจสอบ เกณฑ์ และตัวชี้วัด
- กลุ่มและแท็กสำหรับจัดระเบียบการทดสอบ