You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Flik Shen <Fl...@infosys.com> on 2010/07/30 11:36:39 UTC

Need help for scripting

Hi,

I want to generate a random id and validate its existing in DB.
I have tried using below components but failed.
Please give me some suggestions.

 1.  BeanShell Sampler, add variable with value 1 into "vars" map by following statement.

vars.put("LoopCountNum", "1");

 1.  While Controller with following condition.

${__javaScript(${ LoopCountNum} < 2)}

 1.  JDBC Sampler with following "Select Statement" and output variable "DuplicateAmount".

SELECT COUNT('X') AS DuplicateAmount FROM table_user WHERE user_id = XXX

 1.  BeanShell Sampler update "LoopCountNum" according to previous return "DuplicateAmount".

if ("0".equals(vars.get("DuplicateAmount"))) vars.put("LoopCountNum", "2");
Item #3 and #4 are children of item #2.
This test plan is executed endlessly.

Please help me to identify the root cause.
Any scripting references or guideline preferred are also welcome.

Thanks and best regards,
Flik

BTW the version of Jmeter is 2.3.4

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


RE: Need help for scripting

Posted by "Krishnamoorthy, Rajadurai" <Ra...@sabre-holdings.com>.
Hi,
	Couple of things here. 

I am not seeing the logic to generate the random number here. 
Where are you incrementing loopCountNum?
Since it is not getting incremented you get 0 all the time and loop will
become endless.



You can generate random numbers like this.

        <BeanShellSampler guiclass="BeanShellSamplerGui"
testclass="BeanShellSampler" testname="Random Helper" enabled="true">
          <stringProp
name="BeanShellSampler.query">//print(&quot;rh&quot;);

if (vars.getObject(&quot;_random_helper_&quot;) == null) {

    import java.util.*;
    
    public class RandomHelper {
        private Random generator = new Random();
    
        public RandomHelper() {
            generator.setSeed(System.currentTimeMillis());
        }
    
 
//----------------------------------------------------------------------
--
        //  method to generate int number from given range (both: min
and max are inclusive)
 
//----------------------------------------------------------------------
--
        public int generateNumber(int min, int max) {
            int n = (min==0 ? max : max - min);
            int r = generator.nextInt(n+1);
            return r + min;
        }
    
 
//----------------------------------------------------------------------
--
        //  methods to retrieve a random value from a list
 
//----------------------------------------------------------------------
--
        public Object getListRandomValue(List list) {
            int pos = generator.nextInt(list.size()-1);
            return list.get(pos);
        }
    
    }

    vars.putObject(&quot;_random_helper_&quot;, new RandomHelper());
    print(&quot;[&quot; + Thread.currentThread().getName() +
&quot;]&quot; + &quot;Random Helper&quot;);
}

</stringProp>
          <stringProp name="BeanShellSampler.filename"></stringProp>
          <stringProp name="BeanShellSampler.parameters"></stringProp>
          <boolProp
name="BeanShellSampler.resetInterpreter">false</boolProp>
        </BeanShellSampler>


Thanks,
Raja


-----Original Message-----
From: Flik Shen [mailto:Flik_Shen@infosys.com] 
Sent: Friday, July 30, 2010 3:07 PM
To: JMeter Users List
Subject: Need help for scripting

Hi,

I want to generate a random id and validate its existing in DB.
I have tried using below components but failed.
Please give me some suggestions.

 1.  BeanShell Sampler, add variable with value 1 into "vars" map by
following statement.

vars.put("LoopCountNum", "1");

 1.  While Controller with following condition.

${__javaScript(${ LoopCountNum} < 2)}

 1.  JDBC Sampler with following "Select Statement" and output variable
"DuplicateAmount".

SELECT COUNT('X') AS DuplicateAmount FROM table_user WHERE user_id = XXX

 1.  BeanShell Sampler update "LoopCountNum" according to previous
return "DuplicateAmount".

if ("0".equals(vars.get("DuplicateAmount"))) vars.put("LoopCountNum",
"2");
Item #3 and #4 are children of item #2.
This test plan is executed endlessly.

Please help me to identify the root cause.
Any scripting references or guideline preferred are also welcome.

Thanks and best regards,
Flik

BTW the version of Jmeter is 2.3.4

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


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