> ## 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.

# Execution Context

> Access execution information and control test execution.

The execution context API provides information about the current test execution state.

## Import

```javascript theme={null}
import exec from 'k6/execution';
```

## Properties

### exec.instance

Provides information about the local k6 instance.

<ResponseField name="currentTestRunDuration" type="number">
  Current test run duration in milliseconds
</ResponseField>

<ResponseField name="iterationsCompleted" type="number">
  Number of completed iterations
</ResponseField>

<ResponseField name="iterationsInterrupted" type="number">
  Number of interrupted iterations
</ResponseField>

<ResponseField name="vusActive" type="number">
  Number of currently active VUs
</ResponseField>

<ResponseField name="vusInitialized" type="number">
  Number of initialized VUs
</ResponseField>

```javascript theme={null}
import exec from 'k6/execution';

export default function () {
  console.log(`Test duration: ${exec.instance.currentTestRunDuration}ms`);
  console.log(`Active VUs: ${exec.instance.vusActive}`);
}
```

### exec.scenario

Provides information about the current scenario.

<ResponseField name="name" type="string">
  Scenario name
</ResponseField>

<ResponseField name="executor" type="string">
  Executor type (e.g., "ramping-vus", "constant-vus")
</ResponseField>

<ResponseField name="startTime" type="number">
  Scenario start time (Unix timestamp in milliseconds)
</ResponseField>

<ResponseField name="progress" type="number">
  Scenario progress (0 to 1)
</ResponseField>

<ResponseField name="iterationInInstance" type="number">
  Current VU's iteration number in this instance
</ResponseField>

<ResponseField name="iterationInTest" type="number">
  Current VU's iteration number across all instances
</ResponseField>

```javascript theme={null}
import exec from 'k6/execution';

export default function () {
  console.log(`Scenario: ${exec.scenario.name}`);
  console.log(`Progress: ${exec.scenario.progress * 100}%`);
  console.log(`Iteration: ${exec.scenario.iterationInInstance}`);
}
```

### exec.vu

Provides information about the current VU.

<ResponseField name="idInInstance" type="number">
  VU ID within the local instance (1-based)
</ResponseField>

<ResponseField name="idInTest" type="number">
  VU ID globally across all instances (1-based)
</ResponseField>

<ResponseField name="iterationInInstance" type="number">
  Current iteration number for this VU in the instance
</ResponseField>

<ResponseField name="iterationInScenario" type="number">
  Current iteration number for this VU in the scenario
</ResponseField>

<ResponseField name="tags" type="object">
  Dynamic object for getting/setting VU tags
</ResponseField>

<ResponseField name="metrics" type="object">
  Object containing tags and metadata
</ResponseField>

```javascript theme={null}
import exec from 'k6/execution';

export default function () {
  console.log(`VU ID: ${exec.vu.idInInstance}`);
  console.log(`Iteration: ${exec.vu.iterationInInstance}`);
  
  // Set custom tags
  exec.vu.tags['custom_tag'] = 'value';
}
```

### exec.test

Provides test-level information and control.

<ResponseField name="options" type="object">
  Consolidated test options (read-only)
</ResponseField>

<ResponseField name="abort([message])" type="function">
  Aborts the test execution immediately
</ResponseField>

<ResponseField name="fail([message])" type="function">
  Marks the test as failed but continues execution
</ResponseField>

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

export default function () {
  const res = http.get('https://test.k6.io');
  
  // Abort test if critical error
  if (res.status === 500) {
    exec.test.abort('Server returned 500');
  }
  
  // Mark test as failed but continue
  if (res.timings.duration > 5000) {
    exec.test.fail('Response too slow');
  }
  
  // Access test options
  console.log(`Test duration: ${exec.test.options.duration}`);
}
```

## Examples

### VU-specific Behavior

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

export default function () {
  const vuId = exec.vu.idInInstance;
  
  // Use different endpoints based on VU ID
  const endpoint = vuId % 2 === 0 ? '/api/v1' : '/api/v2';
  http.get(`https://test.k6.io${endpoint}`);
}
```

### Progress Monitoring

```javascript theme={null}
import exec from 'k6/execution';

export default function () {
  const progress = exec.scenario.progress;
  
  // Log progress every 10%
  if (progress > 0 && progress % 0.1 < 0.01) {
    console.log(`Test is ${Math.floor(progress * 100)}% complete`);
  }
}
```

### Conditional Abort

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

let errorCount = 0;

export default function () {
  const res = http.get('https://test.k6.io');
  
  if (res.status !== 200) {
    errorCount++;
  }
  
  // Abort if too many errors
  if (errorCount > 100) {
    exec.test.abort('Too many errors detected');
  }
}
```

### Dynamic Tagging

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

export default function () {
  // Add scenario-specific tags
  exec.vu.tags['scenario'] = exec.scenario.name;
  exec.vu.tags['vu_id'] = exec.vu.idInInstance.toString();
  
  http.get('https://test.k6.io');
}
```

### Iteration-based Logic

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

export default function () {
  const iteration = exec.scenario.iterationInInstance;
  
  // Warmup for first 10 iterations
  if (iteration < 10) {
    console.log('Warmup iteration');
    return;
  }
  
  // Normal test execution
  http.get('https://test.k6.io');
}
```

<Warning>
  Execution context properties cannot be accessed in the init context. They are only available in the VU context (default function).
</Warning>
