Metriche personalizzate e trend in k6
Definisca in k6 metriche Counter, Gauge, Rate e Trend personalizzate per misurare esattamente ciò che conta negli scenari avanzati.
Metriche personalizzate e trend in k6 è una lezione Load Testing & Performance Benchmarking (JMeter & k6) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Load Testing & Performance Benchmarking (JMeter & k6), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Load Testing & Performance Benchmarking (JMeter & k6) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Impara Load Testing & Performance Benchmarking (JMeter & k6) con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Metriche personalizzate e trend in k6» è gratuita?
Sì — il testo completo di «Metriche personalizzate e trend in k6» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Load Testing & Performance Benchmarking (JMeter & k6), passa a CoddyKit PRO. Il corso Load Testing & Performance Benchmarking (JMeter & k6) include 4 lezioni in totale.
Cosa imparerò in «Metriche personalizzate e trend in k6»?
Definisca in k6 metriche Counter, Gauge, Rate e Trend personalizzate per misurare esattamente ciò che conta negli scenari avanzati. Eserciti Load Testing & Performance Benchmarking (JMeter & k6) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Load Testing & Performance Benchmarking (JMeter & k6)?
Non è richiesta alcuna esperienza precedente. Load Testing & Performance Benchmarking (JMeter & k6) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Metriche personalizzate e trend in k6»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Load Testing & Performance Benchmarking (JMeter & k6)?
Sì. Ogni lezione Load Testing & Performance Benchmarking (JMeter & k6) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Scenari di utenti virtuali (VU)
- Parametrizzazione dei dati in k6
- Esecuzione nel cloud con k6
- Metriche personalizzate e trend in k6