0Pricing
Load Testing & Performance Benchmarking (JMeter & k6) · レッスン

k6のカスタムメトリクスとトレンド

k6で独自のCounter、Gauge、Rate、Trendメトリクスを定義し、高度なシナリオで重要な指標を正確に測定する方法を学びます。

「k6のカスタムメトリクスとトレンド」はCoddyKit上の無料Load Testing & Performance Benchmarking (JMeter & k6)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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.

よくある質問

「k6のカスタムメトリクスとトレンド」レッスンは無料ですか?

はい。「k6のカスタムメトリクスとトレンド」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Load Testing & Performance Benchmarking (JMeter & k6)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Load Testing & Performance Benchmarking (JMeter & k6)コースには全4レッスンが含まれています。

「k6のカスタムメトリクスとトレンド」で何を学びますか?

k6で独自のCounter、Gauge、Rate、Trendメトリクスを定義し、高度なシナリオで重要な指標を正確に測定する方法を学びます。 ブラウザで直接実行するハンズオンコードでLoad Testing & Performance Benchmarking (JMeter & k6)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Load Testing & Performance Benchmarking (JMeter & k6)を始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのLoad Testing & Performance Benchmarking (JMeter & k6)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン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)に戻る