Java+jmeter Interface Test

Source: Internet
Author: User

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
  1. Package com.test.jmeter;
  2. Import java.io.IOException;
  3. Import org.apache.http.client.ClientProtocolException;
  4. Import org.apache.jmeter.config.Arguments;
  5. Import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
  6. Import Org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
  7. Import Org.apache.jmeter.samplers.SampleResult;
  8. Public class Jmeter_getsearchsuggestion extends Abstractjavasamplerclient {
  9. Private static String label = "Jmeter_getsearchsuggestion"; //define a label name that appears in the results window of the JMeter
  10. Private String URL;
  11. Private String data;
  12. Public void Setuptest () {
  13. Define test initial values, setuptest only used before the test starts
  14. System.out.println ("setuptest");
  15. }
  16. @Override
  17. Public Sampleresult runtest (Javasamplercontext arg0) {
  18. url = arg0.getparameter ("url");
  19. data = Arg0.getparameter ("data");
  20. Sampleresult SR;
  21. sr = New Sampleresult ();
  22. Sr.setsamplelabel (label);
  23. Testapigetsearchsuggestion t = new testapigetsearchsuggestion ();
  24. Sr.samplestart ();
  25. try {
  26. Method of calling the interface under pressure measurement
  27. T.postjson (URL, data);
  28. Sr.setsuccessful (true);
  29. } catch (Clientprotocolexception e) {
  30. Sr.setsuccessful (false);
  31. E.printstacktrace ();
  32. } catch (IOException e) {
  33. Sr.setsuccessful (false);
  34. E.printstacktrace ();
  35. }
  36. Sr.sampleend (); //JMeter End statistic response time stamp
  37. return SR;
  38. }
  39. Public void Teardowntest (Javasamplercontext arg0) {
  40. Super.teardowntest (arg0);
  41. }
  42. Public Arguments getdefaultparameters () {
  43. parameter definition, displayed in foreground, can also not define
  44. Arguments params = new Arguments ();
  45. Params.addargument ("url", "http://gapp.test.com/merchandise/GetSearchSuggestion");
  46. Params.addargument ("Data", "data={\" token\ ": \" aaaaaaaaaa\ ", \" body\ ": {\" keywords\ ": \" Blue Moon \ "}}");
  47. return params;
  48. }
  49. }


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

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.