Load Testing & Performance Benchmarking (JMeter & k6) · บทเรียน

เมตริกแบบกำหนดเองและแนวโน้มใน k6

กำหนดเมตริก Counter, Gauge, Rate และ Trend ของคุณเองใน k6 เพื่อวัดสิ่งที่สำคัญอย่างแท้จริงสำหรับสถานการณ์ขั้นสูง

บทเรียน 4 จาก 413 ขั้นตอน

เมตริกแบบกำหนดเองและแนวโน้มใน k6 เป็นบทเรียน Load Testing & Performance Benchmarking (JMeter & k6) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Load Testing & Performance Benchmarking (JMeter & k6) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Load Testing & Performance Benchmarking (JMeter & k6) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Beyond Built-in Metrics

k6 ships with built-in metrics like http_req_duration, but advanced tests often need to measure domain-specific values: items in a cart, business transaction time, or a custom error rate. k6 lets you create custom metrics.

The Four Metric Types

k6 offers four custom metric types from k6/metrics:

  • Counter — cumulative sum.
  • Gauge — last recorded value.
  • Rate — percentage of non-zero/true values.
  • Trend — statistics like min, max, avg, p95.

Importing Metric Constructors

Import the constructors you need and create the metric objects in module scope so they are shared across iterations.

import { Counter, Trend, Rate, Gauge } from 'k6/metrics';

A Counter Example

A Counter accumulates a value over the whole test. Here we count how many orders were created.

const ordersCreated = new Counter('orders_created');

export default function () {
  ordersCreated.add(1);
}

A Trend for Timing

A Trend collects a series of numbers and reports statistics. It is ideal for timing custom logic.

import http from 'k6/http';
const loginTime = new Trend('login_time');

export default function () {
  const res = http.post('https://test.k6.io/login');
  loginTime.add(res.timings.duration);
}

A Rate for Success Ratio

A Rate tracks how often a condition is true. Add true or false and k6 reports the percentage of trues.

const successRate = new Rate('success_rate');
successRate.add(res.status === 200);

A Gauge for Last Value

A Gauge keeps only the most recent value you add. Use it for things like the current queue length reported by an endpoint.

const queueDepth = new Gauge('queue_depth');
queueDepth.add(42);

Tagging Custom Metrics

Just like built-in metrics, you can attach tags when adding a value. This lets you slice your custom metric by endpoint or feature.

loginTime.add(res.timings.duration, { region: 'eu' });

Thresholds on Custom Metrics

Custom metrics integrate fully with thresholds. You can fail a run if your business metric crosses a limit.

export const options = {
  thresholds: {
    login_time: ['p(95)<500'],
    success_rate: ['rate>0.98'],
  },
};

Reading Custom Metrics in Summary

At the end of a run, custom metrics appear in the end-of-test summary alongside built-in ones, showing the appropriate statistics for their type.

k6 run scenario.js

Choosing the Right Type

Picking the correct metric type matters: use a Trend for durations, a Rate for success ratios, a Counter for totals, and a Gauge for snapshot values. The wrong type gives misleading summaries.

Quick Check

Pick the right metric type for the job.

Recap

You can now extend k6 with custom metrics.

  • Counter sums, Gauge keeps the last value, Rate tracks ratios, Trend reports stats.
  • Add tags and thresholds to custom metrics for targeted SLOs.
  • They appear in the summary and any output backend.
เริ่มต้นได้ฟรี

เรียนรู้ Load Testing & Performance Benchmarking (JMeter & k6) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “เมตริกแบบกำหนดเองและแนวโน้มใน k6” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เมตริกแบบกำหนดเองและแนวโน้มใน k6” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Load Testing & Performance Benchmarking (JMeter & k6) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Load Testing & Performance Benchmarking (JMeter & k6) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เมตริกแบบกำหนดเองและแนวโน้มใน k6”

กำหนดเมตริก Counter, Gauge, Rate และ Trend ของคุณเองใน k6 เพื่อวัดสิ่งที่สำคัญอย่างแท้จริงสำหรับสถานการณ์ขั้นสูง คุณปฏิบัติ Load Testing & Performance Benchmarking (JMeter & k6) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Load Testing & Performance Benchmarking (JMeter & k6) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Load Testing & Performance Benchmarking (JMeter & k6) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “เมตริกแบบกำหนดเองและแนวโน้มใน k6” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Load Testing & Performance Benchmarking (JMeter & k6) นี้ได้ไหม

ได้ บทเรียน Load Testing & Performance Benchmarking (JMeter & k6) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. สถานการณ์ผู้ใช้เสมือน (VUs)
  2. การกำหนดพารามิเตอร์ข้อมูลใน k6
  3. การเรียกใช้ k6 บนคลาวด์
  4. เมตริกแบบกำหนดเองและแนวโน้มใน k6
← กลับไปที่ Load Testing & Performance Benchmarking (JMeter & k6)