1. Create a project, import a package 1. Create javaproject2, introduce basic Lib \ ext packages in jmeter: apachejmeter_java.jar, apachejmeter_core.jar3, and introduce 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 the number of workers on the Interface |
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 running sequence is as follows:
Getdefaparameparameters() -->
Setuptest(Javasamplercontext context) -->
Runtest(Javasamplercontext context) -->
Teardowntest
(Javasamplercontext
Context)
Frequently used methods: ①. addargument ("name", "value") defines the number of transactions ②. samplestart () defines the start time 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 whether the execution result succeeds or fails, jmeter counts the number of successful failures, which can be reflected in the aggregation report.
Ii. Example 1: testapigetsearchsuggestion. java. This code can be used to deduce 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 form private string URL; public void setuptest () {// defines the initial values of the test. setuptest only uses system before the start of the test. 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 {// call the method t of the interface under pressure. 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 number definition, which is 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 testing the main function successfully, compress it into a jar package and put it in the % jmeter_home % \ Lib \ ext folder.
Iii. Execution case 1. Execute % jmeter_home % \ bin \ jmeter. bat2. Join the thread group, Java request, view result tree, aggregate report 3. the result after running is
TIPS: shortcut key Ctrl + R execution case, CTRL + e clear execution result
Exam materials: http://wangym.iteye.com/blog/731729http://www.taobaotest.com/blogs/2293
Zookeeper