1. Create a project, import Package 1, create a Java project 2. Introduce the Lib \ ext basic package in jmeter: apachejmeter_java.jar, apachejmeter_core.jar3, and introduce the jmeter log package: jorphan. jar, logkit-2.0.jar, commons-logging-1.1.1.jar, avalon-framework-4.1.4.jar4, introduced httpclient-4.3.4.jar (is a set of clients based on httpcore), httpcore-4.3.2.jar (is a set of HTTP protocol implementation package) jmeter 4 classes used:
Arguments |
Getdefaparameparameters() Used to obtain Interface Parameters |
Sampleresult |
Runtest(Using amplercontext context) similar to LR action
|
Void |
Setuptest(JavasamplercontextContext)The initialization method is similar to the init of LR and setup () in JUnit () |
Void |
Teardowntest(JavasamplercontextContext)Similar to LR end and teardown () in JUnit () |
The execution sequence is as follows:
Getdefaparameparameters() -->
Setuptest(Javasamplercontext context) -->
Runtest(Javasamplercontext context) -->
Teardowntest
(Javasamplercontext
Context)
Common Methods: ①. addargument ("name", "value") defines parameters ②. samplestart () defines the start of a transaction, similar to lr_start_transaction of LR, like LR, do not place irrelevant codes ③ and sampleend () between transactions to define the end of the transaction. Similar to LR's lr_end_transaction ④ and setsuccessful (true and false), set the success or failure of the running result, jmeter counts the number of successful failures, which can be reflected in the aggregation report.
Ii. Example 1: testapigetsearchsuggestion. java. This Code allows you to request an API, judge the returned data, and write a file. 2. jmeter_getsearchsuggestion.java, which is the focus of this section.
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. abstract‑amplerclient; import Org. apache. jmeter. protocol. java. sampler. using amplercontext; import Org. apache. jmeter. samplers. sampleresult; public class jmeter_getsearchsuggestion extends act1_amplerclient {Private Static string label = "jmeter_getsearchsuggestion"; // defines the label name, which is displayed in the jmeter result window private string URL; public void setuptest () {// define the initial test value. setuptest only uses system before the test starts. out. println ("setuptest") ;}@ overridepublic sampleresult runtest (datagamplercontext 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 t for calling the interface under pressure test. postjson (URL, data); Sr. setsuccessful (true);} catch (clientprotocolexception e) {Sr. setsuccessful (false); E. printstacktrace ();} catch (ioexception e) {Sr. setsuccessful (false); E. printstacktrace ();} Sr. sampleend (); // return SR for the jmeter end statistics response time mark;} public void teardowntest (datagamplercontext arg0) {super. teardowntest (arg0);} public arguments getdefaparameparameters () {// Parameter definition, displayed on the foreground, you can also 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. After the main function test is successful, compress it into a jar package and put it in the % jmeter_home % \ Lib \ ext directory.
Iii. Run Case 1, run % jmeter_home % \ bin \ jmeter. bat2, add thread group, Java request, view result tree, aggregate report 3. The execution result is
TIPS: shortcut key Ctrl + R run case, CTRL + e clear running result
References: http://wangym.iteye.com/blog/731729http://www.taobaotest.com/blogs/2293
Zookeeper