K6
K6 is a command-line tool for API and system load testing. This integration uses K6's experimental-prometheus-rw output to write load-testing metrics to the DataKit Prometheus Remote Write endpoint, which then reports them to Guance. K6 does not require a persistent service; the load generator only needs network access to DataKit.
Configuration¶
Prerequisites¶
- Install K6 and verify that
k6 versionruns successfully. - Install DataKit and ensure that the load generator can access DataKit on port
9529. - Enable the
prom_remote_writecollector in DataKit. - Assign a unique
benchmark_idandtestidto each load test to prevent data from different runs from being mixed.
Configure DataKit¶
Create prom_remote_write.conf under conf.d/prom in the DataKit installation directory, as shown below:
[[inputs.prom_remote_write]]
path = "/prom_remote_write"
methods = ["PUT", "POST"]
# K6 metrics start with k6_. Write them to the k6 measurement and preserve their original names.
job_as_measurement = false
measurement_name = "k6"
keep_exist_metric_name = true
After completing the configuration, restart DataKit for the changes to take effect.
Configure K6¶
Point the Remote Write URL to DataKit and use the experimental-prometheus-rw output:
export K6_PROMETHEUS_RW_SERVER_URL="http://<datakit-ip>:9529/prom_remote_write"
export K6_PROMETHEUS_RW_PUSH_INTERVAL="5s"
export K6_PROMETHEUS_RW_TREND_STATS="p(95),p(99),min,max"
k6 run \
-o experimental-prometheus-rw \
--tag benchmark_id="k6-demo-20260917" \
--tag testid="baseline-r300-rep1" \
script.js
If HTTP Basic Authentication is enabled on the DataKit endpoint, also set K6_PROMETHEUS_RW_USERNAME and K6_PROMETHEUS_RW_PASSWORD. Inject credentials through a secrets management system; never store real passwords in scripts or command history.
K6 Script Example¶
The following script runs at a constant arrival rate for 10 minutes. Replace the target URL with the actual test endpoint and adjust the number of VUs to match the service capacity:
import http from 'k6/http';
import { check } from 'k6';
export const options = {
scenarios: {
steady_load: {
executor: 'constant-arrival-rate',
rate: 300,
timeUnit: '1s',
duration: '10m',
preAllocatedVUs: 100,
maxVUs: 300,
},
},
};
export default function () {
const response = http.get(`${__ENV.TARGET_URL}/health`);
check(response, {
'HTTP status is 200': (res) => res.status === 200,
});
}
Verify Data¶
- Check the K6 console and confirm that no HTTP errors or Remote Write output errors occurred during the test.
- Open the metric query in Guance, select the
k6measurement, and filter bybenchmark_idandtestid. - Confirm that the following metrics are continuously reported throughout the test: total requests, failure rate, average duration, P95, P99, and maximum VUs.
- Calculate the request rate from the cumulative request count; do not treat the cumulative total as RPS. The query window must be longer than
K6_PROMETHEUS_RW_PUSH_INTERVAL, or the rate chart may contain gaps.
Metrics¶
The K6 measurement is named k6. The following table lists common K6 load-testing metrics, their units, and their meanings. Duration metrics are displayed in milliseconds. Ratio metrics have raw values from 0 to 1 and are multiplied by 100 when displayed as percentages.
| Metric | Unit | Description |
|---|---|---|
k6_checks_rate |
% (raw value: 0-1) |
Ratio of successful checks; multiply by 100 when displayed as a percentage. |
k6_data_received_total |
bytes |
Cumulative amount of data received by K6. |
k6_data_sent_total |
bytes |
Cumulative amount of data sent by K6. |
k6_http_req_blocked_avg |
ms |
Average request blocking duration. |
k6_http_req_blocked_max |
ms |
Maximum request blocking duration. |
k6_http_req_blocked_p90 |
ms |
Request blocking duration 90. |
k6_http_req_blocked_p95 |
ms |
Request blocking duration 95. |
k6_http_req_blocked_p99 |
ms |
Request blocking duration 99. |
k6_http_req_connecting_avg |
ms |
Average TCP connection duration. |
k6_http_req_connecting_max |
ms |
Maximum TCP connection duration. |
k6_http_req_connecting_p90 |
ms |
Tcp connection duration 90. |
k6_http_req_connecting_p95 |
ms |
Tcp connection duration 95. |
k6_http_req_connecting_p99 |
ms |
Tcp connection duration 99. |
k6_http_req_duration_avg |
ms |
Average end-to-end HTTP request duration. |
k6_http_req_duration_max |
ms |
Maximum end-to-end HTTP request duration. |
k6_http_req_duration_p90 |
ms |
End-to-end http request duration 90. |
k6_http_req_duration_p95 |
ms |
End-to-end http request duration 95. |
k6_http_req_duration_p99 |
ms |
End-to-end http request duration 99. |
k6_http_req_failed_rate |
% (raw value: 0-1) |
HTTP request failure ratio; multiply by 100 when displayed as a percentage. |
k6_http_req_receiving_avg |
ms |
Average response data receive duration. |
k6_http_req_receiving_max |
ms |
Maximum response data receive duration. |
k6_http_req_receiving_p90 |
ms |
Response data receive duration 90. |
k6_http_req_receiving_p95 |
ms |
Response data receive duration 95. |
k6_http_req_receiving_p99 |
ms |
Response data receive duration 99. |
k6_http_req_sending_avg |
ms |
Average request data send duration. |
k6_http_req_sending_max |
ms |
Maximum request data send duration. |
k6_http_req_sending_p90 |
ms |
Request data send duration 90. |
k6_http_req_sending_p95 |
ms |
Request data send duration 95. |
k6_http_req_sending_p99 |
ms |
Request data send duration 99. |
k6_http_req_tls_handshaking_avg |
ms |
Average TLS handshake duration. |
k6_http_req_tls_handshaking_max |
ms |
Maximum TLS handshake duration. |
k6_http_req_tls_handshaking_p90 |
ms |
Tls handshake duration 90. |
k6_http_req_tls_handshaking_p95 |
ms |
Tls handshake duration 95. |
k6_http_req_tls_handshaking_p99 |
ms |
Tls handshake duration 99. |
k6_http_req_waiting_avg |
ms |
Average server response wait duration. |
k6_http_req_waiting_max |
ms |
Maximum server response wait duration. |
k6_http_req_waiting_p90 |
ms |
Server response wait duration 90. |
k6_http_req_waiting_p95 |
ms |
Server response wait duration 95. |
k6_http_req_waiting_p99 |
ms |
Server response wait duration 99. |
k6_http_reqs_total |
requests |
Cumulative HTTP request count; use this metric's RATE to calculate the request rate. |
k6_iteration_duration_avg |
ms |
Average scenario iteration duration. |
k6_iteration_duration_max |
ms |
Maximum scenario iteration duration. |
k6_iteration_duration_p90 |
ms |
Scenario iteration duration 90. |
k6_iteration_duration_p95 |
ms |
Scenario iteration duration 95. |
k6_iteration_duration_p99 |
ms |
Scenario iteration duration 99. |
k6_iterations_total |
iterations |
Cumulative number of scenario iterations. |
k6_vus |
VUs |
Current number of VUs. |
k6_vus_max |
VUs |
Maximum number of VUs during the test. |
All HTTP metrics include tags such as scenario, testid, and benchmark_id. Request-level metrics may also include method, url, name, status, error, and expected_response. Queries should filter by at least benchmark_id and testid; otherwise, data from different load-test runs will be aggregated.
Percentile metrics not configured in K6_PROMETHEUS_RW_TREND_STATS are not written. If you need P90, P95, or P99, configure them explicitly before starting K6, for example: K6_PROMETHEUS_RW_TREND_STATS="p(90),p(95),p(99),min,max".