From 2363294f082e040eb1da4eca09973144a50f3adc Mon Sep 17 00:00:00 2001 From: Tam Mullen Date: Fri, 24 Sep 2021 10:34:37 +0100 Subject: [PATCH] Added more detail on influxdb and grafana to k6 readme --- tests/performance/README.md | 67 ++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/tests/performance/README.md b/tests/performance/README.md index 120c43d2c0b..fa523fa2024 100644 --- a/tests/performance/README.md +++ b/tests/performance/README.md @@ -30,8 +30,10 @@ Automated k6 performance tests for WooCommerce. To be used for benchmarking perf - [Custom Metrics](#custom-metrics) - [Other Resources](#other-resources) +--- +--- ## Pre-requisites - +--- ### Install k6 To install k6 on macOS using [Homebrew](https://brew.sh/) @@ -40,22 +42,25 @@ To install k6 on macOS using [Homebrew](https://brew.sh/) [For other platforms please see the k6 installation guide.](https://k6.io/docs/getting-started/installation/) -Alternatively you can use the k6 docker image to execute tests. +Alternatively the k6 docker image can be used to execute tests. `docker pull loadimpact/k6` +--- +--- ## Configuration - +--- ### Test Environment Before using the tests a test environment is needed to run against. -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. +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 to just see the tests in action. `npx wc-e2e docker:up ./tests/e2e/docker/init-sample-products.sh` If using a different environment the details can be changed in `config.js`. +--- ### Config Variables `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. @@ -83,10 +88,14 @@ add_product_regular_price | regular price of product to be added in merchant add think_time_min | minimum sleep time (in seconds) between each request | no think_time_max | maximum sleep time (in seconds) between each request | no + +--- +--- ## Running Tests 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. +--- ### Running Indvidual Tests To execute an individual test file (for example `requests/shopper/shop-page.js`) containing requests. @@ -97,6 +106,7 @@ Docker `docker run --network="host" -v /[YOUR LOCAL WC DIRECTORY FULL PATH]/requ This will run the individual test for 1 iteration. +--- ### Running Scenarios 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. @@ -118,26 +128,67 @@ 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/) +For example these steps can be used to install and setup InfluxDB and Grafana locally. + +Install InfluxDB + +`brew install influxdb@1` + +Run InfluxDB + +`brew services start influxdb@1` + +InfluxDB can now be accessed at http://localhost:8086/ + +Add this option to the `k6 run` command to send results to the InfluxDB instance into a database named `myk6db`. If this database does not exist, k6 will create it automatically. + +`--out influxdb=http://localhost:8086/myk6db` + +Install Grafana (this command uses 6.7.4 for compatibility with the imported dashboard mentioned below). + +`curl -O https://dl.grafana.com/oss/release/grafana-6.7.4.darwin-amd64.tar.gz` + +`tar -zxvf grafana-6.7.4.darwin-amd64.tar.gz` + +Run Grafana + +`cd grafana-6.7.4` + +`./bin/grafana-server web` + +Grafana can now be accessed at http://localhost:3000/ + +Create a custom Grafana dashboard using [these instructions](https://k6.io/docs/results-visualization/influxdb-+-grafana/#custom-grafana-dashboard) or import [this dashboard](https://grafana.com/grafana/dashboards/2587) using [these instructions](https://grafana.com/docs/grafana/v7.5/dashboards/export-import/#importing-a-dashboard). + +--- +--- ## Writing Tests +--- ### Capturing Requests 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. @@ -148,6 +199,7 @@ Alternatively any application which captures HTTP requests can be used to figure Tests could also be created to simulate API requests. +--- ### Static Assets The tests only simulate the HTTP requests and do not include static assets requests (fonts, images, css, js etc). @@ -158,6 +210,7 @@ Although static resources do affect the bandwidth and the overall response time In additional any use of CDNs, cache settings, and themes used would also vary from site to site. +--- ### Request Headers Every HTTP requests tested includes the headers. @@ -165,6 +218,7 @@ Every HTTP requests tested includes the headers. 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. +--- ### Correlation 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. @@ -173,22 +227,27 @@ An example of this is the `woocommerce-process-checkout-nonce` . This is returne 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 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 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. +--- ### Custom Metrics 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. +--- +--- ## Other Resources [k6 documention](https://k6.io/docs/) is a very useful resource for test creation and execution.