JMeter Primer (4): Java Request Instance

Source: Internet
Author: User

Objective: To test the Java program;

   first, the core steps1. Create a Java project; 2.JMeterThe jar file in the Lib directory is added 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 and public void Setuptest (Javasamplercontext arg0): executed once per thread before testing, Do some initialization work ; public Sampleresult runtest (Javasamplercontext arg0): Starts the test, the parameter value can be obtained from the arg0 parameter; public void Teardowntest (Javasamplercontext arg0): Called at the end of the test; 4. Export to runnable Jar File;
5. Place the jar package in the Jmeter_home\lib\ext directory; 6. Open JMeter as Administrator; 7. Create Thread group, Java Request, result tree, test;Second, examplesUsing JMeter for servicePerformance Testing, the service is: the input two parameters via IO to the file, 1, write Java code services:
Package test, import Java.io.file;import Java.io.printwriter;public class Outputservice {public static void Output (String filename,int A, int b) throws Exception {PrintWriter out = new PrintWriter (new File); Out.write (A + " : "+b); Out.close ();}}
Test class: (see Final Test Class)2. Set JMeter  After opening jmeter as an administrator and creating a Java request, the structure looks like this: A new test class was found in Java requests: We found three parameters in the JMeter: we fill in the values of the response: we must be very strange, how there are some things that can not understand, it is  JMeter provides a function that we can do because we have to do a performance test, so we need to open multiple threads for concurrent testing, so random numbers are important; commonly used functions are: (1) _random generate random integers, (2) _randomstring generate random strings; Set the number of concurrent threads in the thread group to 10000 and run after saving; the graph results are as follows: Aggregated report: Table View results: Results succeeded, nearly 10,000 files were generated: 3. Problems encountered during 3.1 export to jar file causes Java request The class name cannot be found; 3.2 JMeter Java.lang.OutOfMemoryError:PermGen space problem: modified in Jmeter.bat: Set heap=-xms512m-xmx1024m set new= -xx:newsize=128m-xx:maxnewsize=128m set survivor=-xx:survivorratio=8-xx:targetsurvivorratio=50% set TENURING=-XX: maxtenuringthreshold=2 Set rmigc=-dsun.rmi.dgc.client.gcinterval=600000-dsun.rmi.dgc.server.gcinterval=600000 Set PERM=-XX:PERMSIZE=256M-XX:MAXPERMSIZE=512M 3.3 JMeter needs to be opened as an administrator, otherwise it will appear:
Package Test;import Org.apache.jmeter.config.arguments;import Org.apache.jmeter.protocol.java.sampler.javasamplerclient;import Org.apache.jmeter.protocol.java.sampler.javasamplercontext;import Org.apache.jmeter.samplers.SampleResult; public class Performencetest implements Javasamplerclient {private Sampleresult results;private string A;private string b ;p rivate String filename;//Sets the parameters passed in, you can set more than one, the parameters that have been set are displayed in the JMeter parameter list of public Arguments getdefaultparameters () {Arguments params = new Arguments ();p arams.addargument ("filename", "0");//Set the parameter and give the default value 0params.addargument ("a", "0");//Set parameters, and give the default value 0params.addargument ("B", "0");//Set the parameter and give the default value 0return params;} The initialization method, which is executed only once per thread at runtime, executes public void Setuptest (Javasamplercontext arg0) {results = new Sampleresult () before the test method runs;} The loop body of the test execution, depending on the number of threads and the number of cycles, can be executed multiple times @overridepublic sampleresult runtest (Javasamplercontext arg0) {b = Arg0.getparameter ("b "); Gets the parameter value set in JMeter a = Arg0.getparameter ("a"); Gets the parameter value set in JMeter filename = arg0.getparameter ("filename"); Get in JMeterSet the parameter value Results.samplestart ();//JMeter start statistic response time tag try {outputservice test = new Outputservice (); Test.output (filename, Integer.parseint (a), Integer.parseint (b)); Results.setsuccessful (true);//The object being tested} catch (Throwable e) { Results.setsuccessful (false); E.printstacktrace ();} finally {results.sampleend ();//JMeter End statistic Response time tag}return results;} The end method, which is actually run only once per thread, executes public void Teardowntest (Javasamplercontext arg0) {}public static void Main (string[) after the test method run finishes ] args) {//TODO auto-generated method Stubarguments params = new Arguments ();p arams.addargument ("A", "0");//Set the parameter and give the default value 0 Params.addargument ("B", "0");//Set the parameter and give the default value 0JavaSamplerContext arg0 = new Javasamplercontext (params); Performencetest test = new Performencetest (); Test.setuptest (arg0); Test.runtest (arg0); Test.teardowntest (arg0);}} Export to Runnable Jar File;

Getting Started with JMeter (4): Java Request Instance

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.