IT & Engineering

Stress testing HTTP with Twisted Python and Treq

Being in the API business has its challenges and maintaining the robustness of the system during peak hours is one of them. That’s why we do lots of stress testing here at Mailgun.

PUBLISHED ON

PUBLISHED ON

Being in the API business has its challenges and maintaining the robustness of the system during peak hours is one of them. That’s why we do lots of stress testing here at Mailgun.

We have tried many different approaches over time, from simple Apache bench to more complicated custom testing suites. But this post is about a “quick and dirty” yet very flexible stress testing using Python.

When it comes to writing HTTP clients in Python we are fans of the Requests library. This is what we recommend to our API users. Requests is great, but it has one weakness: It’s a blocking one-call-per-thread affair: it’s hard or impossible to generate tens of thousands of requests quickly with it.

Introducing Treq on Twisted

To solve this problem we looked at Treq (Github repository). Treq is an HTTP client library inspired by Requests, but it runs on Twisted and it possesses the typical Twisted powers: it is asynchronous and highly concurrent when it comes to network I/O.

Treq is not specific to stress testing at all: it’s a great tool for writing highly concurrent HTTP clients in general, like web crawlers. Treq is elegant, simple to use and powerful. Here’s an example:

The simple testing script

Below is a simple script which uses Treq to bombard a single URL with maximum possible number of requests.

The output:

The “Generated” ones are the requests that have been prepared, but the Twisted reactor has not sent them yet. This script ignores all errors for simplicity, adding the stats for timeouts is left as an exercise for the reader.

The script can be used as a starting point and improved and extended with your own custom application-specific logic. One suggested improvement would be to use collections.Counter instead of the ugly globals. The script runs on a single thread, and to squeeze the maximum number of requests from a machine something like mulitprocessing can be used.

Happy stress testing!

Cheers, Mailgunners

Sign Up

It's easy to get started. And it's free.

See what you can accomplish with the world’s best email delivery platform.

Related readings

Mailgun on Performance Cloud Servers

This was posted on August 28, 2014. Until recently, Mailgun has been in a managed colo environment hosted by our parent company, Rackspace, using bare metal servers.

Read more

HTTP crash course

HTTP stands for Hypertext Transfer Protocol, and it’s a mechanism used to transfer data across the internet between clients (computers, laptops, phones, etc.) and servers.

Read more

Gubernator: Cloud-native distributed rate limiting for microservices

Today, Mailgun is excited to opensource Gubernator, a high performance distributed rate-limiting microservice. What does Gubernator do? Great question.

Read more

Popular posts

Mailgun iconSee what you can accomplish with the world's best email delivery platform. It's easy to get started.Let's get sending
CTA icon Mailgun Icon