First, the creation of engineering, Primer 1, the creation of Java Project 2, the introduction of JMeter Lib\ext Basic Package: Apachejmeter_java.jar, APACHEJMETER_CORE.JAR3, Introduction of JMeter Log package: JORPHAN.JAR,LOGKIT-2.0.JAR,COMMONS-LOGGING-1.1.1.JAR,AVALON-FRAMEWORK-4.1.4.JAR4, Introduction of Httpclient-4.3.4.jar (is a set of HTTP protocol implementation packages)4 classes used by JMeter:
Arguments:getdefaultparameters used to get the parameters of the interface
Sampleresult:runtestjavasamplercontext context) action similar to LR
void:( context) initialization method, similar to the LR init and setup in JUnit ()
Void:teardowntest (Javasamplercontext context) similar to LR in end and JUnit in Teardown ()
The order of execution is: Getdefaultparameters ()--setuptest (Javasamplercontext context)--runtest (Javasamplercontext Context)--teardowntest (Javasamplercontext context) ①, addargument ("name", "value") define parameters②, Samplestart () define the start of the transaction, similar to the LR lr_start_transaction, and do not place extraneous code between transactions like LR③, Sampleend () defines the end of the transaction, similar to the lr_end_transaction of LR④, Setsuccessful (True, false) sets the success or failure of the run result, jmeter the number of successful statistics failures, which can be reflected in the aggregated report. Second, use Case 1, Testapigetsearchsuggestion.java, the code to implement the API interface request, the return of the data to judge, and write files. 2, Jmeter_getsearchsuggestion.java, for this section to introduce the focus
- Package com.test.jmeter;
- Import java.io.IOException;
- Import org.apache.http.client.ClientProtocolException;
- 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;
- Public class Jmeter_getsearchsuggestion extends Abstractjavasamplerclient {
- Private static String label = "Jmeter_getsearchsuggestion"; //define a label name that appears in the results window of the JMeter
- Private String URL;
- Private String data;
- Public void Setuptest () {
- Define test initial values, setuptest only used before the test starts
- System.out.println ("setuptest");
- }
- @Override
- Public Sampleresult runtest (Javasamplercontext arg0) {
- url = arg0.getparameter ("url");
- data = Arg0.getparameter ("data");
- Sampleresult SR;
- sr = New Sampleresult ();
- Sr.setsamplelabel (label);
- Testapigetsearchsuggestion t = new testapigetsearchsuggestion ();
- Sr.samplestart ();
- try {
- Method of calling the interface under pressure measurement
- T.postjson (URL, data);
- Sr.setsuccessful (true);
- } catch (Clientprotocolexception e) {
- Sr.setsuccessful (false);
- E.printstacktrace ();
- } catch (IOException e) {
- Sr.setsuccessful (false);
- E.printstacktrace ();
- }
- Sr.sampleend (); //JMeter End statistic response time stamp
- return SR;
- }
- Public void Teardowntest (Javasamplercontext arg0) {
- Super.teardowntest (arg0);
- }
- Public Arguments getdefaultparameters () {
- parameter definition, displayed in foreground, can also not define
- Arguments params = new Arguments ();
- Params.addargument ("url", "http://gapp.test.com/merchandise/GetSearchSuggestion");
- Params.addargument ("Data", "data={\" token\ ": \" aaaaaaaaaa\ ", \" body\ ": {\" keywords\ ": \" Blue Moon \ "}}");
- return params;
- }
- }
3, the main function test successfully, hit into a jar package, put in the%jmeter_home%\lib\ext directory can be
Third, run the use Case 1, run%JMETER_HOME%\BIN\JMETER.BAT2, add thread Group, Java request, view result tree, aggregate report3. The result after execution is
Tips: Shortcut keys ctrl+r Run cases, ctrl+e clear run results
Java+jmeter Interface Test