This is documentation for the next version of Grafana Loki documentation. For the latest stable release, go to the latest version.
Use k6 to load test log queries
When designing a test scenario for load testing the read path of a Loki installation, it is important to know what types of queries you expect.
Supported query types
Loki has 5 types of queries:
- instant query
- range query
- labels query
- label values query
- series query
In a real-world use-case, such as querying Loki using it as a Grafana data source, all of these queries are used. Each of them has a different API endpoint. The xk6-loki extension provides a JavaScript API for all these query types.
Instant query
Instant queries can be executed using the function instantQuery(query, limit)
on the loki.Client instance. limit is the maximum number of entries to
return; pass 0 for no limit.
JavaScript example code fragment:
export default () => {
client.instantQuery(`rate({app="my-app-name"} | logfmt | level="error" [5m])`, 100);
}Range query
Range queries can be executed using the function rangeQuery(query, duration, limit)
on the loki.Client instance. limit is the maximum number of entries to
return; pass 0 for no limit.
JavaScript example code fragment:
export default () => {
client.rangeQuery(`{app="my-app-name"} | logfmt | level="error"`, "15m", 100);
}Labels query
Labels queries can be executed using the function labelsQuery(duration)
on the loki.Client instance:
JavaScript example code fragment:
export default () => {
client.labelsQuery("10m");
}Label values query
Label values queries can be executed using the function labelValuesQuery(label, duration)
on the loki.Client instance:
JavaScript example code fragment:
export default () => {
client.labelValuesQuery("app", "10m");
}Series query
Series queries can be executed using the function seriesQuery(matchers, duration)
on the loki.Client instance. Pass the label matchers as a plain LogQL
selector; the client adds the match[] query parameter automatically.
JavaScript example code fragment:
export default () => {
client.seriesQuery(`{app=~"loki-.*"}`, "10m");
}Metrics
The extension collects metrics that are printed in the end-of-test summary in addition to the built-in metrics. These metrics are collected only for instant and range queries.
Labels pool
With the xk6-loki extension, you can use the field labels on the Config
object. It contains label names and values that are generated in a reproducible
manner. Use the same labels cardinality configuration for both write and
read testing.
JavaScript example code fragment:
const labelCardinality = {
"app": 5,
"namespace": 2,
};
const conf = new loki.Config(BASE_URL, 10000, 1.0, labelCardinality);
const client = new loki.Client(conf);
function randomChoice(items) {
return items[Math.floor(Math.random() * items.length)];
}
export default function() {
let app = randomChoice(conf.labels.app);
let namespace = randomChoice(conf.labels.namespace);
client.rangeQuery(`{app="${app}", namespace="${namespace}"} | logfmt | level="error"`, "15m", 0);
}Alternatively, you can define your own pool of label names and values, and then randomly select labels from your pool instead of a generated pool.
JavaScript example
A more complex example of a read scenario can be found in xk6-loki repository. The test file read-scenario.js can be reused and extended for your needs.
It allows you to configure ratios for each type of query and the ratios of time ranges.
JavaScript example:
const queryTypeRatioConfig = [
{
ratio: 0.1,
item: readLabels
},
{
ratio: 0.1,
item: readLabelValues
},
{
ratio: 0.1,
item: readSeries
},
{
ratio: 0.5,
item: readRange
},
{
ratio: 0.2,
item: readInstant
},
];This configuration would execute approximately
- 10% labels requests
- 10% label values requests
- 10% requests for series
- 50% range queries
- 20% instant queries
during a test run.


