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
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