HaveComputerWillCode.Com

Welcome!
Life is a Non-Deterministic Finite State Automata
Automation ? (*pGeekiness)++ : Code /eneration;

August 2, 2010

Downloading (streaming) files one ‘Range’ at a time using Jmeter

Filed under: Performance,Testing — Tags: , — admin @ 9:18 am

The JMX file can be downloaded here.

In one of my earlier posts, I showed how to download parts of a huge file using the Accept-Range and Range HTTP Headers with Jmeter. Whilst a starting point, what I really wanted to do was to bring down the entire file one ‘range’ at a time. By downloading smaller segments instead of the whole file, it is possible to simulate thousands of concurrent downloads from a remote server from a single Jmeter instance. And given the constant lack of extra hardware available when testing, this is ideal!

Personally, I found Jmeter somewhat ‘opaque’ when I tried doing anything clever which is why I’m making this post. In any other programming language, this task would be easy. In Jmeter, it required a bit more thought!

The Jmeter project to fetch a file range by range is here:

I won’t go through it in detail because it’s straight forward. All I do is keep tally of the number of bytes brought down so far (FirstByte) and then loop until the number of bytes to be read (RangeLength) is not equal to the content length that was actually fetched (ContentLength). At that point, I know the file has finished downloading. Each time, the ‘Get Zip File Range’ HTTP Sampler brings down the range of bytes FirstByte..(FirstByte+RangeLength-1) inclusive.

The ‘Get Zip File Range’ HTTP Sampler that actually does the useful work of fetching the correct range can be seen here:

Given it’s a fairly dull program, the only interesting part is the need to call into Javascript to evaluate the condition in the While Loop:

The rest is obvious when you see it laid out! There is a bug in it though… if ‘TotalFileLength MOD RangeLength == 0′ you’ll get one 404 Error.

Enjoy!

Powered by WordPress