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:
 Packagetest;ImportJava.io.File;ImportJava.io.PrintWriter; Public classOutputservice { Public Static voidOutput (String filename,intAintbthrowsException {PrintWriter out=NewPrintWriter (NewFile (filename)); Out.write (A+ ":" +b);    Out.close (); }}

Test class:

 Packagetest;Importorg.apache.jmeter.config.Arguments;Importorg.apache.jmeter.protocol.java.sampler.JavaSamplerClient;ImportOrg.apache.jmeter.protocol.java.sampler.JavaSamplerContext;ImportOrg.apache.jmeter.samplers.SampleResult;//The service is: the input two parameters via IO to the file; Public classPerformencetestImplementsjavasamplerclient {Privatesampleresult results; PrivateString A; PrivateString B; PrivateString filename; //Set the parameters that are passed in, you can set multiple, and the parameters you have set are displayed in the JMeter parameter list     PublicArguments getdefaultparameters () {Arguments params=NewArguments (); Params.addargument ("FileName", "0");//set the parameter and give the default value 0Params.addargument ("A", "0");//set the parameter and give the default value 0Params.addargument ("B", "0");//set the parameter and give the default value 0        returnparams; }    //initialization method, which is executed only once per thread at the actual runtime and executed before the test method runs     Public voidsetuptest (Javasamplercontext arg0) {results=NewSampleresult (); }    //The loop body of the test execution, which executes multiple times depending on the number of threads and the number of cycles     Publicsampleresult runtest (Javasamplercontext arg0) {b= Arg0.getparameter ("B");//get the parameter values set in JMeterA = Arg0.getparameter ("a");//get the parameter values set in JMeterfilename = arg0.getparameter ("filename");//get the parameter values set in JMeterResults.samplestart ();//jmeter Start statistic response time stamp        Try{outputservice Test=NewOutputservice ();            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 stamp        }        returnresults; }    //The end method, which is executed only once per thread at the actual run time and executed after the test method has finished running     Public voidteardowntest (Javasamplercontext arg0) {}/*Public static void Main (string[] args) {//TODO auto-generated method stub Arguments params = new A        Rguments (); 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=512m3.3 JMeter needs to be opened as an administrator, otherwise it will appear:

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.