Monday, April 7, 2014

Array should be simple in performance testing


   










    Recently I saw a question in Stackoverflow on array indexing on the Jmeter test platform.  Logically speaking it should be fairly simple,  especially for a developer who has 677 reputation points.  However, in practice it was not the case when using Jmeter.

   To paraphrase the challenge,  the author just needs to index into an array called "STU_IDsub" using a variable called "index".  The syntax needed to get it right on JMeter is even beyond the skills of  a seasoned developer.

    The following is a code snippet that shows how easy it actually is on the NetGend platform .  For the sake of completeness,  we will emulate a full test scenario:  a user pulling down a list from inventory and randomly picking an item to view.

1
2
3
4
5
STU_IDsub = substring(http.reply, 'inventoryId="', '"');
index     = randNumber(0, length(STU_IDsub) -1);
id        = STU_IDsub[index];


  • Line 1:  Do a HTTP transaction to get the inventory.
  • Line 2:  Extract the inventroyIDs by specifying a left boundary and a right boundary.
  • Line 3:  Get a random index in the range. Note the indexing is 0 based.
  • Line 4:  Use the random index from previous step and get the value of the entry (inventory id)
  • Line 5:  Use the inventory id to do a HTTP transaction to view the detail of that inventory.

    In NetGend’s simple approach Line 4 has the answer to the array indexing question.  In this contrived example, we get the desired inventory id in two steps:  1) use randNumber() to get a random index and 2) index into the array to get the value.   NetGend platform also provides a convenience function randElement().   In the following code snippet, we use one line instead of two - see the line with bold font.

1
2
3
4
STU_IDsub = substring(http.reply, 'inventoryId="', '"');
id        = randElement(STU_IDsub);




    Another good use of array is to simulate users picking a few inventories to view.  one common implementation is as follows:  set up a loop, then, in each step of the loop, randomly pick an element.  Although this implementation is straightforward it doesn't work because the simulated user may pick the same items in the loop due to the randomness.

    Again, NetGend has a simple solution thanks to the function "randSubset()" which will return a subset of the elements in the array. User can then loop through the subset to do the desired HTTP transactions.

1
2
3
4
5
6
STU_IDsub = substring(http.reply, 'inventoryId="', '"');
ids       = randSubset(STU_IDsub);
for (i=0; i < length(ids); i++) {
    action(http, "http://www.example.com/viewInventory/${ids[i]}");
}

    In real world, there will be many complex performance test scenarios involving the logic of array, here at NetGend, we are confident that we will have the simplest solutions to these complex testing challenges.

No comments:

Post a Comment