We recently needed to load test a new web application, and just could not get Jmeter to do anything useful. As an alternative, I tried using Locust. After a couple of hours, and fewer than 60 lines of code, I had a (mostly) working test.
If you have a need to load test anything, and even a minimal understanding of Python, I highly recommend looking into Locust. Here’s a brief overview of how it works:
The official documentation has a good basic example, so I won’t repeat it here. At a minimum, you simply need to define two classes:
Locust
TaskSet
A task set is just a group of one or more tasks you’d like your simulated users to perform during the test. Tasks can be hard-coded, or created and added to the set on-the-fly.
While this venture was successful overall, there were a few small annoyances I should mention.
Locust will gather and display stats as it’s creating users, but as soon as it reaches your requested number, all the stats are reset and it starts gathering new ones. If you want to see how many users it takes to cause problems on your site, you need to sit and stare at the web interface as it ramps up or that information will be gone.
Locust won’t request a URL unless you explicitly tell it to. A legitimate user with an actual web browser will most likely end up requesting several URLs when loading a page (for images, style sheets, and scripts). Keep this in mind when building your tests. If you want to simulate an actual “page view”, you’ll need to explicitly list all the URLs for various resources, or try to automate this with something like BeautifulSoup.
For example, this will attempt to extract the URL from any tag with a src
attribute:
resource_urls = set() soup = BeautifulSoup(response.text) for res in soup.find_all(src=True): url = res['src'] resource_urls.add(url)
The good news is that if you use the TaskSet
class’s built-in client to request a URL, it will automatically be included in the stats.
That is, you can’t tell it how many hits each user should simulate. If you want a specific number, you’ll need to just stare at the web interface and stop the test when the total gets close to what you want.
Overall, Locust is a great tool to quickly test running applications under load. Another tool we like for automated unit testing is Jenkins. You can read our article on Jenkins, Humans vs. Robots: Testing for Quality.