# k6Tests

Run performance tests from the [k6-hybrid-performance-tests](https://github.com/theorchard/k6-hybrid-performance-tests) repository.

## Parameters
| Name                    | Description                                                                                       | Type           | Default  | Required                     |
|-------------------------|---------------------------------------------------------------------------------------------------|----------------|----------|------------------------------|
| **k6 parameters**       |                                                                                                   |                |          |                              |
| testFile                | The path to the test file you wish to run (relative to the tests directory)                       | `string`       | ''       | yes                          |
| backendService          | Name of the microservice to be tested                                                             | `string`       | ''       | yes for api                  |
| appUrl                  | Url of the application to be tested                                                               | `string`       | ''       | yes for graphql and frontend |
| graphqlUrl              | Url of the GraphQL endpoint to be tested                                                          | `string`       | ''       | no                           |
| k6BackendStage1Duration | Duration of the first stage of the k6 test                                                        | `string`       | ''       | yes for graphql and frontend |
| k6BackendStage1VUs      | Number of virtual users for the first stage of the k6 test                                        | `string`       | ''       | yes for graphql and frontend |
| k6BackendStage2Duration | Duration of the second stage of the k6 test                                                       | `string`       | ''       | yes for graphql and frontend |
| k6BackendStage2VUs      | Number of virtual users for the second stage of the k6 test                                       | `string`       | ''       | yes for graphql and frontend |
| k6BackendStartVus       | Number of virtual users to start from                                                             | `string`       | ''       | yes for graphql and frontend |
| k6FrontendVus           | Number of virtual users for the frontend tests                                                    | `string`       | ''       | yes for graphql and frontend |
| k6FrontendDuration      | Duration of the frontend tests                                                                    | `string`       | ''       | yes for graphql and frontend |
| k6GracefulStopDuration  | Duration to wait for graceful stop of the k6 test                                                 | `string`       | ''       | yes for graphql and frontend |
| k6MetricThresholds      | Thresholds for k6 asserting specific metrics are met. Passed as a map of JSON strings (see below) | `List<String>` | ''       | no                           |
| waitCompletion          | set this to false to not wait for execution to complete                                           | `Boolean`      | true     | no                           |
| propagateFailure        | set this to false to not propagate failures                                                       | `Boolean`      | true     | no                           |

## Usage

The k6 framework has two modes of running:

1. Api test - Runs the api.ts test file against a specified backend service. The service must be defined in the code repository as a json file.
you will need to provide the backendService parameter.
```groovy
stage('K6 Perfromance Tests') {
    when {
        branch 'master'
    }
    steps {
                k6Tests testFile: 'api.ts',
                        backendService: 'ows-vectororder',
    }
}
```

you can override json configuration for api testing by passing all needed environment variables for an adhoc run 
```groovy
stage('K6 Perfromance Tests') {
    when {
        branch 'master'
    }
    steps {
                k6Tests testFile: 'api.ts',
                        backendService: 'ows-vectororder',
                        k6BackendStage1Duration: '10s',
                        k6BackendStage1VUs: '10',
                        k6BackendStage2Duration: '10s',
                        k6BackendStage2VUs: '10',
                        k6BackendStartVus: '1',
                        k6GracefulStopDuration: '180s'
    }
}
```

2. Journey test - Runs the given test file against the application URL. The test file must be created in the tests directory and can be a combination
of backend and/or frontend scenarios. The goal with this functionality is to simulate load on specific user journeys.

```groovy
stage('K6 Perfromance Tests') {
    when {
        branch 'master'
    }
    steps {
                k6Tests testFile: 'abacus/contract-visit-test.ts',
                        appUrl: 'https://abacus.qaorch.com',
                        k6BackendStage1Duration: '10s',
                        k6BackendStage1VUs: '10',
                        k6BackendStage2Duration: '10s',
                        k6BackendStage2VUs: '10',
                        k6BackendStartVus: '1',
                        k6FrontendVus: '1',
                        k6FrontendDuration: '20s',
                        k6GracefulStopDuration: '180s'
    }
}
```

## Metric Thresholds

The `k6MetricThresholds` parameter allows you to define thresholds for k6 metrics. It should be a JSON array of objects, passed like the below:

```groovy
                k6Tests testFile: 'api.ts',
                        backendService: 'ows-vectororder',
                        k6MetricThresholds: ['{"key":"api_eligibility_duration","metric":"avg","threshold":2000}']
```

## Integrate to pipelines and not wait for exeecution to finish 

The `waitCompletion` parameter allows you to not wait for actual k6 execution and continue with pipeline
The `propagateFailures` parameter allows you to not propagate failures when execution is finished

```groovy
                k6Tests testFile: 'api.ts',
                        backendService: 'ows-vectororder',
                        waitCompletion: false,
                        propagateFailure: false
```