Objective: To test the Java program;
1. Core steps
1. create a Java project; 2. add the JAR file under the lib directory of jmeter to the build path of this project; 3. create a class and implement the assumamplerclient interface or inherit abstract‑amplerclient, and rewrite the following code:
- Public arguments getdefaparameparameters (): sets available parameters and default values;
- Public void setuptest (javasamplercontext arg0): execute each thread once before the test and perform initialization;
- Public sampleresult runtest (javasamplercontext arg0): start the test. You can obtain the parameter value from the arg0 parameter;
- Public void teardowntest (effecamplercontext arg0): called at the end of the test;
4. Export is
Runnable JAR File5. Put the jar package into the jmeter_home \ Lib \ ext directory; 6.
Administrator identityOpen jmeter; 7. Create a thread group, Java request, and result tree for testing;
Ii. Instances
Jmeter is used to test the service performance. The service is to store two input parameters in a file through IO;
1. Write Java code
Service:
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(filename)); out.write(a+":"+b); out.close(); }}
Test class:
Package test; import Org. apache. jmeter. config. arguments; import Org. apache. jmeter. protocol. java. sampler. using amplerclient; import Org. apache. jmeter. protocol. java. sampler. using amplercontext; import Org. apache. jmeter. samplers. sampleresult; public class extends mencetest implements extends amplerclient {private sampleresult results; private string a; private string B; private string filename; // you can set multiple input parameters, the configured parameters are displayed in the jmeter parameter list. public arguments getdefaparameparameters () {arguments Params = new arguments (); Params. addargument ("FILENAME", "0"); // set the parameter and assign the default value 0 Params. addargument ("A", "0"); // set the parameter and assign the default value 0 Params. addargument ("B", "0"); // set the parameter and assign the default value 0 return Params;} // initialization method. during actual running, each thread is executed only once, run the public void setuptest (export amplercontext arg0) {Results = new sampleresult () ;}// test execution cycle body before running the test method, execute multiple times @ overridepublic sampleresult runtest (datagamplercontext arg0) {B = arg0.getparameter ("B") based on the number of threads and the number of cycles "); // obtain the parameter value A = arg0.getparameter ("A") set in jmeter; // obtain the parameter value filename = arg0.getparameter ("FILENAME") set in jmeter "); // obtain the parameter value results set in jmeter. samplestart (); // jmeter starts to count the response time mark try {outputservice test = new outputservice (); test. output (filename, integer. parseint (A), integer. parseint (B); results. setsuccessful (true); // called by the tested object} catch (throwable e) {results. setsuccessful (false); E. printstacktrace ();} finally {results. sampleend (); // jmeter end statistics response time mark} return results;} // end method. during actual running, each thread only executes the result once, run the public void teardowntest (export amplercontext arg0) {} public static void main (string [] ARGs) command after the test method is completed) {// todo auto-generated method stubarguments Params = new arguments (); Params. addargument ("A", "0"); // set the parameter and assign the default value 0 Params. addargument ("B", "0"); // set the parameter and assign the default value 0 parameter amplercontext arg0 = new parameter amplercontext (Params); Specify mencetest test = new parameter mencetest (); test. setuptest (arg0); test. runtest (arg0); test. teardowntest (arg0 );}}
Export is runnable JAR file;
2. Set jmeter
Open jmeter as an administrator and create a Java request. The structure is shown in: a new test class is found in the Java request:
We found three parameters in jmeter: we entered the following values in the response:
It must be strange to everyone that there is something you can't understand. This is a function provided by jmeter. We can test the performance, so we need to open multiple threads for concurrent testing, therefore, random numbers are very important. Common functions are: (1) _ random generates random integers; (2) _ randomstring generates random strings;
Set the number of concurrent threads in the thread group to 10000. Save the thread and run it. The graphic result is as follows: Aggregation Report: Table view result: the result is successful, and nearly 10000 files are successfully generated:
3. problems encountered during the period1. because export is JAR file, the Java request class name cannot be found; 2. jmeter Java. lang. outofmemoryerror: permgen space problem: In jmeter. modify in BAT: Set heap =-xms512m-xmx1024mset new =-XX: newsize = 128 M-XX: maxnewsize = 128 mset kernel vor =-XX: Kernel vorratio = 8-XX: target1_vorratio = 50% set tenuring =-XX: maxtenuringthreshold = 2 set rmigc =-dsun. RMI. DGC. client. gcinterval = 600000-dsun. RMI. DGC. server. gcinterval = 600000 set perm =-XX: permsize = 256 m-XX: maxpermsize = 512 m
3. jmeter must be opened as an administrator; otherwise, the following error occurs: