Data Seeding Scripts

by Community Manager on 07-06-2011 02:50 PM - last edited on 07-12-2011 07:20 AM

This topic presents a variety of SOASTA CloudTest data seeding scripts. Scripts can be copied and pasted directly into the SOASTA CloudTest Script Editor using Central > Scripts. Longer scripts have an accompanying image that can be clicked to pop out the full example s ript. Shorter scripts are presented inline (in the right column).

 
  1. Script 1: Single-element data array, choose one randomly

The Single-element data array, choose one randomly script creates an array and then chooses a random value from that array. See also: Using Dynamic Data > Array Files.

  1. First, declare and format the array:

var oStrings =
[
"PG3032901074056",
"PG3052321074056",
"PG3062641074056",
"PG3082651074056",
"PG3182821075056",
];

  1. Next, choose an element from the array based on a random number.

var a = oStrings[Math.floor(oStrings.length * (Math.random()))];

  1. Set a clip property to contents of “a” – the randomly selected index value.

$prop.set("MessageClip", "PropertyName", a);

Show debug information in the results.

$context.result.postMessage($context.result.LEVEL_INFO, "PropertyName: " + a);

  1. Script 2: Single-element data array, choose six randomly

This example script creates a single-element array, choosing six different elements from array.


Lines 1-8 create and format an array:

var oStrings =
[
"PG3032901074056",
"PG3052321074056",
"PG3062641074056",
"PG3082651074056",
"PG3182821075056",
];

Next, lines 10-15 create six variables, a-f, to store six random numbers:

var a = oStrings[Math.floor(oStrings.length * (Math.random()))];
var b = oStrings[Math.floor(oStrings.length * (Math.random()))];
var c = oStrings[Math.floor(oStrings.length * (Math.random()))];
var d = oStrings[Math.floor(oStrings.length * (Math.random()))];
var e = oStrings[Math.floor(oStrings.length * (Math.random()))];
var f = oStrings[Math.floor(oStrings.length * (Math.random()))];

Next, lines 16-21 populate clip properties with the random numbers by variable.

$prop.set("MessageClip", "prop1", a);
$prop.set("MessageClip", "prop2", b);
$prop.set("MessageClip", "prop3", c);
$prop.set("MessageClip", "prop4", d);
$prop.set("MessageClip", "prop5", e);
$prop.set("MessageClip", "prop6", f);

Lines 22-27 set debug messages for each variable.

$context.result.postMessage($context.result.LEVEL_INFO, "ecard: " + a);
$context.result.postMessage($context.result.LEVEL_INFO, "ecard: " + b);
$context.result.postMessage($context.result.LEVEL_INFO, "ecard: " + c);
$context.result.postMessage($context.result.LEVEL_INFO, "ecard: " + d);
$context.result.postMessage($context.result.LEVEL_INFO, "ecard: " + e);
$context.result.postMessage($context.result.LEVEL_INFO, "ecard: " + f);

  1. Script 3: Single-element data array, select unique values

The Single-element data array, select unique values script shows how a script can be used to ensure that only a single value from an array is used by a clip/composition. It takes into account track and clip indexes to make sure that only unique values are used in the test. If the serial repeats or the number of virtual users exceeds the number defined in the script, the composition will abort.

Notes: There are two important restrictions: (1) There must be enough values to handle the number of Virtual Users times the maximum number of repeats of the Clip within each Virtual User.  (2) The script must be set ahead of time to specify the maximum number of repeats of the Clip within each Virtual User (see the variable maxClipRepeats).


  1. Script 4: Single-element data array, select unique values – second example

The incrementing is done based on track and clip indexes. This is necessary when each serial repeat of a clip needs a different element in the array. This example is different because it substitutes in zeros (0) instead of aborting the composition.


  1. Script 5: Two-element data array, choose one record randomly

The Two-element data array, choose one record randomly script is run at the beginning of the clip. It creates two track properties behind the scenes (i.e. the track properties are not created in the composition editor). It loads each element of the array into its own property. This type of script is used when the first element of the array is related to the second element – as in username/password combinations.


  1. First, define the set of data from which to select (user ID and password).

var testData =
[
["lindab462@aol.com","4690C47DADADA88BF8F4C6A2F262798A"],
["dancehottie90@msn.com","109A25E92070491C83C2ED3ABE96CACE"],
["Cortneyjboyd@gmail.com","FF94B25791A80AF1C5E11FAF66038226"],
["Gakouri@suffolk.edu","C7561DB7A418DD39B2201DFE110AB4A4"],
["tomshea9@hotmail.com","60FB1504776106EAEC25522A6CF4D9C8"],
["gonavy90@hotmail.com","DCFC756608AC5C7578C59E306CDEF7A9"],
["boyki1vm@cmich.edu","FCE9CD59CCEAA9290829200F27D150FB"],
["swalker@aps4kids.org","A5410EE37744C574BA5790034EA08F79"],
["nkechiyere@aol.com","F222791CC077C629D9357F13D2143F82"],
["chxyu@ucsc.edu","6A1759F184BCA1DA10F19441390EAA34"],
["bgammill@drury.edu","754968094C842A07B663962196A776EC"],
];

  1. Generate a random index into the test data.

var randomIndex = Math.min(Math.round(Math.random() * (testData.length - 1)), testData.length - 1);

    1. Create and populate custom properties for this instance of this Track.

var propertyList = $context.currentTrack.propertyList;
if ($context.currentClipIndex == 0)
propertyList.createProperty("email");
propertyList.setPropertyValue("email", testData[randomIndex][0]);
if ($context.currentClipIndex == 0)
propertyList.createProperty("password");
propertyList.setPropertyValue("password", testData[randomIndex][1]);

  1. Script 6: Two-element data array, increment through values

The Two-element data array, increment through values script is run at the beginning of each clip. It loads each element of the array into its own clip property based on the track index (virtual user number). The user selected in this script is used throughout the track – it doesn’t change on the same track (currentTrackIndex).


If you want to change the user for each repeat of the test clip, use currentClipIndex instead.

Note: Ensure that you have enough data in the script to handle the number of virtual users and serial repeats of the test clip.

var testData =
[
["mariopaleka","mp7777777"],
["nbavpaulz","lakers4"],
["chitownlegend","mj23chicago"],
["laurencetan8","loverboy"],
["dfalkson","77447744"],
];

var a = testData[$context.currentTrackIndex];

$prop.set("MessageClip", "user", a[0]);
$prop.set("MessageClip", "pass", a[1]);

  1. Script 7: Seed data from URL

The Seed Data from URL script randomly selects one of the lines from a list and puts that value into a clip property named DataListValue.

This example assumes there is just a single element on each row in the CSV file. Meaning, it is a simple list of values to be used in the test. The data would look like this in the file:

Username1
Username2
Username3


In line 1, declare a variable using context.readFromURL.

var dataList = $context.readFromURL("http://www.myhost.com/directory/file.csv");

Make a variable, a, for the increment.

var a = dataList[Math.floor(dataList.length * (Math.random()))];

To iterate through the list (instead of random), use these options:

  1. var a = dataList[$context.currentClipIndex]; (different users in a track)
  2. var a = dataList[$context.currentTrackIndex]; (each track gets a different user)

$prop.set("MessageClip", "DataListValue", a);
$context.result.postMessage($context.result.LEVEL_INFO, "Data List Value: " + a);
$context.result.postMessage($context.result.LEVEL_INFO, "Data List Length: " + dataList.length);

This functionality can be expanded to extract data from multiple comma-separated values on the same line. Here are additional examples. If the file contained the following lines:

San Francisco,CA,94103
Timbuktu
Caribou,Aroostook County,Maine,USA
Hill Valley,CA,91905

The array of strings on the right would be returned:

 

dataList[0][0] San Francisco
dataList[0][1] CA
dataList[0][2] 94103

dataList[1] Timbuktu

dataList[2][0] Caribou
dataList[2][1] Aroostook County
dataList[2][2] Maine
dataList[2][3] USA

dataList[3][0] Hill Valley
dataList[3][1] CA
dataList[3][2] 91905