With handleSummary(), you can completely customize your end-of-test summary. In this document, read about:
- How handleSummary() works
- How to customize the content and output location of your summary
- The data structure of the summary object
handleSummary() is available only for local tests.
However, we plan to support the feature for k6 Cloud tests, too. Track progress in this issue.
About handleSummary()
After your test runs, k6 aggregates your metrics into a JavaScript object. The handleSummary() function takes this object as an argument (called data in all examples here).
You can use handleSummary() to create a custom summary or return the default summary object. To get an idea of what the data looks like, run this script and open the output file, summary.json.
Fundamentally, handleSummary() is just a function that can access a data object. As such, you can transform the summary data into any text format: JSON, HTML, console, XML, and so on. You can pipe your custom summary to standard output or standard error, write it to a file, or send it to a remote server.
k6 calls handleSummary() at the end of the test lifecycle.
Use handleSummary()
The following sections go over the handleSummary() syntax and provide some examples.
To look up the structure of the summary object, refer to the reference section.
Syntax
k6 expects handleSummary() to return a {key1: value1, key2: value2, ...} map that represents the summary metrics.
The keys must be strings. They determine where k6 displays or saves the content:
- stdout for standard output
- stderr for standard error,
- any relative or absolute path to a file on the system (this operation overwrites existing files)
The value of a key can have a type of either string or ArrayBuffer.
You can return multiple summary outputs in a script. As an example, this return statement sends a report to standard output and writes the data object to a JSON file.
Example: extract data properties
This minimal handleSummary() extracts the median value for the iteration_duration metric and prints it to standard output:
Example: modify default output
If handleSummary() is exported, k6 does not print the default summary. However, if you want to keep the default output, you could import textSummary from the K6 JS utilities library. For example, you could write a custom HTML report to a file, and use the textSummary() function to print the default report to the console.
You can also use textSummary() to make minor modifications to the default end-of-test summary. To do so:
- Modify the data object however you want.
- In your return statement, pass the modified object as an argument to the textSummary() function.
The textSummary() function comes with a few options:
Option | Description |
---|---|
indent | How to start the summary indentation |
enableColor | Whether to print the summary in color. |
For example, this handleSummary() modifies the default summary in the following ways:
- It deletes the http_req_duration{expected_response:true} sub-metric.
- It deletes all metrics whose key starts with iteration.
- It begins each line with the → character.
In the collapsible, you can use the tabs to compare default and modified reports.
To see the output of the preceding script, select Modified. For compactness, these outputs were limited with the summaryTrendStats option.
Example: make custom file format
This script imports a helper function to turn the summary into a JUnit XML. The output is a short XML file that reports whether the test thresholds failed.
Output for a test that crosses a threshold looks something like this:
Example: send data to remote server
You can also send the generated reports to a remote server (over any protocol that k6 supports).
noteThe last examples use imported helper functions. These functions might change, so keep an eye on jslib.k6.io for the latest.
Of course, we always welcome PRs to the jslib, too!
Summary data reference
Summary data includes information about your test run time and all built-in and custom metrics (including checks).
All metrics are in a top-level metrics object. In this object, each metric has an object whose key is the name of the metric. For example, if your handleSummary() argument is called data, the function can access the object about the http_req_duration metric at data.metrics.http_req_duration.
Metric schema
The following table describes the schema for the metrics object. The specific values depend on the metric type:
Property | Description |
---|---|
type | String that gives the metric type |
contains | String that describes the data |
values | Object with the summary metric values (properties differ for each metric type) |
thresholds | Object with info about the thresholds for the metric (if applicable) |
noteIf you change the default trend metrics with the summaryTrendStats option, the keys for the values of the trend will change accordingly.
Example summary JSON
To see what the summary data looks like in your specific test run:
Add this to your handleSummary() function:
Inspect the resulting raw-data.json file.
The following is an abridged example of how it might look:
Custom output examples
These examples are community contributions. We thank everyone who has shared!