JMeter Learning (17) JMeter test Java

Source: Internet
Author: User

Objective: To test the Java program;


First, the core steps
1. Create a Java project;
2. Add the jar file in the JMeter Lib directory to the build Path of this project;
3. Create a class and implement the Javasamplerclient interface or inherit abstractjavasamplerclient, and override:

 Public Arguments getdefaultparameters (); Set default values for available parameters;  Public void setuptest (Javasamplercontext arg0): Each thread is executed once before testing, doing some initialization work;  Public sampleresult runtest (Javasamplercontext arg0): Starting the test, the parameter values can be obtained from the arg0 parameter;  Public void teardowntest (javasamplercontext arg0): Called at the end of the test;

4.Export for runnable Jar File;
5. Place the jar package into the Jmeter_home\lib\ext directory;
6. Open JMeter as Administrator;
7. Create thread groups, Java Request, view results tree, test;

Second, examples

1. Create a new project in Eclipse: Javaforjmeter

2. Add all jars in the {Jmeter_home}\lib directory to the build Path of this project;

3. Add Class Hello with the following code:

 PackageCom.test.webservice; Public classHello { PublicString SayHello () {return"Hello"; }     Publicstring Sayhellotoperson (string s) {if(s = =NULL|| S.equals ("")) S= "Nobody"; return(NewStringBuilder ()). Append ("Hello"). Append (s). ToString (); }     Public intSumintAintb) {returnA +b; }}

4. Add the class Perftest and inherit the Abstractjavasamplerclient, and add the following code:

 PackageCom.test.webservice;Importorg.apache.jmeter.config.Arguments;Importorg.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;ImportOrg.apache.jmeter.protocol.java.sampler.JavaSamplerContext;ImportOrg.apache.jmeter.samplers.SampleResult;ImportCom.test.webservice.Hello; Public classPerftestextendsabstractjavasamplerclient {PrivateString A; PrivateString B; /**holds the result data (shown as Response data in the Tree display).*/    PrivateString Resultdata; //This method is used to customize the Java method into the parameter. //params.addargument ("Num1", ""); indicates that the parameter name is NUM1, and the default value is null. //set the default values for the available parameters;     PublicArguments getdefaultparameters () {Arguments params=NewArguments (); Params.addargument ("Num1", "" "); Params.addargument ("Num2", "" "); returnparams; }    //Each thread is executed once before testing, doing some initialization work;     Public voidsetuptest (Javasamplercontext arg0) {}//starting the test, the parameter values can be obtained from the arg0 parameter;     Publicsampleresult runtest (Javasamplercontext arg0) {a= Arg0.getparameter ("NUM1"); b= Arg0.getparameter ("num2"); Sampleresult SR=NewSampleresult (); Try{Sr.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) {sr.setresponsedata (Resultdata,NULL);            Sr.setdatatype (Sampleresult.text); }            //System.out.println (resultdata);Sr.setsuccessful (true); } Catch(Throwable e) {sr.setsuccessful (false);        E.printstacktrace (); } finally{sr.sampleend ();//JMeter End statistic response time stamp        }        returnSR; }    //called at the end of the test;     Public voidteardowntest (Javasamplercontext arg0) {//System.out.println (end); //System.out.println ("The Cost is" + (End-start)/1000);    }        //Main is only for debugging purposes, the last time to hit the jar package is commented out.     /*Public static void Main (string[] args) {//TODO auto-generated method stub Arguments params = NE           W Arguments (); Params.addargument ("Num1", "1");//Set the parameter and give the default value of 1 params.addargument ("num2", "2");//Set the parameter and give the default value of 2 Javasampl           Ercontext arg0 = new Javasamplercontext (params);           PERFTESTBBB test = new perftestbbb ();           Test.setuptest (arg0);          Test.runtest (arg0);       Test.teardowntest (arg0); }     */}

5.Export for runnable Jar File:hello.jar

6. Put this Hello.jar package into the Jmeter_home\lib\ext directory;

7. Open JMeter, add thread group, add Java request, view result tree.

Three. Explain the class used by JMeter

Arguments

getdefaultparameters () parameters used to get the interface

Sampleresult

runtest (Javasamplercontext context) action similar to LR

void

setuptest (javasamplercontext context) initialization method, similar to the LR init and the setup () in JUnit

void

teardowntest (javasamplercontext context) teardown () similar to the end of LR and JUnit ()

The order of execution is: getdefaultparameters()-- setuptest(javasamplercontext context)-- runtest(javasamplercontext Context) --teardowntest (javasamplercontext context) Common methods: ①, Addargument ("name", "value") define parameters ②, Samplestart () define the start of the transaction, similar to the LR of the lr_start_transaction, and LR like the transaction between the non-trivial code ③, Sampleend () Defines the end of the transaction, similar to LR's Lr_end_transaction④, setsuccessful (True, false) to set the success or failure of the result of the run, the number of times the JMeter statistical success failed, which can be reflected in the aggregated report.

At this point, this has been explained.

JMeter Learning (17) JMeter test Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.