Automated k6 performance tests for WooCommerce. To be used for benchmarking performance (both single user and under load) by simulating and measuring the response time of HTTP requests.
In the WooCommerce e2e tests there is a Docker Initialization Script [`init-sample-products.sh`](https://github.com/woocommerce/woocommerce/tree/trunk/tests/e2e/docker/init-sample-products.sh) that will set up a shop with sample products imported and the shop settings (payment method, permalinks, address etc) needed for the tests already set. It is recommended using this if to just see the tests in action.
`config.js` comes with some example values for using with the suggested local test environment. If using a different environment be sure to update the values.
When refering to running k6 tests usually this means executing the test scenario. The test scenario file in turn determines which requests we run and how much load will be applied to them. It is also possible to execute individual test files containing requests and pass in scenario config as a CLI flag but scenario files allow for more configuration options.
Included in the `tests` folder are some sample scenarios that can be ran or used as a starting point to be modified to suit the context in which tests are being ran.
`simple-all-shopper-requests.js` and `simple-all-merchant-requests.js` are included scenarios that use the `per-vu-iterations` scenario executor type to run each request sequentially for 1 iteration to make sure they are working.
`example-all-requests-ramping-vus.js` and `example-all-requests-arrival-rate.js` are included example scenarios for load testing that use the `ramping-vus` and `ramping-arrival-rate` scenario executor types to run all the requests under load of multiple virtual users. These scenarios can be modified to suit the load profile for the context that the tests will be ran.
Another aspect that affects the traffic pattern of the tests is the amount of “Think Time” in between requests. In real world usage of an application users will spend some amount of time before doing some action. For some situations there will be a need to simulate this as part of the test traffic and it is common to have this as part of load tests.
The amount of think time can be controlled from `config.js`.
>**_Note: It’s important to note to be very careful when adding load to a scenario. By accident a dangerous amount of load could be ran aginst the test environment that could effectively be like a denial-of-service attack on the test environment. Also important to consider any other consequences of running large load such as triggering of emails._**
To execute a test scenario (for example `tests/simple-all-shopper-requests.js`).
CLI `k6 run tests/simple-all-shopper-requests.js`
Docker `docker run --network="host" -v /[YOUR LOCAL WC DIRECTORY FULL PATH]/tests:/tests -it loadimpact/k6 run /tests/simple-all-shopper-requests.js`
### Debugging Tests
To help with getting a test working, the `--http-debug="full"` flag prints to console the full log of requests and their responses. It is also useful to use `console.log()` to print messages when debugging.
### User Agent
k6 adds a user agent to requests, for example `User-Agent: k6/0.33.0 (https://k6.io/)` which makes it easier to understand what load is coming from k6 synthetically generating it versus real user load when looking at server logs.
## Results Output
### Basic Results
By default when running the test using `k6 run`, there is an aggregated summary report at the end of the test.
### InfluxDB and Grafana
[See this guide for more details](https://k6.io/docs/results-visualization/influxdb-+-grafana/)
k6 tests rely on HTTP requests in order to test the backend. They can either be constructed from scratch, by using the k6 recorder, or by converting a HAR file.
The k6 recorder is a browser extension which captures http requests generated as you perform actions in a tab. It generates a test with all the HTTP requests from your actions which can then be modified to make it execuatable.
Alternatively any application which captures HTTP requests can be used to figure out the requests to include in a test such as the network section within browser developer tools.
Tests could also be created to simulate API requests.
The tests only simulate the HTTP requests and do not include static assets requests (fonts, images, css, js etc).
Including them would increase the size and complexity of the tests.
Although static resources do affect the bandwidth and the overall response time for the user, they should have a smaller impact on the server load and aren’t usually as intensive on the server as running code.
In additional any use of CDNs, cache settings, and themes used would also vary from site to site.
To make it easier to manage the headers they have been moved to a separate file so any changes can be made to all the requests at once.
In `headers.js` the common headers are grouped by type and then can be imported in for use in the requests. However if an individual request uniquely needs a specific header this can still be added in as an extra member of the headers object literal of that request.
To make a test work so it can be ran reliably multiple times usually there is a need to correlate any dynamic data in the test. This means extracting one or more values from the response of one request and then reusing them in subsequent requests.
An example of this is the `woocommerce-process-checkout-nonce` . This is returned in the response of the `/checkout``GET` request and is passed into the `/?wc-ajax=checkout``POST` request.
A value can be correlated by using k6 selector `.find` to extract the data from a response by matching an element. Alternatively for extracting data from a response unsuitable for using selectors k6 has a `findBetween` utility that makes its easier by just having to provide the left and right boundaries of the data.
Groups are used to organize common logic in the test scripts and can help with the test result analysis. For example the `group` ``"Proceed to checkout"`` groups together multiple requests triggered by this action.
Checks are like asserts but they don’t stop the tests if they record a failure (for example in a load test with 1000s of iterations of a request this allows for an isolated flakey iteration to not stop test execution).
All requests have had checks for at least a `200` http status repsonse added and most also have an additional check for a string contained in the response body.
By default the built-in metrics group the HTTP requests timings so it isn't possible drill down into individual HTTP request timings. To enable seeing these individual timing in the aggregated summary report it is possible to create a custom metric for each request.
The tests name them after the requests which the metric represents.