JMeter Interface Test Example

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

void

g Etdefaultparameterssampleresult

runtestjavasamplercontext context) similar to LR action
( context)   initialization method, similar to the LR init and setup in JUnit ()

and teardown in JUnit ()

The order of execution is: getdefaultparameters(context)- -runtest javasamplercontext context) --(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.
  • 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

JMeter Interface Test Example

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.