---
title: "Using k6 for load testing | Grafana Enterprise Logs documentation"
description: "Using the k6 Loki extension for load testing."
---

# Using k6 for load testing

Grafana [k6](/oss/k6/) is a modern load-testing tool. Its clean and approachable scripting [API](/docs/k6/latest/javascript-api/) works locally or in the cloud. Its configuration makes it flexible.

The [xk6-loki extension](https://github.com/grafana/xk6-loki) permits pushing logs to and querying logs from a Loki instance. It acts as a Loki client, simulating real-world load to test the scalability, reliability, and performance of your Loki installation.

## Before you begin

k6 is written in Golang. [Download and install](https://go.dev/doc/install) a Go environment.

## Installation

`xk6-loki` is an extension to the k6 binary. Build a custom k6 binary that includes the `xk6-loki` extension.

1. Install the `xk6` extension bundler:
   
   Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```bash
   go install go.k6.io/xk6/cmd/xk6@latest
   ```
2. Check out the `grafana/xk6-loki` repository:
   
   Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```bash
   git clone https://github.com/grafana/xk6-loki
   cd xk6-loki
   ```
3. Build k6 with the extension:
   
   Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```bash
   make k6
   ```

## Usage

Use the custom-built k6 binary in the same way as a non-custom k6 binary:

Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```bash
./k6 run test.js
```

`test.js` is a Javascript load test. Refer to the [k6 documentation](/docs/k6/latest/) to get started.

### Scripting API

The custom-built k6 binary provides a Javascript `loki` module.

Your Javascript load test imports the module:

js ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```js
import loki from 'k6/x/loki';
```

Classes of this module are:

Expand table

| class    | description                                   |
|----------|-----------------------------------------------|
| `Config` | configuration for the `Client` class          |
| `Client` | client for writing and reading logs from Loki |

`Config` and `Client` must be called on the k6 init context (see [Test life cycle](/docs/k6/latest/using-k6/test-lifecycle/)) outside of the default function so the client is only configured once and shared between all VU iterations.

The `Client` class exposes the following instance methods:

Expand table

| method                                         | description                                                                                                                              |
|------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
| `push()`                                       | shortcut for `pushParameterized(5, 800*1024, 1024*1024)`                                                                                 |
| `pushParameterized(streams, minSize, maxSize)` | execute push request ( [POST /loki/api/v1/push](/docs/loki/latest/reference/loki-http-api/#ingest-logs)                                  |
| `instantQuery(query, limit)`                   | execute instant query ( [GET /loki/api/v1/query](/docs/loki/latest/reference/loki-http-api/#query-logs-at-a-single-point-in-time)        |
| `client.rangeQuery(query, duration, limit)`    | execute range query ( [GET /loki/api/v1/query\_range](/docs/loki/latest/reference/loki-http-api/#query-logs-within-a-range-of-time)      |
| `client.labelsQuery(duration)`                 | execute labels query ( [GET /loki/api/v1/labels](/docs/loki/latest/reference/loki-http-api/#query-labels)                                |
| `client.labelValuesQuery(label, duration)`     | execute label values query ( [GET /loki/api/v1/label/&lt;name&gt;/values](/docs/loki/latest/reference/loki-http-api/#query-label-values) |
| `client.seriesQuery(matchers, duration)`       | execute series query ( [GET /loki/api/v1/series](/docs/loki/latest/reference/loki-http-api/#query-streams)                               |

**Javascript load test example:**

js ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```js
import loki from 'k6/x/loki';

const timeout = 5000; // ms
const conf = loki.Config("http://localhost:3100", timeout);
const client = loki.Client(conf);

export default () => {
   client.pushParameterized(2, 512*1024, 1024*1024);
};
```

Refer to [grafana/xk6-loki](https://github.com/grafana/xk6-loki#javascript-api) for the complete `k6/x/loki` module API reference.
