JMeter First Experience/java Network request script add

Source: Internet
Author: User

Jmeter Easy to use (there are many pictures on the web, tutorials, here is just a brief description):

Jmeter software use, first create a test plan, on the test plan right-click Add ---threads< c19> (Users)-- thread group

Then Right-click on the thread group-- > Add-->SamplerHttp request .

Configure the response information in the Http request , save it

Then you need to create the result Tree of the response, create step: ( Right-click on the thread group , add-on listener- View results Tree )

You can add your own listener to your needs .

Jmeter JAVA script uses :

test with a personal, custom java script .

    1. First, you need to copy it in the lib/ext package of Jmeter.

Apachejmeter_core.jar

Apachejmeter_java.jar

These two packages are in a Java project.

2. Create test class entry   implements  abstractjavasamplerclient method.

  1. jar other jar package references, need   these jar package store to lib some class not found
  2. here is to note that if When a network request occurs, in jmeter default Lib commons-apache-httpclient 3. X version network request package, here you can consider replacing the package or   Use the package Span lang= "ZH-CN", otherwise An exception occurs when requesting the network.
  3. export jar after package, you can   jar package placed to   lib/ext  package.
  4. after You create a thread group in Jmeter, create a Java request, and if the path is placed correctly, the The test class. Once you have created a listener such as the corresponding result view tree , you are ready to listen.

The Java request class is posted here for reference:

1 ImportJava.io.ByteArrayOutputStream;2 Importjava.io.IOException;3 ImportJava.io.InputStream;4 Importjava.io.UnsupportedEncodingException;5 ImportJava.net.URI;6 Importjava.net.URISyntaxException;7 Importjava.util.Arrays;8 ImportJava.util.HashMap;9 Importjava.util.List;Ten  One Importorg.apache.commons.httpclient.HttpClient; A Importorg.apache.commons.httpclient.HttpConstants; - Importorg.apache.commons.httpclient.HttpException; - ImportOrg.apache.commons.httpclient.NameValuePair; the ImportOrg.apache.commons.httpclient.methods.PostMethod; - Importorg.apache.jmeter.config.Argument; - Importorg.apache.jmeter.config.Arguments; - Importorg.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; + ImportOrg.apache.jmeter.protocol.java.sampler.JavaSamplerContext; - ImportOrg.apache.jmeter.samplers.SampleResult; +  A Importcom.leo.utils.Constants; at Importcom.leo.utils.HttpBase; -  -@SuppressWarnings ({"Deprecation", "unused" }) -  Public classTestloginextendsabstractjavasamplerclient { -  -      Publicsampleresult runtest (Javasamplercontext arg0) { inSampleresult result =NewSampleresult (); -hashmap<string, object> HashMap =NewHashmap<>(); toHashmap.put ("Size", "10"); + Result.samplestart (); - result.setrequestheaders (hashmap.tostring ()); theResult.setdataencoding ("UTF-8"); *HttpClient client =NewHttpClient (); $Postmethod Postmethod =NULL;Panax NotoginsengPostmethod =NewHttpbase (true,false). CREATEJSON_HM ( - Constants.home_goods_url, hashMap); theString BODY =NULL; +Postmethod.addrequestheader ("Content-type", "Application/json"); A          for(inti = 0; I < Postmethod.getparameters (). length; i++) { theNamevaluepair pair =postmethod.getparameters () [i]; +Result.setrequestheaders (Pair.getname () + "::" +Pair.getvalue ()); -         } $         Try { $             intStatusCode =Client.executemethod (postmethod); -             if(StatusCode = = 200) { -InputStream in =Postmethod.getresponsebodyasstream (); theBODY =ToString (in); -Result.setsuccessful (true);Wuyi Result.setresponsedata (body); the}Else { -Result.setsuccessful (false); Wu result.sampleend (); -             } About}Catch(HttpException e) { $ e.printstacktrace (); -}Catch(IOException e) { - e.printstacktrace (); -         } A Result.setresponsemessage (body); +Result.setsuccessful (true); the result.sampleend (); -         returnresult; $     } the  the     PrivateString ToString (InputStream in)throwsIOException { the         //TODO auto-generated Method Stub theBytearrayoutputstream OutStream =NewBytearrayoutputstream (); -         byte[] data =New byte[128]; in         intCount =-1; the          while((count = in.read (data, 0, 128))! =-1) theOutstream.write (data, 0, count); About  thedata =NULL; the         return NewString (Outstream.tobytearray ()); the     } +  - @Override the      Public voidSetuptest (Javasamplercontext context) {Bayi         Super. Setuptest (context); theArguments arguments=getdefaultparameters (); the          -          -     } the  the @Override the      Public voidTeardowntest (Javasamplercontext context) { the         //TODO auto-generated Method Stub -         Super. Teardowntest (context); the  the     } the}

The Sampleresult can be used to print display information: for example:

Therefore, the information in the Sampleresult needs to be set.

JMeter First Experience/java Network request script add

Related Article

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.