How to get started load testing your API from a template

Posted by Load Impact on Feb 27, 2018

It’s important to perform API Performance Testing. In this article, we’ll review our simple strategy for getting started and give you a template or sample script for your JSON API.

Before we get started, however, it’s important to make sure we’re using the same words to mean the same things. LoadImpact load tests are generally sized by virtual users, or VUs. Those VUs simulate actual users and actual user behavior to help test your site or app’s resiliency.

But an API isn’t used by a human; it’s accessed by another program. Thus, for an API, we measure things like requests per second (RPS) to gauge response time. Watching how your response time changes as you increase the RPS to your API is the most straightforward place to start.

Before we talk more strategy, let’s get you right into a sample script that tests HTTP/JSON API calls. We’ll explain a little about the script after the sample.

local username = "xxxxx"
local password = "yyyyyyy"

local data, headers, response

data = json.stringify({email=username, password=password})
headers = {
   ["Content-Type"] = "application/json"
}
response = http.request({"POST",
       "https://api.xxxx.com/login",
       headers = headers,
       data = data,
       response_body_bytes=1000000})

if response.status_code ~= 200 then
 log.error('login API error')
 do return end
end

-- read response value
local token = json.parse(response.body).token.key

headers = {
 ["Content-Type"] = "application/json",
 ["Authorization"] = "Token "..token
}
response = http.request({"GET",
 "https://api.xxxx.com/v3/account",
 headers = headers,
 response_body_bytes=1000000})

if response.status_code ~= 200 then
 log.error('fetching API error')
 do return end
end

log.debug(response.body)
data = json.parse(response.body)
--process data

client.sleep(math.random(20, 40))

You can see in the sample that you’ll need to customize it to work with your local test server login - and to use your actual API calls. A word of warning -- even if you have an ecommerce site, be sure you’re load testing only your own APIs. Payment APIs in particular, even with test logins, don’t take it well when you load test their APIs. That’s their job to do. However, if you have external API calls as part of a start-to-finish load test, check with your external API providers before you do.

load_graph_baseline.png

In any case, this sample code gives you the simple framework for your own API test script. We recommend that you start your API testing with a baseline test. Let’s review our recommendations for how to set your API testing at the right levels, since LoadImpact’s default test configuration value is VUs, not RPS.

We generally estimate 25 RPS per VU. Since we have to configure LoadImpact in VUs, and we want RPS for our API testing, we just divide our desired RPS by 25 to arrive at the right VU setting. For example, if you wanted to be able to accommodate 100,000 simultaneous requests per second, you could set your load tests at 4000 VUs. (100,000 / 25 = 4000.)

Run your baseline test to see how your API performs under that load and adjust accordingly. Then, run this same test regularly to see if API code changes affect performance. But other changes can affect it, too, like external API calls, database changes and even server configuration. (Don’t forget to load test consistently and continually during the development process. See why we think this is important.) 

Once you’ve run a baseline test, you may want to see just how much your API can take, slowly increasing the VU setting (remembering each VU corresponds to 25 RPS) to see just what it takes to stress your API to the point performance isn’t acceptable.

This should give you a good start on your API performance testing.

Happy testing!

Topics: API integration load testing, Load Testing, Performance testing, baseline test

Recent Posts

Popular posts

Posts by Topic

see all

Subscribe to Email Updates