Sometimes you always have to write code, you have to say you have to, this does not look down on the Java request, write code, then first to implement a class,
Package com.company.jemeters; Public class Hello { publicint sum (int A,int b) { return A +B; }}
Implementation of an addition, then we go to write the test class
We need it here. Jemter Library Add to Lib
Packagecom.company.jemeters;Importorg.apache.jmeter.config.Arguments;Importorg.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;ImportOrg.apache.jmeter.protocol.java.sampler.JavaSamplerContext;ImportOrg.apache.jmeter.samplers.SampleResult; Public classPerftestextendsabstractjavasamplerclient {PrivateString A; PrivateString B; PrivateString Resultdata; PublicArguments getdefaultparmeters () {Arguments params=NewArguments (); Params.addargument ("Num1", "" "); Params.addargument ("Num2", "" "); returnparams; } Public voidsetuptest (Javasamplercontext arg0) {} Publicsampleresult runtest (Javasamplercontext arg0) {a=arg0.getparameter ("Num1"); b=arg0.getparameter ("num2"); Sampleresult Str=NewSampleresult (); Str.setsamplelabel ("Java Request"); Try{Str.samplestart ();//jmeter Start statistic response time stampHello test =NewHello (); //The response of the measured method can be output to the response data in the JMeter view result tree by doing the following. Resultdata =string.valueof (Test.sum (Integer.parseint (a), Integer. parseint (b))); if(Resultdata! =NULL&& resultdata.length () > 0) {Str.setresponsedata ("The result is:" +resultdata,NULL); Str.setdatatype (Sampleresult.text); } System.out.println (Resultdata); Str.setsuccessful (true); }Catch(Throwable e) {str.setsuccessful (false); }finally{str.sampleend (); } returnstr; } Public voidteardowntest (Javasamplercontext arg0) {}}
And that's it, and we're going to start testing the jar package.
Put it under Jemter's \lib\ext file,
Open JMeter, create a new Java request,
Then click Run
This also we finished the JMeter test Java class, is not very simple, actually this is just a start.
JMeter Testing Java code