Archive

Posts Tagged ‘load testing’

Multipart posting with Apache Benchmark

March 21st, 2011 5 comments

Last week I wanted to load test an upload functionality created for one of the projects. The testing team was busy with other stuff so I decided to do it on my own. Being a hardcore programmer and someone who has never used any of the regular testing tools (read M$ Window$ based tools) I had the only option of using Apache Benchmark on my Ubuntu 10.10

Since I had already used ab (the Apache Benchmark command name), I was pretty confident that within few minutes I will be done. But multipart form posting wasn’t as straightforward as I had thought. My initial assumption was to provide a file path to -p option of ab and it would handle the stuff required for multipart posting. Unfortunately that wasn’t the case. I realized that I had to provide a file name but it should contain the complete information about the data to be posted. In other words, I had to manage the boundary required for multipart posting.

After some research and this small but important tip, I managed to prepare the POST data in the required format along with the correct Content-type required by Apache Benchmark. The final command looked like

ab -n 10 -c 2 -C PHPSESSID=rk53j7gsrmaiuc3gvo86ipltr1 -p /var/www/post_data.txt -T "multipart/form-data; boundary=1234567890" http://my-domain.com/upload.php

Following is the breakdown of options provided to the command –

I provided the cookie information (option -C ) along with the command since my upload script checks for authentication.
-p allows me to provide a file name which contains the complete information about the data to be POSTed along with multipart boundaries.
-T is for Content-type header. This is where I also tell ab about the boundary in my POST data along with the standard multipart/form-data content type.
And then finally the URL of where all the data has to be posted.

The contents of the post_data.txt file are

--1234567890
Content-Disposition: form-data; name="ID"

3
--1234567890
Content-Disposition: form-data; name="videofile"; filename="ab1_pod.avi"
Content-Type: video/x-msvideo

[base64 encoded file content here]
--1234567890--

Remember that the format of the file should be exactly the same (your boundary label can be different than mine though). Even if you miss a single new line or add an extra new line somewhere then you won’t get the expected results.

Finally to base64 encode the file to be posted, you can simply use PHP code as follows and paste the content in the above placeholder.

echo base64_encode(file_get_contents('/home/aditya/Videos/my_video.avi'));

That’s it. Happy testing.
As always, comments and suggestions are most welcome.