Wednesday, May 30, 2012

Random Text in a Test Plan

The tests that I work on need random text.  While Apache JMeter has the __RandomString function for populating form data, it wasn't really what I wanted.  I was looking for readable text in the posted data.  In addition to making the test data more realistic, it also exercises the search index in my employer's application in a more realistic way.  

The approach I came up with is to read a news feed at the start of each thread, and extract the subjects and descriptions at the beginning of each thread.  Once that's done, a random subject and description can be selected for each iteration.

There are three pieces to setting this up:
  1. An HTTP Request to pull the feed XML
  2. Regular Expression Extrators to pull all of the headline data
  3. A BSF Sampler to randomly set the subject and description variables to one of the extracted headlines
Additional Comments:
  • Some of the RSS feeds I've tested were failing mid-test in some of my testing.  I attributed this to rejections from the RSS feed if I made too many requests.  Watch out for that if your test runs a lot of threads.
  • Ideally, the test would hit the RSS feed only once, and then share the strings with all threads.  I was unable to successfully share a variable among all threads, so I worked around it by spreading threads across multiple RSS feeds.

Here are the details:

feed.jmx  (download a plan containing the following)

Type:  HTTP Requeset
Name:  get AP feed
Server Name or IP:  hosted.ap.org
Port: 80
URLlineups/USHEADS-rss_2.0.xml?SITE=VTBUR&SECTION=HOME

Type:  Regular Expression Extractor
Name:  get subjects
Apply To:  Main Sample Only
Response Field to check:  Body (unescaped)
Reference Name:  subjects
Regular Expression:  <title>(.*?)</title>
Template:  $1$
Match No.:  -1
Default Value:  no feed data

Type:  Regular Expression Extractor
Name:  get descriptions
Apply To:  Main Sample Only
Response Field to check:  Body (unescaped)
Reference Name:  descriptions
Regular Expression:  <description>(.*?)</description>
Template:  $1$
Match No.:  -1
Default Value:  no feed data

Type:  BSF Sampler
Name:  pick subject & description
Language:  javascript
Script:

pickData('subject');
pickData('description');

function pickData(won) {
    var wonVal = 'no feed data';
    var opts = won + 's';
    var optsCount = vars.get(opts + "_matchNr");

    if (optsCount >= 3){
        var pickVar = opts + "_" + randomFromTo(3, optsCount);
        wonVal = vars.get(pickVar);
    }
    vars.put(won, wonVal);
    return wonVal;
}

function randomFromTo(from, to) {
    var picked = Math.floor(Math.random() * (to - from + 1) + from) + '';
    return picked;
}

Wednesday, May 23, 2012

Launching JMeter - on a mac

To launch Apache JMeter on OSX, I use a short script that runs from a terminal window.  (I use TextExpander to run the commands, but that's just because I'm lazy).  This will set a 2GB JVM and put me in the directory with my plans so that opening them is easier.

Wrapping this in a clickable launcher would be cleaner, but this way, I can easily see if I hit an OutOfMemory error, and I can CTRL-C to kill it if the interface locks up.

Script:
JVM_ARGS="-Xms2g -Xmx2g"
export JVM_ARGS
jmpath='/Applications/jmeter_2.12'
jmexec=${jmpath}/bin/jmeter
cd ~/Documents/jmplans/
chmod +x ${jmexec}
${jmexec}

Tuesday, May 22, 2012

JMeter Variables - picking a random value

To randomize form submission data in Jmeter, I've put together a sampler that selects a random element from a hard-coded list of options.  The result is a variable named nextAction that can be used later in the plan.  I've needed this for population of a field that has a defined set of expected values at the server.

Type:  BSF Sampler
Name:  set nextAction
Language:  javascript
Script:

var opts = ['create','comment','update','close'];
var opt = opts[Math.floor(Math.random() * opts.length)];

vars.put("nextAction",opt);
opt