You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Zhaoxia Yang <ya...@gmail.com> on 2012/10/29 05:47:05 UTC

Random function in for loop doesn't work as expected

Hi,

I have below script in post or pre processor of JMeter. I want the Strings are generated randomly for each loop.

for(i=0;i<5;i++)
{
  var rdStrA="${__RandomString(5,abcdefghijklmnopqrstuvwxyz)}";
  var rdStrB="${__RandomString(5,abcdefghijklmnopqrstuvwxyz)}";
 vars.put("rdStrA"+i, rdStrA);
 vars.put("rdStrB"+i, rdStrB);
}

The result is:
For the 5 times loop, rdStrAs are always same, rdStrB is different from rdStrA but 5 rdStrBs are same.

The response data of Debug Sampler shows:
...
rdStrA0=cnbyw
rdStrA1=cnbyw
rdStrA2=cnbyw
rdStrA3=cnbyw
rdStrA4=cnbyw
rdStrB0=bomad
rdStrB1=bomad
rdStrB2=bomad
rdStrB3=bomad
rdStrB4=bomad

This is really odd.
I also tried __Random(min,max) to generate random integer inside for loop, it also performs the same way.

Thanks.

Zhaoxia Yang
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
For additional commands, e-mail: user-help@jmeter.apache.org


Re: Random function in for loop doesn't work as expected

Posted by Shmuel Krakower <sh...@gmail.com>.
Hi,
I think your problem is with the fact the when using ${} it is calculated
before the actual execution of the code.
So it means that there is actually a static string in there and not a
random one.

You may import the relevant jmeter class to your script and use it directly
:
http://jmeter.apache.org/api/org/apache/jmeter/functions/RandomString.html

But I am unsure how exactly to accomplish this.


Another option is to google for java random strings which will show you
something like:

public static String generateString(Random rng, String characters, int length){
    char[] text = new char[length];
    for (int i = 0; i < length; i++)
    {
        text[i] = characters.charAt(rng.nextInt(characters.length()));
    }
    return new String(text);}


Good luck

Shmuel Krakower.
Beatsoo.org - re-use your jmeter scripts for application performance
monitoring from worldwide locations for free.



On Mon, Oct 29, 2012 at 6:47 AM, Zhaoxia Yang <ya...@gmail.com> wrote:

> Hi,
>
> I have below script in post or pre processor of JMeter. I want the Strings
> are generated randomly for each loop.
>
> for(i=0;i<5;i++)
> {
>   var rdStrA="${__RandomString(5,abcdefghijklmnopqrstuvwxyz)}";
>   var rdStrB="${__RandomString(5,abcdefghijklmnopqrstuvwxyz)}";
>  vars.put("rdStrA"+i, rdStrA);
>  vars.put("rdStrB"+i, rdStrB);
> }
>
> The result is:
> For the 5 times loop, rdStrAs are always same, rdStrB is different from
> rdStrA but 5 rdStrBs are same.
>
> The response data of Debug Sampler shows:
> ...
> rdStrA0=cnbyw
> rdStrA1=cnbyw
> rdStrA2=cnbyw
> rdStrA3=cnbyw
> rdStrA4=cnbyw
> rdStrB0=bomad
> rdStrB1=bomad
> rdStrB2=bomad
> rdStrB3=bomad
> rdStrB4=bomad
>
> This is really odd.
> I also tried __Random(min,max) to generate random integer inside for loop,
> it also performs the same way.
>
> Thanks.
>
> Zhaoxia Yang
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
> For additional commands, e-mail: user-help@jmeter.apache.org
>
>