Jul 082012
 

After having written the afore-discussed Flask web service, I needed a framework to load test the whole thing. I wound up using multi-mechanize. Overall, it’s a pretty decent framework. It’s pretty easily configurable, the documentation is thorough enough for most needs, and presents the test data in a variety of formats. Projects are quick and easy to set up, leaving you to focus on your test scripts. Test scripts are pretty easy to wrap your head around. Create a class called Transaction, write a run() method, and make sure any test code you need run gets called from that run() method. You can spin up as many processes and/or threads as you need to accurately test your application with a few simple tweaks of the configuration. There are a few simple tips I’ve picked up writing this test script.

First and foremost, while you can use urllib and urllib2 with multi-mechanize, if you’re using session data in your application, you need to use mechanize’s Browser object. This lets you recycle the same object for all your service calls, thus preserving all of your oh-so-necessary session data (like say….that you logged into the application).

Next up, if you have a web service method that can only be accessed via POST, you’ll want to use the mechanize.Browser.open() method to open with a URL that is GET-friendly and doesn’t have any security restrictions (like your index page, for example). I didn’t have such a page, and as a result, I had to add a dummy GET segment to my method in order to get the test to run without giving me lip about not being authorized (really I should have just added an index page that accepts GET and doesn’t require you to be logged in, but adding some dummy “it’s OK if you GOT” code to my POST method was quicker, until I had more time to sit down and do cleanup on the code).

Lastly, does your application return JSON? Did you write up a  shiny little test script for multi-mechanize thinking you were going to do some good for your application only to beat your head against the desk wondering why you were getting errors about not viewing HTML (even though you told your client you were sending it JSON)? If so, that’s because multi-mechanize has no idea what to do with you. Luckily, there’s a simple fix, once you’re willing to go digging about in the source code. Go to the mechanize directory in wherever Python eggs are installed (like /usr/local/lib/python2.7/dist-packages in Linux), and open up the _headers.py file. Around line 43, you’ll find a line that looks like this:

html_types = ["text/html"]

and just append “application/json” to the list:

html_types = ["text/html", "application/json"]

With the exception of having to dig around source code to allow JSON, overall multi-mechanize is a pretty good, simple load testing framework that I recommend to anyone. Thanks to it being open-source,  issues like my needing to allow JSON responses are more of a timesink than they are actual problems. Now, get out there and beat the hell out of your web applications!

 Posted by at 12:00 PM