How can we write java code in JMETER?
I got some information from google,Java Sampler is using for that and I tried that way Just i did copy paste some codes from googe,but getting errors from import also ,Always org.apache.jmeter is showing error,Any body can tell me about implementation and sample code alao
This is my java code
package com.code4reference.jmeter.functions;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
public class JavaRequestSamplerDemo extends AbstractJavaSamplerClient {
@Override
public SampleResult runTest(JavaSamplerContext ctx) {
JMeterVariables vars = JMeterContextService.getContext().getVariables();
vars.put("demo", "demoVariableContent");
SampleResult sampleResult = new SampleResult();
sampleResult.setSuccessful(true);
sampleResult.setResponseCodeOK();
sampleResult.setResponseMessageOK();
return sampleResult;
}
}
If you're missing org.apache.jmeter.* package you need to add everything all jars from /lib/ext folder of your JMeter installation to Java build classpath. Refer to your IDE documentation on how to do it.
In Eclipse it can be done via Project -> Properties -> Java Build Path In Idea it's in Project -> Dependencies If you're using Apache Ant, you need to do something like
<path id="build.classpath">
<fileset dir="${path.to.your.jmeter.lib.ext.folder">
<include name="*.jar"/>
</fileset>
<path>
There are at least 3 places where you can inject your Java code.
1. Java Request.
Examples:
Which are source code for JavaTest and SleepTest Java Request Samplers
2. Your own Sampler
Example:
3. Beanshell or JSR233 Sampler
JMeter supports Beanshell and JSR233 scripting, both can understand Java syntax.
JMeter source code is available from JMeter Downloads page.
Except Java Sampler which requires knowledge with specific JMeter context, other options:
The JSR223 Sampler allows JSR223 script code
For performance it is better to use __groovy function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With