18. Jmete Java Engineering Test

Source: Internet
Author: User

1. Create a new project in eclipse

2. Introduction of two Files "Apachejmeter_core.jar" and "Apachejmeter_java.jar" to Java Engineering from JMeter's installation directory Lib/ext


Import Rack Package

Add a rack package to add

Because JMeter has its own compiled methods and classes, if not imported, compile time will be error


3. Writing JMeter Auxiliary functions


The order of execution is:
Getdefaultparameters ()-----> Setup Test (javasamplercontext context)----->runtest (Javasamplercontext context) ----->teardowntest (Javasamplercontext)
Give parameters-----> Initialize-----> Run-----> End

Instance code:
Package com.jmeter_test;
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;

/**
* JMeter The custom script test class.
* The script function is to send the string in the input box.
* @param <log>
* @param <log>
* @param <Logger>
*/
public class Jmeter_test<log, log, logger> extends Abstractjavasamplerclient {
/**
* Output to the JMeter console's log class.
* You need to refer to Logkit-2.0.jar under the JMeter lib directory.
*/
Private Logger log = GetLogger ();
Private Sampleresult results; Run the results.
Private String teststr; JMeter the parameters of the console input.

/**
* Set parameters that are passed in, you can set more than one, and the parameters you have set are displayed in the JMeter parameter list.
*/
Public Arguments getdefaultparameters () {
Log.info ("Execute getdefaultparameters ...");
Arguments params = new Arguments ();
/*
* Define a parameter that is displayed in the JMeter parameter list,
* The first parameter is the default display name for the parameter,
* The second parameter is a default value
*/
Params.addargument ("Teststr", "" ");
return params;
}

/**
* Initialization method, which is executed only once per thread at the actual runtime, before the test method is run,
* Similar to the Init method in LoadRunner.
*/
public void Setuptest (Javasamplercontext arg0) {
Log.info ("Start execute setuptest ...");
results = new Sampleresult ();
Teststr = Arg0.getparameter ("Teststr", "" ");
if (teststr! = null && teststr.length () > 0) {
Results.setsamplerdata (TESTSTR);
}
}

/**
* Test execution of the loop body, depending on the number of threads and the number of cycles to execute multiple times, similar to the action method in LoadRunner
*/
Public Sampleresult runtest (Javasamplercontext arg0) {
Log.info ("Execute runtest ...");
Defines a transaction that represents the starting point of a transaction, similar to LoadRunner's lr.start_transaction
Results.samplestart ();
Defines a transaction that represents the end point of a transaction, similar to LoadRunner's lr.end_transaction
Results.sampleend ();
if (Teststr.length () < 5) {
Log.info ("Fail ...");
Used to set the success or failure of the run result, or false to indicate that the result failed, otherwise it would be successful
Results.setsuccessful (FALSE);
} else {
Log.info ("TestString is\t" +teststr);
Results.setsuccessful (TRUE);
}
return results;
}
/**
* End method, which is executed only once per thread at the actual runtime and executed after the test method runs,<br>
* Similar to the End method in LoadRunner.
*/
public void Teardowntest (Javasamplercontext arg0) {
}
The/**main function is used for local debugging and should be commented out when packaging */
//*
public static void Main (string[] args)
{
Arguments params = new Arguments ();
Params.addargument ("Teststr", "51zxw");//set parameter and give default value 1
Javasamplercontext arg0 = new Javasamplercontext (params);
Jmeter_test test = new Jmeter_test ();
Test.setuptest (arg0);
Test.runtest (arg0);
Test.teardowntest (arg0);
}/**/
}


Common methods:
1) addargument (*name*,*value*) Defining parameters
2) Samplestart () defines the beginning of the thing, similar to the LR start transaction of LR, and the same thing as LR do not put irrelevant code between things
3) Sampleend () defines the end of the thing, similar to the lr_end_transation of LR
4) setsuccessful (true,false) Set the result or failure of the operation results, jmeter statistical results fail, in the aggregation report can reflect

4. Export the project to a jar, placed under the $jmeter installation directory $/lib/ext/, other dependent jars placed under the installation directory/lib/

Select the project you want to export--->file--->export---->java---->java file--->next----> in the path to the Java file, select Place in $jmeter installation directory $/lib/ext/, the Java file is called Strtrest.java-----> tick the items that need to be exported------>finish


5. Run the script
1) Select "Test Plan" on the left side of the main interface, right-click menu-Add--->threads (Users)---> Thread Group
2) then select the new thread group, right-click menu-Add---Sampler---java request
3) Select the new Java request, right-click menu---Add---monitor---aggregation report
4) In the Java Requests tab, you can select the class name you want to test, and in the thread group option, you can enter the number of times you want to loop and the number of concurrent






18. Jmete Java Engineering Test

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.