You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by "Drach, Noa" <No...@ca.com> on 2013/06/13 14:54:47 UTC

Sharing logic between thread groups

Hello all,

In my test I have 5 thread groups - I was wondering if it is possible to share common logic between the thread groups.

Basically all of my tests have user id and organization id in the headers.
For each thread that is running I am randomly selecting a user id in the range of 1-50 and organization id in the range of 1-100.

All this is achieved by using a Bean Shell Preprocessor that is attached to each of the samplers.

For each of the samplers this random editing of the header might also be accompanied by some editing of the url as a result of the random user ID.

By attaching a preprocessor to each sample I manage to do all this - but I was wondering if there is some way to define some utility methods that I can call from each Bean Shell processor and avoid repeating the logic.

For example:
...
HeaderManager hm = sampler.getHeaderManager();
callMySetHeaders(hm);
// Do some specific code

I know that it is possible to have custom functions - but my understanding is that they only return a string - I am asking if there is a way to write more complex code.

Thanks,
Noa Drach


Re: Sharing logic between thread groups

Posted by sebb <se...@gmail.com>.
On 16 June 2013 17:47, Drach, Noa <No...@ca.com> wrote:
> Thanks,
> It looks like using beanshell.preprocessor.init is the direction for what I was looking for.
>
> I have one more question as a result:
> If I put the preprocessor as a child of an http request everything works OK, but if I try to put the preprocessor as a sibling of the sampler or as a sibling of the HTTP Header Manager I manage to get the results from my functions(I verified that the requests are sent with different values in the headers) and at the same time I get in each invocation:
>
> ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval       Sourced file: inline evaluation of: ``getTenant(Integer.parseInt(vars.get("NUM_OF_TENANTS"))); getPersonId(50);        . . . '' : Typed variable declaration : Error in method invocation: Method getHeaderManager() not found in class'org.apache.jmeter.sampler.TestAction'
> WARN  - jmeter.modifiers.BeanShellPreProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval    Sourced file: inline evaluation of: ``getTenant(Integer.parseInt(vars.get("NUM_OF_TENANTS"))); getPersonId(50);        . . . '' : Typed variable declaration : Error in method invocation: Method getHeaderManager() not found in class'org.apache.jmeter.sampler.TestAction'
>
> This structure gets the results correctly without errors and warnings:
> Test Plan
>         HTTP Header Manager
>         My Thread group
>                 HTTP Request Sampler
>                         Bean shell preprocessor
>
> This structure produces errors while getting the results correctly:
> Test Plan
>         HTTP Header Manager
>         My Thread group
>                 Bean shell preprocessor
>                 HTTP Request Sampler

Are there any other samplers in scope?
The error message suggests that there is a TestAction somewhere. That
does not have a Header Manager.
Your script will need to check the sampler type first.

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

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


RE: Sharing logic between thread groups

Posted by "Drach, Noa" <No...@ca.com>.
Thanks,
It looks like using beanshell.preprocessor.init is the direction for what I was looking for.

I have one more question as a result:
If I put the preprocessor as a child of an http request everything works OK, but if I try to put the preprocessor as a sibling of the sampler or as a sibling of the HTTP Header Manager I manage to get the results from my functions(I verified that the requests are sent with different values in the headers) and at the same time I get in each invocation:

ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval	Sourced file: inline evaluation of: ``getTenant(Integer.parseInt(vars.get("NUM_OF_TENANTS"))); getPersonId(50);        . . . '' : Typed variable declaration : Error in method invocation: Method getHeaderManager() not found in class'org.apache.jmeter.sampler.TestAction' 
WARN  - jmeter.modifiers.BeanShellPreProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval	Sourced file: inline evaluation of: ``getTenant(Integer.parseInt(vars.get("NUM_OF_TENANTS"))); getPersonId(50);        . . . '' : Typed variable declaration : Error in method invocation: Method getHeaderManager() not found in class'org.apache.jmeter.sampler.TestAction'

This structure gets the results correctly without errors and warnings:
Test Plan
	HTTP Header Manager
	My Thread group
		HTTP Request Sampler
			Bean shell preprocessor

This structure produces errors while getting the results correctly:
Test Plan
	HTTP Header Manager
	My Thread group
		Bean shell preprocessor
		HTTP Request Sampler

Thanks,
Noa

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


Re: Sharing logic between thread groups

Posted by sebb <se...@gmail.com>.
On 13 June 2013 13:54, Drach, Noa <No...@ca.com> wrote:
> Hello all,
>
> In my test I have 5 thread groups - I was wondering if it is possible to share common logic between the thread groups.
>
> Basically all of my tests have user id and organization id in the headers.
> For each thread that is running I am randomly selecting a user id in the range of 1-50 and organization id in the range of 1-100.
>
> All this is achieved by using a Bean Shell Preprocessor that is attached to each of the samplers.

You can use a single PreProcessor at the top level of each Thread
Group and it will apply to all the samples.

> For each of the samplers this random editing of the header might also be accompanied by some editing of the url as a result of the random user ID.
>
> By attaching a preprocessor to each sample I manage to do all this - but I was wondering if there is some way to define some utility methods that I can call from each Bean Shell processor and avoid repeating the logic.

You can define functions in a BeanShell startup script and call them
from the pre-processor.
Have a look at the sample .bshrc files in the bin directory.

Or just put the script in a file which can be re-used by multiple
pre-processors.

> For example:
> ...
> HeaderManager hm = sampler.getHeaderManager();
> callMySetHeaders(hm);
> // Do some specific code
>
> I know that it is possible to have custom functions - but my understanding is that they only return a string - I am asking if there is a way to write more complex code.
>
> Thanks,
> Noa Drach
>

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


RE: Sharing logic between thread groups

Posted by "Drach, Noa" <No...@ca.com>.
Thanks,

Can you give me some keyword to search for the previous discussions you mentioned?

Thanks,
Noa

-----Original Message-----
From: Adrian Speteanu [mailto:asp.adieu@gmail.com] 
Sent: Thursday, June 13, 2013 18:20
To: JMeter Users List
Subject: Re: Sharing logic between thread groups

You can add a header manager to use the same header for all your requests.
Use the header manager in a parent of all the samples that need to be in its scope. For those requests that need to over-write it, use the HM as child to those samplers.

For the general question: can logic be shared between threads? It has been discussed recently, check those discussions first, maybe you find some useful tips there, or share with us what those threads don't cover.

Cheers,
Adrian

On Thu, Jun 13, 2013 at 3:54 PM, Drach, Noa <No...@ca.com> wrote:

> Hello all,
>
> In my test I have 5 thread groups - I was wondering if it is possible 
> to share common logic between the thread groups.
>
> Basically all of my tests have user id and organization id in the headers.
> For each thread that is running I am randomly selecting a user id in 
> the range of 1-50 and organization id in the range of 1-100.
>
> All this is achieved by using a Bean Shell Preprocessor that is 
> attached to each of the samplers.
>
> For each of the samplers this random editing of the header might also 
> be accompanied by some editing of the url as a result of the random user ID.
>
> By attaching a preprocessor to each sample I manage to do all this - 
> but I was wondering if there is some way to define some utility 
> methods that I can call from each Bean Shell processor and avoid repeating the logic.
>
> For example:
> ...
> HeaderManager hm = sampler.getHeaderManager(); callMySetHeaders(hm); 
> // Do some specific code
>
> I know that it is possible to have custom functions - but my 
> understanding is that they only return a string - I am asking if there 
> is a way to write more complex code.
>
> Thanks,
> Noa Drach
>
>

Re: Sharing logic between thread groups

Posted by Adrian Speteanu <as...@gmail.com>.
You can add a header manager to use the same header for all your requests.
Use the header manager in a parent of all the samples that need to be in
its scope. For those requests that need to over-write it, use the HM as
child to those samplers.

For the general question: can logic be shared between threads? It has been
discussed recently, check those discussions first, maybe you find some
useful tips there, or share with us what those threads don't cover.

Cheers,
Adrian

On Thu, Jun 13, 2013 at 3:54 PM, Drach, Noa <No...@ca.com> wrote:

> Hello all,
>
> In my test I have 5 thread groups - I was wondering if it is possible to
> share common logic between the thread groups.
>
> Basically all of my tests have user id and organization id in the headers.
> For each thread that is running I am randomly selecting a user id in the
> range of 1-50 and organization id in the range of 1-100.
>
> All this is achieved by using a Bean Shell Preprocessor that is attached
> to each of the samplers.
>
> For each of the samplers this random editing of the header might also be
> accompanied by some editing of the url as a result of the random user ID.
>
> By attaching a preprocessor to each sample I manage to do all this - but I
> was wondering if there is some way to define some utility methods that I
> can call from each Bean Shell processor and avoid repeating the logic.
>
> For example:
> ...
> HeaderManager hm = sampler.getHeaderManager();
> callMySetHeaders(hm);
> // Do some specific code
>
> I know that it is possible to have custom functions - but my understanding
> is that they only return a string - I am asking if there is a way to write
> more complex code.
>
> Thanks,
> Noa Drach
>
>