"Turn" JMeter Learning (18) JMeter Test Java (ii)

Source: Internet
Author: User

Instance:

The service is: the input two parameters via IO to the file;

1. Open MyEclipse, write Java codeService:
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:

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;//    The service is: the input two parameters via IO to the file; public class Performencetest implements Javasamplerclient {private sampleresult results;    Private String A;    Private String B;    private String filename; Set the parameters passed in, you can set more, the parameters that have been set are displayed in the JMeter parameter list public Arguments getdefaultparameters () {Arguments params = new Argume        NTS (); Params.addargument ("filename", "0");//Set the parameter and give the default value 0 params.addargument ("a", "0");//Set the parameter and give the default value of 0 Params.adda    Rgument ("B", "0");//Set the parameter and give the default value 0 return params; }//initialization method, the actual runtime is executed only once per thread, execute public void setuptest (Javasamplercontext arg0) {results = new sampleres before the test method runs)    Ult (); }//test execution of the loop body, depending on the number of threads and the number of cycles performed multiple public 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"); Gets the parameter value set in JMeter Results.samplestart ();//JMeter start statistic response time tag try {outputservice test = new Out            Putservice ();            Test.output (filename, Integer.parseint (a), Integer.parseint (b));            Results.setsuccessful (TRUE);            The object being measured is called} catch (Throwable e) {results.setsuccessful (false);        E.printstacktrace ();    } finally {results.sampleend ();//JMeter End statistic response time Tag} return results; }//End method, which is actually run only once per thread, executes public void Teardowntest (Javasamplercontext arg0) {}/* public Stati after the test method runs.        c void Main (string[] args) {//TODO auto-generated method stub Arguments params = new Arguments (); Params.addargument ("A", "0");//Set the parameter and give the default value 0 params.addargument ("b", "0");//Set the parameter and give the default value of 0 params.addargument     ("filename", "abc.txt");   Javasamplercontext arg0 = new Javasamplercontext (params);        Performencetest test = new Performencetest ();        Test.setuptest (arg0);        Test.runtest (arg0);    Test.teardowntest (arg0); }    */}

Build the jar package and place it in the Jmeter_home/lib/ext directory.

2. Set JMeterAfter 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: here we use two functions: (1) _random generates random integers; (2) _ RandomString generate random strings, set the number of concurrent threads in a thread group to 5, and run after saving; aggregated reports:

As a result, 5 files were successfully generated:

3. Problems encountered

3.1 Export for jar file causes the class name of Java request not to be found; 3.2 JMeter Java.lang.OutOfMemoryError:PermGen space problem: Modify in Jmeter.bat: Set H eap=-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:

"Turn" JMeter Learning (18) JMeter Test Java (ii)

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.