you're reading...
Load Testing

How to load test Ruby on Rails Applications with JMeter

RoR version 2.3.5, JMeter version 2.4

There are a few specifics using JMeter with RoR applications.

1.Handling forms
We have to take into account that forms are submitted using authenticity_token which is generated randomly with every form. The solution is to catch the authenticity token from the form page using a regular expression extractor, save it into a JMeter variable and send it with the form.

This article provides a detailed explanation how to do it. I only used \s+ instead of empty spaces to make it work.

2. We have to pay attention how the authenticity_token field in represented on the page in order to parse it right. You can look up this field if you enter your HTTP request for the page with this form into JMeter and open a View Results Tree listener (Add -> Listener -> View Results Tree). Run your test, click on the request and open the Response data tab in text format. Perform search in it for “authenticity_token” (you might have several on the page, if you have several forms).

For example, the field in my regular forms looked like:

<input name="authenticity_token" type="hidden" value="[actual value]" />

correspondingly, my regular expression for JMeter was:


I also have a few AJAX forms that are shown on demand and stored as Javascript variables. In those cases I had all the quotes slash escaped, and the field looked like:

<input name=\"authenticity_token\" type=\"hidden\" value=\"[actual value]\" />

so my regular expression became (the escape slashes get escaped in their turn):


3. Handling AJAX requests
I figured out how to pass parameters into AJAX requests by running those requests in FireFox and watching what parameters are passed. For example, I had a few requests with “update” method, and Rails routing defines it as performed over “PUT”. However, you have to enter it as a “POST” method, add a parameter “_method” to the request parameters and assign “put” to it. Bizarre?

Also, with the Rails routing and remote forms you have just a regular path, no .js in the end. I was able to see this path when running FireBug. In this case you need to add the right headers to the request by running “Add -> Config Element -> HTTP Header Manager”. I added the following headers to my requests handling remote forms:

Accept = text/javascript, text/html, application/xml, text/xml, */
X-Requested-With = XMLHttpRequest
Content-Type = application/x-www-form-urlencoded; charset=UTF-8

4. Getting object ids
Sometimes after creating an object we need this object’s id assigned by Rails in order to access this object (it might be needed in a request path etc.). We can extract this object id from a response page if it contains links with this id, using Regular Expression Extractor. The regular expression will depend on a particular case.

5. We can assign Regular Expression Extractor results to variables and use them in other Regular Expression Extractors. Unfortunately, JMeter has a bug when it’s not able to read a variable value when it’s used in a Regular Expression Extractor for AJAX responses (if the response is regular, it’s fine). So, don’t struggle for no reason, it’s not going to work! Use something else. I tried to pass a variable for an object id to a regular expression when parsing an AJAX response and ended up just using \d+ . I luckily had a unique tag for this expression.

6. Uploading Images
JMeter has a problem with Rails and multipart forms, that’s why it doesn’t work if there is a form with an image field in it. Rails doesn’t set content-type for text fields of multipart forms, so JMeter sets it up by default and as a result the text field content doesn’t come right and the submission fails. I had to hack JMeter code according to this post and then building it from source. To build it from source, run the following commands in the JMeter folder:

ant download_jars
ant package

After rebuilding JMeter, it worked fine to upload images.

7. Number of Matches for Regular Expression Extractor
Sometimes we need to see if some parameter is on the page, sometimes we need to make sure that the number of occurrences of this parameter is within certain limits. In this case we need to figure out the number of matches instead of a match itself. The number of matches is represented by the regular expression VARNAME_matchNr. The group number should be set to -1. Note, they we can get EITHER a match number, or a certain match itself, but not both.
To check if there is a form on the page (i.e. there is an authenticity token), I used the following parameters in the Regular Expression Extractor:

Reference Name = TEST
Regular Expression = (<input\s+name="authenticity_token"\s+type="hidden"\s+value="(.*?)"\s*\/>)
Template = $1$
Match No. = -1
Default Value = null

Then the number of matches is TEST_matchNr. I couldn’t get it working without a default value.

8. JMeter seems to have trouble using VARNAME_matchNr in the If Controller, when I wanted to check if there is a form on the page. It simply didn’t see it. I had to create another variable to make it working. I used another Regular Expression Extractor to get the value of TEST_matchNr, where I specified ‘Apply To – JMeter Variable – TEXT_matchNr. The other parameters were the following:

Reference Name = TEST_NUM
Regular Expression = (\d+)
Template = $1$
Match No. = 1

Then I used the variable TEST_NUM in the If Controller:

Condition = ${TEST_NUM} > 0

About RailsBlogger

I'm a Software Developer with over 10 years of experience, Java and Ruby on Rails.


No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ip tracing
%d bloggers like this: