DEV Community

Raj Beemi
Raj Beemi

Posted on

Improved k6 Load Test Script with Custom Metrics, Tags, and Labels

Integrating custom metrics with tags and labels in your k6 load test script provides more granular insights and better organization of your performance data. This allows you to track specific aspects of your application and analyze performance across different dimensions.

Here's an improved version of the k6 load test script that includes custom metrics with tags and labels:

import http from 'k6/http';
import { check, sleep } from 'k6';
import { Counter, Trend } from 'k6/metrics';

// Custom metrics with labels
const myCounter = new Counter('my_custom_counter');
const myTrend = new Trend('my_custom_trend');

export let options = {
    vus: 10, // number of virtual users
    duration: '30s', // test duration
    thresholds: {
        'http_req_duration': ['p(95)<500'], // 95% of requests must complete below 500ms
    },
};

export default function () {
    let res = http.get('https://api.yoursite.com/endpoint', {
        tags: { name: 'APIEndpoint' }, // tagging the request
    });

    // Add custom metric with tags
    myCounter.add(1, { tag: 'requests' });
    myTrend.add(res.timings.duration, { tag: 'response_time' });

    // Check the response status and add a tag for success or failure
    let checkResult = check(res, {
        'status was 200': (r) => r.status === 200,
    });

    // Log results with tags
    if (checkResult) {
        myCounter.add(1, { tag: 'success' });
    } else {
        myCounter.add(1, { tag: 'failure' });
    }

    // Additional label for different environments
    myTrend.add(res.timings.duration, { environment: 'production' });

    sleep(1);
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. Custom Metrics with Labels and Tags:

    • Counter and Trend Metrics: The script defines custom metrics using Counter and Trend.
    • Adding Tags and Labels: Metrics are recorded with tags and labels to provide more context. For example, tags are added to distinguish between request types or response times.
  2. Thresholds:

    • Performance Thresholds: A threshold is set to ensure 95% of requests complete in less than 500ms. This is useful for monitoring and ensuring SLAs.
  3. Tagged Requests:

    • Tags in Requests: Each HTTP request is tagged with a descriptive label, such as name: 'APIEndpoint', to categorize and filter metrics in Datadog.
  4. Check Results with Tags:

    • Conditional Tags: Based on the result of the check function, the counter is incremented with success or failure tags. This helps in distinguishing between successful and failed requests.
  5. Environment Labels:

    • Environment Context: Additional labels like environment: 'production' are used to provide context about the environment in which the test is being run. This is useful when comparing metrics across different environments.

Running the Script and Visualizing in Datadog

To run the script and send metrics to Datadog:

  1. Run the Script:
   k6 run --out datadog load_test.js
Enter fullscreen mode Exit fullscreen mode
  1. Visualize Metrics:
    • Metrics Explorer: Navigate to Metrics Explorer in Datadog, search for your custom metrics (my_custom_counter and my_custom_trend), and apply filters using the tags (requests, response_time, success, failure, environment).
    • Dashboard Creation: Add these metrics to a dashboard to visualize performance over time and across different tags.

Conclusion

By enhancing your k6 load test script with custom metrics, tags, and labels, you gain more detailed insights into your application's performance. This approach allows you to monitor specific aspects of your application, identify performance bottlenecks, and make data-driven decisions to improve reliability and user experience.

Integrating with Datadog provides a robust platform for real-time monitoring and alerting, ensuring you can quickly respond to any issues detected during load testing.

Happy testing and monitoring!

Top comments (0)