C Programming for LoadRunner - Random value from String Array

450 views
Skip to first unread message

SAPPerformance

unread,
Jul 14, 2015, 8:54:03 AM7/14/15
to LR-Loa...@googlegroups.com
Hello,
i have the following code
 
static  String [] ip = {"Value1","Value2","Value3","Value4","Value5",Value6","Value7"};
I am trying to pass the above values randomly in a later statement and its working fine, no problem with the script, the only issue is that, the purpose here is for each virtual user to pick one of the values (value1 through value7)  randomly, however, when i load 10 virtual users, they all get the first or second value only although i have set the random as below, the values3,4,5,6,7 are never used for some reason. Any explanation or solution to why it isnt picking up the other values since its random ?

   static  Random rand = new Random();    
   static  int  i = rand.nextInt(ip.length);
 
then i pass on the value using ip [ i ]
 
Any help is appreciated. Thanks
 
 

James Pulley

unread,
Jul 14, 2015, 9:37:06 AM7/14/15
to LR-Loa...@googlegroups.com, azar...@gmail.com
Have you considered leveraging the built in unique parameter capability?   Instead of declaring an array, place these values in a file and then elect the unique parameter with update once.  This is why this capability exists in the product.   You could also use the built in random parameter pull from a file.  You will still have the issue of approximate uniformness for large data sets creeping in.  It is odd that you are placing parameters into an array and managing them directly instead of using the built in parameter capability inside of LoadRunner.

If you insist on going the hand-coded C route, The C rand() function is approximately (but not precisely) for very large data sets.  Here you have a small dataset of seven values to seven users.  There is virtually no way to guarantee uniqueness with the random number generator, particularly one which is running on seven different virtual users likely without the benefit of having set a common random seed.  What you could likely do is take a look at the virtual user number and use that as an offset into your array.  

     
int index_offset_into_array = atoi ( lr_eval_string("{Virtual user ID which is a unique number}" )  ) % 7;


~~Assuming~~ that all of your virtual users are in the same group then your virtual user IDs have a near certainly of being in sequence.   the index should return an offset of 0 to 6, or you can add one to it to get it into the rand of 1 to 7 if you like.  You should then be able to assign a single value to each user matching their remainder on the division of the virtual user ID by the number of users.  As you move onto the eighth and subsequent users you would get another traversal of the indexes as the modulus repeats the pattern of the index offsets.

A third option is to feed the values from an external queue solution, such as the virtual table server. Once "popped" from the queue then no other user can leverage that particular value.  This makes your test setup a bit more complex and you need to remember to seed the queues before each test.

James Pulley

SAPPerformance

unread,
Jul 18, 2015, 9:03:42 AM7/18/15
to LR-Loa...@googlegroups.com, azar...@gmail.com
Thanks James, we were able to incorporate the change, we had to do it a in a slightly different way, but it worked

James Pulley

unread,
Jul 20, 2015, 3:07:30 PM7/20/15
to LR-Loa...@googlegroups.com, azar...@gmail.com
When you have an opportunity please post your end resolution to allow others to benefit.
Reply all
Reply to author
Forward
0 new messages