{"id":21582670,"url":"https://github.com/automation-test-starter/k6-performance-test-starter","last_synced_at":"2025-03-18T08:24:34.948Z","repository":{"id":215413521,"uuid":"738832795","full_name":"Automation-Test-Starter/K6-Performance-Test-starter","owner":"Automation-Test-Starter","description":"An introductory document on using K6 for performance testing.","archived":false,"fork":false,"pushed_at":"2024-01-11T08:45:26.000Z","size":41,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-24T15:08:38.801Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Automation-Test-Starter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2024-01-04T06:35:24.000Z","updated_at":"2024-11-02T12:33:02.000Z","dependencies_parsed_at":"2024-01-05T10:42:15.770Z","dependency_job_id":null,"html_url":"https://github.com/Automation-Test-Starter/K6-Performance-Test-starter","commit_stats":null,"previous_names":["automation-test-starter/k6-performance-test-starter"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automation-Test-Starter%2FK6-Performance-Test-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automation-Test-Starter%2FK6-Performance-Test-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automation-Test-Starter%2FK6-Performance-Test-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automation-Test-Starter%2FK6-Performance-Test-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Automation-Test-Starter","download_url":"https://codeload.github.com/Automation-Test-Starter/K6-Performance-Test-starter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244182217,"owners_count":20411799,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T14:16:32.260Z","updated_at":"2025-03-18T08:24:34.925Z","avatar_url":"https://github.com/Automation-Test-Starter.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- markdownlint-disable MD041 --\u003e\n\u003c!-- markdownlint-disable MD033 --\u003e\n\u003cdiv align=\"right\"\u003e\u003cstrong\u003e\u003ca href=\"./README_ZH.md\"\u003e🇨🇳中文\u003c/a\u003e\u003c/strong\u003e  | \u003cstrong\u003e🇬🇧English\u003c/strong\u003e\u003c/div\u003e\n\u003c!-- markdownlint-disable MD041 --\u003e\n\u003c!-- markdownlint-disable MD033 --\u003e\n\n# K6-Performance-Test-starter\n\nA quick-start introductory document on performance testing with K6.\n\n- [K6-Performance-Test-starter](#k6-performance-test-starter)\n  - [Introduction of K6](#introduction-of-k6)\n  - [Official website and documentation](#official-website-and-documentation)\n  - [Installation](#installation)\n    - [Installation on Mac systems](#installation-on-mac-systems)\n    - [Windows installation](#windows-installation)\n    - [Docker installation](#docker-installation)\n    - [Installation on other systems](#installation-on-other-systems)\n    - [Confirming a successful K6 installation](#confirming-a-successful-k6-installation)\n  - [First k6 test script](#first-k6-test-script)\n    - [Write the first test script](#write-the-first-test-script)\n      - [Create a new K6 performance testing project directory and go to](#create-a-new-k6-performance-testing-project-directory-and-go-to)\n      - [Create a file named `demo.js` for writing test scripts](#create-a-file-named-demojs-for-writing-test-scripts)\n      - [Editing Test Scripts](#editing-test-scripts)\n      - [Running the Test Script](#running-the-test-script)\n      - [Check the test results](#check-the-test-results)\n      - [Parsing demo test script](#parsing-demo-test-script)\n  - [K6 common function](#k6-common-function)\n    - [HTTP Requests](#http-requests)\n      - [GET Request Example](#get-request-example)\n      - [POST Request Example](#post-request-example)\n      - [Supported HTTP Methods](#supported-http-methods)\n      - [HTTP Request Tags](#http-request-tags)\n    - [Metrics](#metrics)\n      - [K6 Built-in Metrics](#k6-built-in-metrics)\n        - [Standard Built-in Metrics](#standard-built-in-metrics)\n        - [HTTP-specific built-in metrics](#http-specific-built-in-metrics)\n        - [Other built-in metrics](#other-built-in-metrics)\n      - [custom metrics](#custom-metrics)\n        - [custom metrics demo](#custom-metrics-demo)\n          - [1.Import the Trend constructor from the k6/metrics module](#1import-the-trend-constructor-from-the-k6metrics-module)\n          - [2.Constructs a new custom metric Trend object in the init context](#2constructs-a-new-custom-metric-trend-object-in-the-init-context)\n          - [3.Use the add method in a script to record metric measurements](#3use-the-add-method-in-a-script-to-record-metric-measurements)\n          - [4.demo\\_custom\\_metrics.js Complete code of custom metric](#4demo_custom_metricsjs-complete-code-of-custom-metric)\n          - [5.Run demo\\_custom\\_metrics.js and view automated trending metrics](#5run-demo_custom_metricsjs-and-view-automated-trending-metrics)\n    - [Checks](#checks)\n      - [1. Check HTTP Response Status](#1-check-http-response-status)\n      - [2. Check HTTP Response Body](#2-check-http-response-body)\n      - [3. Adding Multiple Checks](#3-adding-multiple-checks)\n    - [Thresholds](#thresholds)\n  - [References](#references)\n\n## Introduction of K6\n\nk6 is an open source tool for performance testing and load testing, primarily used to evaluate and validate the performance and stability of applications. Here are some key features and information about k6:\n\n1. **Open Source:** k6 is a completely open source performance testing tool with code stored on GitHub. This means that users are free to access, use and modify the tool's source code.\n\n2. **JavaScript scripting:** k6 uses the JavaScript language to write test scripts, which makes writing test cases relatively easy and more developer-friendly. Scripts can contain HTTP requests, WebSocket connections, script execution logic, and more.\n\n3. **Support for multiple protocols:** k6 supports a variety of common protocols, including HTTP, WebSocket, Socket.IO, gRPC and so on, so it can be widely used in various types of applications. 4.\n\n4. **Distributed Testing:** k6 has distributed testing capabilities, allowing tests to be run on multiple nodes to simulate a more realistic production environment load.\n\n5. **Real-time results and reports:** k6 provides real-time results, including request response time, throughput, etc., and is able to generate detailed HTML reports to help users better understand the performance status of their applications.\n\n6. **Containerization Support:** k6 adapts to containerized environments, can be easily integrated into CI/CD pipelines, and works with common container orchestration tools such as Kubernetes.\n\n7. **Plugin ecosystem:** k6 supports plugins that allow users to extend its functionality to meet specific needs.\n\n8. **Active Community:** Since k6 is an open source project, there is an active community that provides support, documentation, and examples to make it easier for users to get started and solve problems.\n\nOverall, k6 is a flexible, powerful and easy-to-use performance testing tool for applications and systems of all sizes.\n\n## Official website and documentation\n\n- [Official website](https://k6.io/)\n- [Official Documentation](https://k6.io/docs/)\n\n## Installation\n\n### Installation on Mac systems\n\nMac systems can install k6 via Homebrew:\n\n```bash\nbrew install k6\n```\n\n### Windows installation\n\nWindows systems can install k6 via Chocolatey:\n\n```bash\nchoco install k6\n```\n\nOr you can install k6 via winget:\n\n```bash\nwinget install k6\n```\n\n### Docker installation\n\nk6 can also be installed via Docker:\n\n```bash\ndocker pull grafana/k6\n```\n\n### Installation on other systems\n\nIn addition to the above systems, K6 also supports Linux (Debian/Ubuntu/Fedora/CentOS), and can be installed by downloading the K6 binaries and K6 extensions, please refer to the [official documentation](https://k6.io/docs/get-started/ For details on how to install K6, please refer to the official documentation ().\n\n### Confirming a successful K6 installation\n\nAfter the installation is complete, you can confirm that K6 has been installed successfully by using the following command:\n\n```bash\nk6 version\n```\n\nIf the installation was successful, the k6 version information will be displayed:\n\n![ ](https://cdn.jsdelivr.net/gh/naodeng/blogimg@master/uPic/QR8wKb.png)\n\n## First k6 test script\n\n### Write the first test script\n\n#### Create a new K6 performance testing project directory and go to\n\n```bash\nmkdir k6-demo\ncd k6-demo\n```\n\n#### Create a file named `demo.js` for writing test scripts\n\n- A test script file can be created with the `k6 new` command:\n\n```bash\nk6 new demo.js\n```\n\n- You can also create a test script file called demo.js directly\n\n```bash\ntouch demo.js\n```\n\n#### Editing Test Scripts\n\nIf the test script file is created with the `k6 new` command, a simple test script is automatically generated as shown below:\n\n```javascript\nimport http from 'k6/http';\nimport { sleep } from 'k6';\n\nexport const options = {\n  // A number specifying the number of VUs to run concurrently.\n  vus: 10,\n  // A string specifying the total duration of the test run.\n  duration: '30s',\n\n  // The following section contains configuration options for execution of this\n  // test script in Grafana Cloud.\n  //\n  // See https://grafana.com/docs/grafana-cloud/k6/get-started/run-cloud-tests-from-the-cli/\n  // to learn about authoring and running k6 test scripts in Grafana k6 Cloud.\n  //\n  // ext: {\n  //   loadimpact: {\n  //     // The ID of the project to which the test is assigned in the k6 Cloud UI.\n  //     // By default tests are executed in default project.\n  //     projectID: \"\",\n  //     // The name of the test in the k6 Cloud UI.\n  //     // Test runs with the same name will be grouped.\n  //     name: \"demo.js\"\n  //   }\n  // },\n\n  // Uncomment this section to enable the use of Browser API in your tests.\n  //\n  // See https://grafana.com/docs/k6/latest/using-k6-browser/running-browser-tests/ to learn more\n  // about using Browser API in your test scripts.\n  //\n  // scenarios: {\n  //   // The scenario name appears in the result summary, tags, and so on.\n  //   // You can give the scenario any name, as long as each name in the script is unique.\n  //   ui: {\n  //     // Executor is a mandatory parameter for browser-based tests.\n  //     // Shared iterations in this case tells k6 to reuse VUs to execute iterations.\n  //     //\n  //     // See https://grafana.com/docs/k6/latest/using-k6/scenarios/executors/ for other executor types.\n  //     executor: 'shared-iterations',\n  //     options: {\n  //       browser: {\n  //         // This is a mandatory parameter that instructs k6 to launch and\n  //         // connect to a chromium-based browser, and use it to run UI-based\n  //         // tests.\n  //         type: 'chromium',\n  //       },\n  //     },\n  //   },\n  // }\n};\n\n// The function that defines VU logic.\n//\n// See https://grafana.com/docs/k6/latest/examples/get-started-with-k6/ to learn more\n// about authoring k6 scripts.\n//\nexport default function() {\n  http.get('https://test.k6.io');\n  sleep(1);\n}\n```\n\nIf the test script file was created directly, you can copy the above into the `demo.js` file.\n\n#### Running the Test Script\n\nIn the directory where the `demo.js` file is located, run the following command:\n\n```bash\nk6 run demo.js\n```\n\n#### Check the test results\n\nIf all is well, you will see output similar to the following:\n\n![ ](https://cdn.jsdelivr.net/gh/naodeng/blogimg@master/uPic/a4vK69.png)\n\nContains the following information:\n\n- **execution:** execution information, including start time, end time, duration, number of VUs, number of iterations, etc.\n- **scenarios:** Scenario information, including scenario name, number of VUs, number of iterations, duration, average response time, throughput, and so on.\n- **http_reqs:** HTTP request information, including request name, number of requests, number of failures, average response time, throughput, and so on.\n\n#### Parsing demo test script\n\n- `import http from 'k6/http';`: import k6's HTTP module, used to send HTTP request.\n\n- `import { sleep } from 'k6';`: Import k6's sleep method to wait for script execution.\n\n- `export const options = { ... }`: Define the configuration items of the test script, including the number of VUs, duration, etc.\n\n- `vus: 10,`: define the number of VUs to be 10 (specify the number of VUs running concurrently).\n\n- `duration: '30s',`: define the duration as 30 seconds (specify the total duration of the test run).\n\n- `export default function() { ... }`: defines the logic of the test script, including sending HTTP requests, executing waits, and so on.\n\n- `http.get('https://test.k6.io');`: send a GET request to `https://test.k6.io`.\n\n- `sleep(1);`: wait 1 second for execution.\n\n\u003e The other comments can be ignored, they are about some advanced features of k6, which will be introduced later.\n\n## K6 common function\n\n### HTTP Requests\n\nThe first step in performance testing with K6 is to define the HTTP requests to be tested.\n\n#### GET Request Example\n\nA simple HTTP request for the GET method is already included in the demo test script created with the `k6 new` command:\n\n```javascript\nimport http from 'k6/http';\nimport { sleep } from 'k6';\n\nexport default function() {\n  http.get('https://test.k6.io');\n  sleep(1);\n}\n```\n\n#### POST Request Example\n\nThis POST request example shows the application of some complex scenarios (POST request with email/password authentication load)\n\n```javascript\nimport http from 'k6/http';\n\nexport default function () {\n  const url = 'http://test.k6.io/login';\n  const payload = JSON.stringify({\n    email: 'aaa',\n    password: 'bbb',\n  });\n\n  const params = {\n    headers: {\n      'Content-Type': 'application/json',\n    },\n  };\n\n  http.post(url, payload, params);\n}\n\n```\n\n\u003e The above is taken from [K6 Official Documentation](https://k6.io/docs/using-k6/http-requests)\n\n#### Supported HTTP Methods\n\nThe HTTP module provided by K6 can handle various HTTP requests and methods. The following is a list of supported HTTP methods:\n\n| NAME | VALUE |\n| ------- | ------- |\n| batch() | makes multiple HTTP requests in parallel (like e.g. browsers tend to do).|\n| del() | makes an HTTP DELETE request.|\n| get() | makes an HTTP GET request.|\n| head()|  makes an HTTP HEAD request.|\n| options()|  makes an HTTP OPTIONS request.|\n| patch()|  makes an HTTP PATCH request.|\n| post()|  makes an HTTP POST request.|\n| put() | makes an HTTP PUT request.|\n| request() | makes any type of HTTP request.|\n\n#### HTTP Request Tags\n\nK6 allows you to add tags to each HTTP request. Combining tags and grouping makes it easy to better organize in test results, group requests and filter results to organize analysis.\n\nThe following is a list of supported tags:\n\n| NAME | DESCRIPTION |\n| ------- | ------- |\n| expected_response | By default, response statuses between 200 and 399 are true. Change the default behavior with `setResponseCallback`.|\n| group | When the request runs inside a `group`, the tag value is the group name. Default is empty.|\n| name | Defaults to URL requested|\n| method | Request method (GET, POST, PUT etc.)|\n| scenario|  When the request runs inside a `scenario`, the tag value is the scenario name. Default is `default`.|\n| status|  response status|\n| url | defaults to URL requested|\n\nExamples of HTTP requests using tag and group tags will be shown in subsequent demos.\n\nYou can also refer to the official examples:[https://grafana.com/docs/k6/latest/using-k6/http-requests/](https://grafana.com/docs/k6/latest/using-k6/http-requests/)\n\n### Metrics\n\nThe metrics are used to measure the performance of the system under test conditions. By default, k6 automatically collects built-in metrics. In addition to the built-in metrics, you can create custom metrics.\n\nMetrics generally fall into four categories:\n\n1. Counters: Summing values.\n2. Gauges: Tracking the smallest, largest, and most recent values.\n3. Rates: Tracking how often non-zero values occur.\n4. Trends: Calculating statistical information (such as mean, mode, or percentile) for multiple values.\n\nTo ensure that test assertions meet the criteria, thresholds can be written based on the conditions of the metrics required by the performance test (the specifics of the expression depend on the type of metric).\n\nFor subsequent filtering of metrics, labels and groupings can be used, allowing for better organization of test results.\n\n\u003e The test results output file can export metrics in a variety of summary and fine-grained formats. For more information, refer to the results output documentation. (This section will be covered in more detail in the later part of the test results output documentation.)\n\n#### K6 Built-in Metrics\n\nEvery k6 test execution emits both built-in and custom metrics. Each supported protocol also has its specific metrics.\n\n##### Standard Built-in Metrics\n\nRegardless of the protocol used in the test, k6 always collects the following metrics:\n\n| Metric Name  | Type | Description |\n| ------- | ------- | -------|\n|vus |Gauge |Current number of active virtual users|\n|vus_max| Gauge| Max possible number of virtual users (VU resources are pre-allocated, to avoid affecting performance when scaling up load )|\n|iterations |Counter| The aggregate number of times the VUs execute the JS script (the default function).|\n|iteration_duration |Trend |The time to complete one full iteration, including time spent in setup and teardown. To calculate the duration of the iteration’s function for the specific scenario, try this workaround|\n|dropped_iterations| Counter| The number of iterations that weren’t started due to lack of VUs (for the arrival-rate executors) or lack of time (expired maxDuration in the iteration-based executors). About dropped iterations|\n|data_received| Counter| The amount of received data. This example covers how to track data for an individual URL.|\n|data_sent |Counter| The amount of data sent. Track data for an individual URL to track data for an individual URL.|\n|checks| Rate |The rate of successful checks.|\n\n##### HTTP-specific built-in metrics\n\nHTTP-specific built-in metrics are generated only when the test makes HTTP requests.Other types of requests, such as WebSocket, do not generate these metrics.\n\n\u003e Note: For all http_req_* metrics, the timestamp is emitted at the end of the request. In other words, the timestamp occurs when k6 receives the end of the response body or when the request times out.\n\nThe following table lists HTTP-specific built-in metrics:\n\n| Metric Name  | Type | Description |\n| ------- | ------- | -------|\n|http_reqs |Counter| How many total HTTP requests k6 generated.|\n|http_req_blocked |Trend| Time spent blocked (waiting for a free TCP connection slot) before initiating the request. float|\n|http_req_connecting |Trend |Time spent establishing TCP connection to the remote host. float|\n|http_req_tls_handshaking| Trend| Time spent handshaking TLS session with remote host|\n|http_req_sending |Trend| Time spent sending data to the remote host. float|\n|http_req_waiting |Trend| Time spent waiting for response from remote host (a.k.a. “time to first byte”, or “TTFB”). float|\n|http_req_receiving |Trend| Time spent receiving response data from the remote host. float|\n|http_req_duration |Trend| Total time for the request. It’s equal to http_req_sending + http_req_waiting + http_req_receiving (i.e. how long did the remote server take to process the request and respond, without the initial DNS lookup/connection times). float|\n|http_req_failed |Rate| The rate of failed requests according to setResponseCallback.|\n\n##### Other built-in metrics\n\nIn addition to the standard built-in metrics and HTTP-specific built-in metrics, K6 built-in metrics also have other built-in metrics:\n\n- Browser metrics : \u003chttps://grafana.com/docs/k6/latest/using-k6/metrics/reference/#browser\u003e\n- Built-in WebSocket metrics : \u003chttps://grafana.com/docs/k6/latest/using-k6/metrics/reference/#websockets\u003e\n- Built-in gRPC metrics : \u003chttps://grafana.com/docs/k6/latest/using-k6/metrics/reference/#grpc\u003e\n\n#### custom metrics\n\nBesides the built-in metrics, you can create custom metrics. For example, you can compute a metric for your business logic, or use the Response.timings object to create a metric for a specific set of endpoints.\n\nEach metric type has a constructor to create a custom metric. The constructor creates a metric object of the declared type. Each type has an add method to take metric measurements.\n\n\u003e Note: Custom metrics must be created in the init context. This limits memory and ensures that K6 can verify that all thresholds evaluate the defined metrics.\n\n##### custom metrics demo\n\nThe following example demonstrates how to create a custom trend metrics for wait time:\n\n\u003e The demo_custom_metrics.js file in the project file already contains this demo example, which can be run directly to view the results.\n\n###### 1.Import the Trend constructor from the k6/metrics module\n\n```javascript\nimport { Trend } from 'k6/metrics';\n```\n\n\u003e \u003e The waiting time trend metrics is a Trends metrics, so the Trend constructor needs to be introduced from the k6/metrics module.\n\n###### 2.Constructs a new custom metric Trend object in the init context\n\n```javascript\nconst myTrend = new Trend('waiting_time');\n```\n\n\u003e Construct a new custom metric Trend object in the init context, the object in the script is myTrend, and its metric is displayed as `waiting_time` in the resulting output.\n\n###### 3.Use the add method in a script to record metric measurements\n\n```javascript\nexport default function() {\n  const res = http.get('https://test.k6.io');\n  myTrend.add(res.timings.waiting);\n}\n```\n\n\u003e Use the add method in the script to record the metric measurement values. Here, `res.timings.waiting` is used, which is the waiting time.\n\n###### 4.demo_custom_metrics.js Complete code of custom metric\n\n```javascript\nimport http from 'k6/http';\nimport { Trend } from 'k6/metrics';\n\nconst myTrend = new Trend('waiting_time');\n\nexport default function () {\n  const res = http.get('https://httpbin.test.k6.io');\n  myTrend.add(res.timings.waiting);\n  console.log(myTrend.name); // waiting_time\n}\n```\n\n###### 5.Run demo_custom_metrics.js and view automated trending metrics\n\n```bash\nk6 run demo_custom_metrics.js\n```\n\nThe running results are as follows:\n\n![ ](https://cdn.jsdelivr.net/gh/naodeng/blogimg@master/uPic/4tbqVc.png)\n\n\u003e As you can see, the custom metric `waiting_time` has been displayed in the result output.\n\nFor more information about custom metrics, please refer to the official documentation: [https://k6.io/docs/using-k6/metrics/#custom-metrics](https://k6.io/docs/using-k6/metrics/#custom-metrics)\n\n### Checks\n\n\u003e This can also be understood as assertions, which verify the test results.\n\nChecks are used to verify whether specific test conditions in different tests are correctly responded to, similar to how we conventionally verify test results in other types of tests to ensure that the system responds as expected.\n\nFor example, a check can ensure that the response status of a POST request is 201, or that the size of the response body matches expectations.\n\nChecks are similar to the concept of assertions in many testing frameworks, but **K6 does not abort the test or end it in a failed state when verifications fail. Instead, k6 tracks the failure rate of failed verifications as the test continues to run**.\n\n\u003e Each check creates a rate metric. To make a check abort or cause the test to fail, it can be combined with thresholds.\n\nBelow, we will introduce how to use different types of checks and how to view check results in test results.\n\n#### 1. Check HTTP Response Status\n\nK6 checks are particularly useful for response assertions related to HTTP requests.\n\nFor example, the following code snippet checks that the HTTP response code is 200:\n\n```javascript\nimport { check } from 'k6';\nimport http from 'k6/http';\n\nexport default function () {\n  const res = http.get('https://httpbin.test.k6.io');\n  check(res, {\n    'HTTP response code is status 200': (r) =\u003e r.status === 200,\n  });\n}\n```\n\nRunning this script, you can see the following results:\n\n![ ](https://cdn.jsdelivr.net/gh/naodeng/blogimg@master/uPic/aTXnpy.png)\n\n\u003e When a script contains checks, the summary report shows how many test checks have passed.\n\nIn this example, note that the check \"HTTP response code is status 200\" is 100% successful when called.\n\n#### 2. Check HTTP Response Body\n\nIn addition to checking the HTTP response status, you can also check the HTTP response body.\n\nFor example, the following code snippet checks that the HTTP response body size is 9591 bytes:\n\n```javascript\nimport { check } from 'k6';\nimport http from 'k6/http';\n\nexport default function () {\n  const res = http.get('https://httpbin.test.k6.io');\n  check(res, {\n    'HTTP response body size is 9591 bytes': (r) =\u003e r.body.length == 9591,\n  });\n}\n```\n\nRunning this script, you can see the following results:\n\n![ ](https://cdn.jsdelivr.net/gh/naodeng/blogimg@master/uPic/AmbL0E.png)\n\n\u003e When a script contains checks, the summary report shows how many test checks have passed.\n\nIn this example, note that the check \"HTTP response body size is 9591 bytes\" is 100% successful when called.\n\n#### 3. Adding Multiple Checks\n\nSometimes, multiple checks need to be added in a single test script. You can directly add multiple checks in a single check() statement, as shown in the script below:\n\n```javascript\nimport { check } from 'k6';\nimport http from 'k6/http';\n\nexport default function () {\n  const res = http.get('https://httpbin.test.k6.io');\n  check(res, {\n    'HTTP response code is status 200': (r) =\u003e r.status === 200,\n    'HTTP response body size is 9591 bytes': (r) =\u003e r.body.length == 9591,\n  });\n}\n```\n\nRunning this script, you can see the following results:\n\n![ ](https://cdn.jsdelivr.net/gh/naodeng/blogimg@master/uPic/5yJyBw.png)\n\nIn this example, both checks pass successfully (the call is 100% successful).\n\n\u003e Note: When a check fails, the script will continue to execute successfully and will not return a \"failed\" exit status. If you need to fail the entire test based on check results, you must combine checks with thresholds. This is particularly useful in specific environments, such as integrating k6 into a CI pipeline or receiving alerts when scheduling performance tests.\n\n### Thresholds\n\n## References\n\n- [Official K6 documentation: https://k6.io/docs/](https://k6.io/docs/)\n- [Official website: https://k6.io/](https://k6.io/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fautomation-test-starter%2Fk6-performance-test-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fautomation-test-starter%2Fk6-performance-test-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fautomation-test-starter%2Fk6-performance-test-starter/lists"}