"Turn" JMeter Learning (17) JMeter test Java

Source: Internet
Author: User

Purpose: To test the Java program

Directory

First, the core steps

Second, examples

Third, JMeter Java Sampler Introduction

Iv. bring your own Java Request Sampler


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 rewrite it;

Public Arguments getdefaultparameters (); Sets the default values for available parameters and public void Setuptest (Javasamplercontext arg0): Executes once per thread before testing, Do some initialization work; public Sampleresult runtest (Javasamplercontext arg0): Start the test, you can get the parameter value 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:

Package Com.test.webservice;public class Hello {public    String SayHello ()    {        return "Hello";    }    Public String Sayhellotoperson (string s)    {        if (s = = NULL | | s.equals (""))            s = "nobody";        Return (new StringBuilder ()). Append ("Hello"). Append (s). toString ();    public int sum (int a,int b)    {        return a+b;    }}

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

Note: Outputs such as System.out are displayed in the command window when the JMeter is started, but excessive output can affect the accuracy of the performance.

Package Com.test.webservice;import Org.apache.jmeter.config.arguments;import Org.apache.jmeter.protocol.java.sampler.abstractjavasamplerclient;import Org.apache.jmeter.protocol.java.sampler.javasamplercontext;import Org.apache.jmeter.samplers.SampleResult;    Import Com.test.webservice.hello;public class Perftest extends Abstractjavasamplerclient {private String A;    Private String B; /** holds the result data (shown as Response data in the Tree display).    */Private String 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 default values for available parameters and public Arguments getdefaultparameters () {Arguments params = new Arguments ();        Params.addargument ("Num1", "" ");        Params.addargument ("num2", "" ");    return params; }//Each thread is executed one time before testing, doing some initialization work; public void Setuptest (Javasamplercontext arg0) {}//start test, the parameter value can be obtained from the arg0 parameter; public     Sampleresult runtest (Javasamplercontext arg0) {a = Arg0.getparameter ("Num1");   b = Arg0.getparameter ("num2");     Sampleresult sr = new Sampleresult ();        Sr.setsamplelabel ("Java request Oh");            try {sr.samplestart ();//JMeter start statistic response time tag Hello test = new Hello ();            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 ("result is:" +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 Tag} return SR;        }//called at the end of the test; public void Teardowntest (Javasamplercontext arg0) {//SYSTEM.OUT.PRINTLN (end); SYSTEM.OUT.PRINTLN ("The CosT is "+ (End-start)/1000);    }//main is only for debugging purposes and is commented out when the jar package is last hit. /* public static void Main (string[] args) {//TODO auto-generated method stub Arguments params = new           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.

Third, JMeter Java Sampler Introduction

arguments

getdefaultparameters ()   parameters for getting the interface

sampleresult

runtest (Javasamplercontext context) is similar to the LR action, Result.samplestart () A transaction begins, Result.sampleend () a transaction   end

void

setuptest ( Javasamplercontext context)   initialization method, similar to LR in Init and in JUnit in Setup ()

void

/td>

teardowntest (javasamplercontext  Context)   teardown () in the end and JUnit similar to LR ()

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. Iv. Bring your own Java Request Sampler

The JMeter implements two Java Request sampler:sleeptest Sampler and Javatest Sampler by default.

Javatest Sampler does not send data requests to the server and is generally used for debugging purposes.

At this point, this has been explained.

"Turn" JMeter Learning (17) JMeter test Java

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.