Jmeter-java Sampler Writing Example

Source: Internet
Author: User
Tags writing test scripts

Today, listen to the section of the training, learn the Jmeter to write the Java request process.

According to a blog post, to modify the code of the bug, paste it below it; In addition to the company a classmate Raylupas wrote about JMeter write Java Sampler article , now finally see understand, write very good Ah, its word version as an accessory bar.

When we do performance testing , sometimes we need to write our own test scripts, many test tools support custom writing test scripts, such as LoadRunner have a lot of custom script protocol, such as "C VUser", "Java vuser" And so on. Similarly, JMeter also supports custom-written test code, but unlike LoadRunner, JMeter does not have its own compiler and needs to be implemented with a third-party compiler. Here's a simple example of Java Custom Test code, Use the Java compiler to write test code (Java compiler can use Eclipse,jbulider, etc.), the implementation function is: Enter any string before testing, and then determine whether the length of the string is greater than 5, if the test result is greater than the success, otherwise the test result bit failed, and then put in the The JMeter simulates 10 user tests and runs the code at the same time, with the following specific implementations:

1. Open the Java compiler, create a new project "Testlength", and create a new package "app".

2. From the installation directory Lib\ext of JMeter, copy two Files "Apachejmeter_core.jar" and "Apachejmeter_java.jar" to "Tester" in the project, and then introduce the two jar files. (The specific introduction method refers to the use of each Java compiler)

3. Create a new class in the "app" package named "Testlength", but this class inherits "Abstractjavasamplerclient" class, if the project introduces two files in step two, you can find " Abstractjavasamplerclient "class.

4. The "Testlength" class inherits the "Abstractjavasamplerclient" class and inherits four methods, namely "Getdefaultparameters", "Setuptest", "Runtest" and " Teardowntest "method." Getdefaultparameters "method is primarily used to set incoming parameters;" The Setuptest "method is the initialization method that is used to initialize each thread of the performance test." Runtest "method is the thread runtime of the performance test;" Teardowntest "method is the test end method used to end each thread in a performance test.

5. The specific implementation code is as follows:

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;
/**
* @author laugh all over the world
*
* TODO to change the template for this generated type comment go to
* Window-preferences-java-code Style. -Code Templates
*/
public class Testlength extends abstractjavasamplerclient{
Private Sampleresult results;
Private String teststr;

Initialization method, which is executed only once per thread at the actual runtime and executed before the test method is run, similar to the Init method in LoadRunner

public void Setuptest (Javasamplercontext arg0) {
results = new Sampleresult ();
Teststr = Arg0.getparameter ("teststring", "" ");
if (teststr! = null && teststr.length () > 0) {
Results.setsamplerdata (TESTSTR);
}
}

Set the parameters that are passed in, you can set multiple, and the parameters you have set are displayed in the JMeter parameter list

Public Arguments getdefaultparameters () {
Arguments params = new Arguments ();
Params.addargument ("Teststr", "abc"); Define a parameter that appears in the JMeter parameter list, the first parameter is the default display name for the parameter, and the second parameter is the default value
return params;
}

The loop body of the test execution, which executes multiple times depending on the number of threads and the number of cycles, similar to the action method in LoadRunner

@SuppressWarnings ("deprecation")
Public Sampleresult Runtest (Javasamplercontext context) {
int len = 0;
Results.samplestart (); Defines a transaction that represents the starting point of a transaction, similar to LoadRunner's lr.start_transaction
Teststr = Context.getparameter ("Teststr");
Len = Teststr.length ();
Results.sampleend (); Defines a transaction that represents the end point of a transaction, similar to LoadRunner's lr.end_transaction
Results.setdataencoding ("UTF-8");
if (Len < 5) {
System.out.println (TESTSTR);
Results.setresponsecode ("Teststr:" +teststr);
Results.setresponsedata ("Teststr:" +teststr);
Results.setsuccessful (FALSE); Used to set the success or failure of the run result, or false to indicate that the result failed, otherwise it would be successful
}else{
Results.setresponsecode ("Teststr:" +teststr);
Results.setresponsedata ("Teststr:" +teststr);
Results.setsuccessful (TRUE);
}
return results;
}

The end method, which is executed only once per thread at the actual runtime and executed after the test method runs, similar to the End method in LoadRunner

public void Teardowntest (Javasamplercontext arg0) {
}
}

6, packaging The above example, and then the resulting "testlength." Jar file is copied to the JMeter installation directory Lib\ext.

7. Run JMeter, add a thread group, then add a Java request (in sampler) below the thread group, and select the class "app we just created" in the class name of the Java request. Testlength ", enter the string to test after" Teststr "in the parameter list below, then add a listener (aggregated report) and set the number of simulated users to be tested. If the test is unsuccessful, JMeter throws the string in its own output box.

From the above example, we can find that using JMeter custom Java test Code, with the JMeter of the function, you can achieve the LoadRunner "Java vuser" protocol Most of the functions, but there is no user limit and completely free (hehe). The above example is very simple, and there is no practical significance, just a simple jmeter test code example, for the purpose of the hope that we communicate together and progress together.


Jmeter-java Sampler Writing Example

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.