Automated Load Testing with TeamCity

Make load testing a first class citizen in your automated testing.

Getting started

We believe in goal oriented and automated load testing. That's why we built k6 to work well in such environments, integrating nicely with tools like TeamCity, continuous integration and delivery server from JetBrains.

This guide will help you get up and running with k6, TeamCity and LoadImpact Insights. Content on this page assumes you are familiar with k6 and TeamCity.

It also assumes you have a LoadImpact account. If not, go get one here – it’s free. Once you have your account setup, try this sample of how to include load testing in your TeamCity setup.

TeamCity

The load test

It starts with code. This is the load test example script we'll be using in this guide. It tests our example site test.loadimpact.com:

k6 load test script

To describe the load test script in words:

  1. The test will ramp up from 0 to 10 users over 60s, stay flat at 10 users for another 60s, before ramping down to 0 users again over 60s
  2. We've set a goal that the 95th percentile response time should be below 500ms. This step, k6 thresholds, is essential, it's how we define our goals which is the basis for the pass/fail mechanism to work that is built into k6. If a threshold fails, k6 will end with a non-zero exit code, which in turn indicates a failed build step to TeamCity (and other CI tools).
  3. In the load test "main" function we define a group Front page inside which we make a request, check the response status code and sleep for 10s before letting the user loop from the top of the function.

Configuration

Next we created a TeamCity project by the name k6_demo which has one build configuration, k6_build.

A TeamCity k6 build

And here is the history when we have executed a couple of times.

Executed TeamCity builds with k6

So slightly more interesting – let’s take a look at the settings of the k6 configuration.

Configuration of TeamCity build for k6

Everything is plain default, no specific settings needed to run a k6 load test.

There is only one build step needed to run the load test. It works fine on both *nix and Windows.

TeamCity k6 build step

The Build Step in TeamCity is a command line build step.

Only one requisite is needed and that is k6 needs to be in the PATH of the build agent.

There is an old TeamCity caveat and that needs to be handled. Even if it is only a single command line you should select to Run a custom script which only contains a single command line to run k6. If not it will add a duplicate set of double quotes to the entire command which means it will not execute.

This is the entire custom script needed.
k6 run --quiet -o cloud github.com/loadimpact/k6-circleci-example/loadtests/main.js

Integrate with LoadImpact Insights

Before we dive into the details – let's get some essentials from your LoadImpact account. We need the LoadImpact API key so you can analyze the results in the LoadImpact Insights cloud service. The LoadImpact API key you get in your LoadImpact account when you are logged in.

LoadImpact profile

Go to your profile, i.e. click your name at the top left and then select “Insider program”. Then copy if from the Manage API token text box underlined in red. The key itself has been masked in the sample below.

LoadImpact Insights API key

Now you have an API key for your account to store the results from the test

Before running, make sure k6 is installed in the executable PATH of your build node. Install instructions can be found in the k6 documentation.

Environment variables

k6 will pick up the LoadImpact API key from the environment variable K6CLOUD_TOKEN so it has to be in the environment when executed. This is set in the TeamCity build settings where you add a new parameter by that name, K6CLOUD_TOKEN.

TeamCity build config environment variables for k6 load test results upload to Insights

Execute

Now let's execute a build. Once started it will look something like the below.

TeamCity build running with k6 load test

And looking into the details of the build log itself we see the output from k6 as it's running.

TeamCity build log output with k6 summary results

If you expand to see the details of the build log there's also a direct link to the full results and analysis in LoadImpact Insights where you can always find all the results of all your tests.

TeamCity build log output with LoadImpact Insights link

If the load test is successful all is fine. If it fails it will exit with a non-zero exit code and thus fail the build step as well.

Result analysis

A passing build is not much fun to look at. But, when builds fail, you want to understand why. No exception when it's a load test failing the build. We've built Insights to be a perfect companion to k6 for analysing results! The analysis workflow is error driven, meaning the goal is to help you quickly finding the cause of the failed load test. Read more on Insights detailed functionality on this and adjacent pages in our Knowledge Base.

TeamCity build with k6 and result analysis with LoadImpact Insights

That's it, happy automated testing!