Benutzerdefinierte Metriken und Trends in k6
Definieren Sie in k6 eigene Counter-, Gauge-, Rate- und Trend-Metriken, um in Ihren anspruchsvollen Szenarien genau das zu messen, worauf es ankommt.
Benutzerdefinierte Metriken und Trends in k6 ist eine kostenlose Load Testing & Performance Benchmarking (JMeter & k6)-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Load Testing & Performance Benchmarking (JMeter & k6)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Load Testing & Performance Benchmarking (JMeter & k6)-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.jsChoosing 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.
Countersums,Gaugekeeps the last value,Ratetracks ratios,Trendreports stats.- Add tags and thresholds to custom metrics for targeted SLOs.
- They appear in the summary and any output backend.
Häufig gestellte Fragen
Ist die Lektion „Benutzerdefinierte Metriken und Trends in k6“ kostenlos?
Ja — der vollständige Text von „Benutzerdefinierte Metriken und Trends in k6“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Load Testing & Performance Benchmarking (JMeter & k6)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Load Testing & Performance Benchmarking (JMeter & k6)-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Benutzerdefinierte Metriken und Trends in k6“?
Definieren Sie in k6 eigene Counter-, Gauge-, Rate- und Trend-Metriken, um in Ihren anspruchsvollen Szenarien genau das zu messen, worauf es ankommt. Du übst Load Testing & Performance Benchmarking (JMeter & k6) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Load Testing & Performance Benchmarking (JMeter & k6) zu starten?
Keine Vorkenntnisse erforderlich. Load Testing & Performance Benchmarking (JMeter & k6) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Benutzerdefinierte Metriken und Trends in k6“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Load Testing & Performance Benchmarking (JMeter & k6)-Lektion Code schreiben und ausführen?
Ja. Jede Load Testing & Performance Benchmarking (JMeter & k6)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Szenarien für virtuelle Benutzer (VUs)
- Datenparametrisierung in k6
- Cloud-Ausführung mit k6
- Benutzerdefinierte Metriken und Trends in k6