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;
}

No comments:

Post a Comment