> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/grafana/k6/llms.txt
> Use this file to discover all available pages before exploring further.

# k6

> The k6 module provides core test functionality including checks, groups, and test control.

The `k6` module is the main module that provides core k6 functionality for load testing scripts.

## Functions

### check(val, sets, \[tags])

Runs checks on a value and emits check metrics.

<ParamField path="val" type="any">
  The value to test
</ParamField>

<ParamField path="sets" type="object">
  Object with check names as keys and check functions as values
</ParamField>

<ParamField path="tags" type="object" optional>
  Optional tags to attach to the check metrics
</ParamField>

<ResponseField name="result" type="boolean">
  Returns `true` if all checks passed, `false` otherwise
</ResponseField>

```javascript theme={null}
import { check } from 'k6';
import http from 'k6/http';

export default function () {
  const res = http.get('https://test.k6.io');
  check(res, {
    'status is 200': (r) => r.status === 200,
    'response time < 500ms': (r) => r.timings.duration < 500,
  });
}
```

### fail(\[message])

Throws an error and interrupts the VU execution.

<ParamField path="message" type="string" optional>
  Error message to display
</ParamField>

```javascript theme={null}
import { fail } from 'k6';

export default function () {
  if (someCondition) {
    fail('Test failed due to condition');
  }
}
```

### group(name, fn)

Runs a function within a named group for better organization and metrics.

<ParamField path="name" type="string">
  Name of the group
</ParamField>

<ParamField path="fn" type="function">
  Function to execute within the group. Must be synchronous.
</ParamField>

<ResponseField name="result" type="any">
  Returns the value returned by the function
</ResponseField>

```javascript theme={null}
import { group } from 'k6';
import http from 'k6/http';

export default function () {
  group('visit homepage', function () {
    http.get('https://test.k6.io');
  });

  group('API tests', function () {
    http.get('https://test.k6.io/api/v1/users');
  });
}
```

<Warning>
  The `group()` function does not support async functions. Use synchronous functions only.
</Warning>

### randomSeed(seed)

Sets the seed for the random generator used for this VU.

<ParamField path="seed" type="integer">
  Seed value for the random number generator
</ParamField>

```javascript theme={null}
import { randomSeed } from 'k6';

export default function () {
  randomSeed(12345);
  // Math.random() will now produce deterministic values
}
```

### sleep(seconds)

Suspends VU execution for the specified duration.

<ParamField path="seconds" type="number">
  Duration in seconds (can be fractional)
</ParamField>

```javascript theme={null}
import { sleep } from 'k6';

export default function () {
  // Sleep for 1.5 seconds
  sleep(1.5);
}
```

## Examples

### Basic Load Test

```javascript theme={null}
import { check, group, sleep } from 'k6';
import http from 'k6/http';

export const options = {
  vus: 10,
  duration: '30s',
};

export default function () {
  group('Homepage', function () {
    const res = http.get('https://test.k6.io');
    check(res, {
      'status is 200': (r) => r.status === 200,
    });
  });

  sleep(1);
}
```
