You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Sam Drew <sa...@forgerock.com> on 2014/07/01 09:22:19 UTC

Custom Configuration Plugin

I’m trying to write a custom JMeter module, that will run a Java method that I use for Server Setup and returns Map<String,String> as configuration properties that can be used in sampling.

At the moment I am slightly unsure of what TestElement I should be extending or implementing in order to achieve this, given that it seems to sit somewhere between a PreProcessor and ConfigTestElement, in that it is both setting up pre-conditions for the test and then providing these as configuration parameters. 

After looking at the example in jmeter-plugins for csv config element I wrote a class which was little more than

class CreateMyServerConfig extends Arguments {
    @Override
    public Map<String, String> getArgumentsAsMap() {
        return ServerInstance.getServerInstance().getPropertiesAsMap();
    }
}

Where getServerInstance would first generate a VM Instance and later just provide a reference to the one created. When I tried to run a test plan with it in the code never appeared to be executed, and I never really knew what to expect. And whether/when this code might be called (particularly as I only really want it run once).

I was hoping that someone might be able to tell me which path to follow, whether I’m barking up the wrong tree and which resources might be helpful. I have looked at https://jmeter.apache.org/extending/jmeter_tutorial.pdf, have seen various information relating to building custom samplers, and have even used the Java Request Sampler in the past, however this has me completely stumped.

Alternatively a way to deal with JMeter’s configuration such that I could embed JMeter in a jar and load the config from classpath would allow me to run the code manually before starting JMeter from within Java.

Thanks,

Sam.

Re: Custom Configuration Plugin

Posted by sebb <se...@gmail.com>.
On 1 July 2014 08:22, Sam Drew <sa...@forgerock.com> wrote:
> I’m trying to write a custom JMeter module, that will run a Java method that I use for Server Setup and returns Map<String,String> as configuration properties that can be used in sampling.

How are you going to use the Map?

Why are the exsisting facilities for providing input to threads not sufficient?

For example,
http://jmeter.apache.org/usermanual/component_reference.html#CSV_Data_Set_Config
http://jmeter.apache.org/usermanual/functions.html#__StringFromFile

> At the moment I am slightly unsure of what TestElement I should be extending or implementing in order to achieve this, given that it seems to sit somewhere between a PreProcessor and ConfigTestElement, in that it is both setting up pre-conditions for the test and then providing these as configuration parameters.
>
> After looking at the example in jmeter-plugins for csv config element I wrote a class which was little more than
>
> class CreateMyServerConfig extends Arguments {
>     @Override
>     public Map<String, String> getArgumentsAsMap() {
>         return ServerInstance.getServerInstance().getPropertiesAsMap();
>     }
> }
>
> Where getServerInstance would first generate a VM Instance and later just provide a reference to the one created. When I tried to run a test plan with it in the code never appeared to be executed, and I never really knew what to expect. And whether/when this code might be called (particularly as I only really want it run once).
>
> I was hoping that someone might be able to tell me which path to follow, whether I’m barking up the wrong tree and which resources might be helpful. I have looked at https://jmeter.apache.org/extending/jmeter_tutorial.pdf, have seen various information relating to building custom samplers, and have even used the Java Request Sampler in the past, however this has me completely stumped.
>
> Alternatively a way to deal with JMeter’s configuration such that I could embed JMeter in a jar and load the config from classpath would allow me to run the code manually before starting JMeter from within Java.
>
> Thanks,
>
> Sam.

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


Re: Custom Configuration Plugin

Posted by Mark Miller <ma...@gmail.com>.
Sam,

I'm going to echo what's already been suggested with a little more
explanation. The way I'd suggest for you to think about your problem is
this: What interfaces can I access to sample (read) Server Instance
properties? (Do they come from a file [ssh / OS Process Sampler] can I
fetch them via a Java method [Java sampler], can I fetch them from an HTTP
interface [HTTP Sampler]?) You get the idea.

Then, if you put this into a setUp ThreadGroup, you can use some
post-processor scripting (beanshell, BSF, JSR223,... your choice) to
populate a set of JMeter properties that will be globally available in your
test plan via ${__P()} (or other ways).

I am doing something like this with variable sets that need token
substitution procession before the test plan runs. In a setUp ThreadGroup I
read XML files with JRuby (in a JSR223 Sampler), manipulate the strings and
then do $props.put(...) to populate the information I need downstream.

If you are looking to hold more than string properties in memory, JMeter
vars support object references as well. I've also done this with properties
(referencing a JRuby hash in a JMeter property). It works though I can't
say that it's a recommended approach (or not).

foo = {"foo" => "bar"} #define a Ruby hash
$props.put("foo",foo)  #stick it in the JMeter properties
$log.info("#{$props.get("foo").is_a?(Hash)}")     #>>retrieve it, returns
'true' in the log regarding its Hash type


Mark


On Tue, Jul 1, 2014 at 9:17 AM, Sam Drew <sa...@forgerock.com> wrote:

> Thanks,
>
> It looks like that should be possible. I didn’t realise that BeanShell was
> able to run arbitrary Java classes.
>
> How would I go about putting the Map<String,String> properties into
> bsh.shared so that other test elements can make use of them? Would
> iterating through the key set and doing vars.put(key, value); allow me to
> access them in threads/samplers?
>
> Regards,
>
> Sam.
>
> On 1 Jul 2014, at 11:24, Ryabtsev Vladimir <v....@pflb.ru> wrote:
>
> > You plan to use this configuration step just once during the test run,
> right?
> > Why don't you use BeanShell Sampler in SetUp Thread Group with
> bsh.shared or something similar?
> >
> > -----
> > VR
> >
> >
> > -----Original Message-----
> > From: Sam Drew [mailto:sam.drew@forgerock.com]
> > Sent: Tuesday, July 01, 2014 11:22 AM
> > To: user@jmeter.apache.org
> > Subject: Custom Configuration Plugin
> >
> > I'm trying to write a custom JMeter module, that will run a Java method
> that I use for Server Setup and returns Map<String,String> as configuration
> properties that can be used in sampling.
> >
> > At the moment I am slightly unsure of what TestElement I should be
> extending or implementing in order to achieve this, given that it seems to
> sit somewhere between a PreProcessor and ConfigTestElement, in that it is
> both setting up pre-conditions for the test and then providing these as
> configuration parameters.
> >
> > After looking at the example in jmeter-plugins for csv config element I
> wrote a class which was little more than
> >
> > class CreateMyServerConfig extends Arguments {
> >    @Override
> >    public Map<String, String> getArgumentsAsMap() {
> >        return ServerInstance.getServerInstance().getPropertiesAsMap();
> >    }
> > }
> >
> > Where getServerInstance would first generate a VM Instance and later
> just provide a reference to the one created. When I tried to run a test
> plan with it in the code never appeared to be executed, and I never really
> knew what to expect. And whether/when this code might be called
> (particularly as I only really want it run once).
> >
> > I was hoping that someone might be able to tell me which path to follow,
> whether I'm barking up the wrong tree and which resources might be helpful.
> I have looked at https://jmeter.apache.org/extending/jmeter_tutorial.pdf,
> have seen various information relating to building custom samplers, and
> have even used the Java Request Sampler in the past, however this has me
> completely stumped.
> >
> > Alternatively a way to deal with JMeter's configuration such that I
> could embed JMeter in a jar and load the config from classpath would allow
> me to run the code manually before starting JMeter from within Java.
> >
> > Thanks,
> >
> > Sam.
> >
> > ---------------------------------------------------------------------
> > 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: Custom Configuration Plugin

Posted by Ryabtsev Vladimir <v....@pflb.ru>.
About bsh.shared: http://jmeter.apache.org/usermanual/best-practices.html#bsh_variables
This is namespace which is available to all threads. You can store any variable (or reference) in bsh.shared from any thread and get it from any thread. In other languages (e.g. JSR223+groovy) you can achieve the same behavior by implementing your own singleton-like class to store shared state.

One more option described at the link given (via JMeterUtils), but it's only available for strings.

Another possible option is to use props (props.put()/props.get()). Keep in mind that props keep their values between test runs, you may want it or not.

Whatever way you choose, keep in mind that you have to take care about thread synchronization when reading and modifying stored variable, otherwise you can get unpredictable results. Convenient way to achieve this implicitly is to use Java Concurrency classes.

You may store your values in JMeter variables to access them easily from samplers (like that: ${my_var}). But vars should be initialized in every thread because variable is available only in those thread which has initialized it. This can be done in BeanShell also.
If you decide  to use properties, you will be able to access properties from fields of various samplers using function: ${__property(my_prop)}.

-----
VR


-----Original Message-----
From: Sam Drew [mailto:sam.drew@forgerock.com] 
Sent: Tuesday, July 01, 2014 7:18 PM
To: JMeter Users List
Subject: Re: Custom Configuration Plugin

Thanks,

It looks like that should be possible. I didn't realise that BeanShell was able to run arbitrary Java classes. 

How would I go about putting the Map<String,String> properties into bsh.shared so that other test elements can make use of them? Would iterating through the key set and doing vars.put(key, value); allow me to access them in threads/samplers?

Regards,

Sam.

On 1 Jul 2014, at 11:24, Ryabtsev Vladimir <v....@pflb.ru> wrote:

> You plan to use this configuration step just once during the test run, right?
> Why don't you use BeanShell Sampler in SetUp Thread Group with bsh.shared or something similar?
> 
> -----
> VR
> 
> 
> -----Original Message-----
> From: Sam Drew [mailto:sam.drew@forgerock.com] 
> Sent: Tuesday, July 01, 2014 11:22 AM
> To: user@jmeter.apache.org
> Subject: Custom Configuration Plugin
> 
> I'm trying to write a custom JMeter module, that will run a Java method that I use for Server Setup and returns Map<String,String> as configuration properties that can be used in sampling.
> 
> At the moment I am slightly unsure of what TestElement I should be extending or implementing in order to achieve this, given that it seems to sit somewhere between a PreProcessor and ConfigTestElement, in that it is both setting up pre-conditions for the test and then providing these as configuration parameters. 
> 
> After looking at the example in jmeter-plugins for csv config element I wrote a class which was little more than
> 
> class CreateMyServerConfig extends Arguments {
>    @Override
>    public Map<String, String> getArgumentsAsMap() {
>        return ServerInstance.getServerInstance().getPropertiesAsMap();
>    }
> }
> 
> Where getServerInstance would first generate a VM Instance and later just provide a reference to the one created. When I tried to run a test plan with it in the code never appeared to be executed, and I never really knew what to expect. And whether/when this code might be called (particularly as I only really want it run once).
> 
> I was hoping that someone might be able to tell me which path to follow, whether I'm barking up the wrong tree and which resources might be helpful. I have looked at https://jmeter.apache.org/extending/jmeter_tutorial.pdf, have seen various information relating to building custom samplers, and have even used the Java Request Sampler in the past, however this has me completely stumped.
> 
> Alternatively a way to deal with JMeter's configuration such that I could embed JMeter in a jar and load the config from classpath would allow me to run the code manually before starting JMeter from within Java.
> 
> Thanks,
> 
> Sam.
> 
> ---------------------------------------------------------------------
> 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


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


Re: Custom Configuration Plugin

Posted by Sam Drew <sa...@forgerock.com>.
Thanks,

It looks like that should be possible. I didn’t realise that BeanShell was able to run arbitrary Java classes. 

How would I go about putting the Map<String,String> properties into bsh.shared so that other test elements can make use of them? Would iterating through the key set and doing vars.put(key, value); allow me to access them in threads/samplers?

Regards,

Sam.

On 1 Jul 2014, at 11:24, Ryabtsev Vladimir <v....@pflb.ru> wrote:

> You plan to use this configuration step just once during the test run, right?
> Why don't you use BeanShell Sampler in SetUp Thread Group with bsh.shared or something similar?
> 
> -----
> VR
> 
> 
> -----Original Message-----
> From: Sam Drew [mailto:sam.drew@forgerock.com] 
> Sent: Tuesday, July 01, 2014 11:22 AM
> To: user@jmeter.apache.org
> Subject: Custom Configuration Plugin
> 
> I'm trying to write a custom JMeter module, that will run a Java method that I use for Server Setup and returns Map<String,String> as configuration properties that can be used in sampling.
> 
> At the moment I am slightly unsure of what TestElement I should be extending or implementing in order to achieve this, given that it seems to sit somewhere between a PreProcessor and ConfigTestElement, in that it is both setting up pre-conditions for the test and then providing these as configuration parameters. 
> 
> After looking at the example in jmeter-plugins for csv config element I wrote a class which was little more than
> 
> class CreateMyServerConfig extends Arguments {
>    @Override
>    public Map<String, String> getArgumentsAsMap() {
>        return ServerInstance.getServerInstance().getPropertiesAsMap();
>    }
> }
> 
> Where getServerInstance would first generate a VM Instance and later just provide a reference to the one created. When I tried to run a test plan with it in the code never appeared to be executed, and I never really knew what to expect. And whether/when this code might be called (particularly as I only really want it run once).
> 
> I was hoping that someone might be able to tell me which path to follow, whether I'm barking up the wrong tree and which resources might be helpful. I have looked at https://jmeter.apache.org/extending/jmeter_tutorial.pdf, have seen various information relating to building custom samplers, and have even used the Java Request Sampler in the past, however this has me completely stumped.
> 
> Alternatively a way to deal with JMeter's configuration such that I could embed JMeter in a jar and load the config from classpath would allow me to run the code manually before starting JMeter from within Java.
> 
> Thanks,
> 
> Sam.
> 
> ---------------------------------------------------------------------
> 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: Custom Configuration Plugin

Posted by Ryabtsev Vladimir <v....@pflb.ru>.
You plan to use this configuration step just once during the test run, right?
Why don't you use BeanShell Sampler in SetUp Thread Group with bsh.shared or something similar?

-----
VR


-----Original Message-----
From: Sam Drew [mailto:sam.drew@forgerock.com] 
Sent: Tuesday, July 01, 2014 11:22 AM
To: user@jmeter.apache.org
Subject: Custom Configuration Plugin

I'm trying to write a custom JMeter module, that will run a Java method that I use for Server Setup and returns Map<String,String> as configuration properties that can be used in sampling.

At the moment I am slightly unsure of what TestElement I should be extending or implementing in order to achieve this, given that it seems to sit somewhere between a PreProcessor and ConfigTestElement, in that it is both setting up pre-conditions for the test and then providing these as configuration parameters. 

After looking at the example in jmeter-plugins for csv config element I wrote a class which was little more than

class CreateMyServerConfig extends Arguments {
    @Override
    public Map<String, String> getArgumentsAsMap() {
        return ServerInstance.getServerInstance().getPropertiesAsMap();
    }
}

Where getServerInstance would first generate a VM Instance and later just provide a reference to the one created. When I tried to run a test plan with it in the code never appeared to be executed, and I never really knew what to expect. And whether/when this code might be called (particularly as I only really want it run once).

I was hoping that someone might be able to tell me which path to follow, whether I'm barking up the wrong tree and which resources might be helpful. I have looked at https://jmeter.apache.org/extending/jmeter_tutorial.pdf, have seen various information relating to building custom samplers, and have even used the Java Request Sampler in the past, however this has me completely stumped.

Alternatively a way to deal with JMeter's configuration such that I could embed JMeter in a jar and load the config from classpath would allow me to run the code manually before starting JMeter from within Java.

Thanks,

Sam.

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