JMeter 初體驗/Java 網絡請求腳本添加

Jmeter  簡易使用(網上有不少圖片,教程,此處 只是簡單 描述一下):java

Jmeter   軟件使用,首先 建立一個  測試計劃,在該 測試計劃上右鍵  添加- ->Threads(Users) -->  線程組apache

而後在 線程組上 右鍵--> 添加-->Sampler-->Http 請求 json

Http 請求中配置 響應的信息,保存便可網絡

而後須要建立響應的  結果樹,建立步驟:(在 線程組上 右鍵,添加 -->監聽器-->查看結果樹 app

可根據我的需求,自行添加 監聽器。ide

 

 

Jmeter JAVA 腳本 使用 01:post

 使用 我的自定義的java腳本進行  測試。測試

  1. 首先須要在  Jmeter   lib/ext  包中拷貝

ApacheJMeter_core.jarspa

ApacheJMeter_java.jar.net

這兩個包 java項目中。

2.建立測試類入口  implements  AbstractJavaSamplerClient ,重寫其內部的 方法便可。

  1. 將該項目打成jar包,(如有 其餘jar包的引用,須要  將這些jar 存放到 lib 文件夾下,不然 會出現 某些 class not found 異常)
  2. 此處須要注意的是,若 有網絡請求發生的時候,在Jmeter 默認lib 中存在 commons-apache-httpclient 3.x 版本的 網絡請求包,此處能夠考慮替換掉該包 或者  使用該包 ,不然 請求網絡時會出現異常。
  3. 導出 jar包後,可將  jar包放置到  lib/ext  包下。
  4.   Jmeter 建立 線程組後,建立一個 Java 請求 ,若是路徑放置正確的話,會  出現 你設定的 測試類。建立好相應的 結果查看樹之類 的監聽器後,便可監聽。

此處貼出 Java 請求 類以供參考:

 1 import java.io.ByteArrayOutputStream;
 2 import java.io.IOException;
 3 import java.io.InputStream;
 4 import java.io.UnsupportedEncodingException;
 5 import java.net.URI;
 6 import java.net.URISyntaxException;
 7 import java.util.Arrays;
 8 import java.util.HashMap;
 9 import java.util.List;
10 
11 import org.apache.commons.httpclient.HttpClient;
12 import org.apache.commons.httpclient.HttpConstants;
13 import org.apache.commons.httpclient.HttpException;
14 import org.apache.commons.httpclient.NameValuePair;
15 import org.apache.commons.httpclient.methods.PostMethod;
16 import org.apache.jmeter.config.Argument;
17 import org.apache.jmeter.config.Arguments;
18 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
19 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
20 import org.apache.jmeter.samplers.SampleResult;
21 
22 import com.leo.utils.Constants;
23 import com.leo.utils.HttpBase;
24 
25 @SuppressWarnings({ "deprecation", "unused" })
26 public class TestLogin extends AbstractJavaSamplerClient {
27 
28     public SampleResult runTest(JavaSamplerContext arg0) {
29         SampleResult result = new SampleResult();
30         HashMap<String, Object> hashMap = new HashMap<>();
31         hashMap.put("size", "10");
32         result.sampleStart();
33         result.setRequestHeaders(hashMap.toString());
34         result.setDataEncoding("UTF-8");
35         HttpClient client = new HttpClient();
36         PostMethod postMethod = null;
37         postMethod = new HttpBase(true, false).createJson_hm(
38                 Constants.HOME_GOODS_URL, hashMap);
39         String body = null;
40         postMethod.addRequestHeader("Content-Type", "application/json");
41         for (int i = 0; i < postMethod.getParameters().length; i++) {
42             NameValuePair pair = postMethod.getParameters()[i];
43             result.setRequestHeaders(pair.getName() + "::" + pair.getValue());
44         }
45         try {
46             int statusCode = client.executeMethod(postMethod);
47             if (statusCode == 200) {
48                 InputStream in = postMethod.getResponseBodyAsStream();
49                 body = ToString(in);
50                 result.setSuccessful(true);
51                 result.setResponseData(body);
52             } else {
53                 result.setSuccessful(false);
54                 result.sampleEnd();
55             }
56         } catch (HttpException e) {
57             e.printStackTrace();
58         } catch (IOException e) {
59             e.printStackTrace();
60         }
61         result.setResponseMessage(body);
62         result.setSuccessful(true);
63         result.sampleEnd();
64         return result;
65     }
66 
67     private String ToString(InputStream in) throws IOException {
68         // TODO Auto-generated method stub
69         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
70         byte[] data = new byte[128];
71         int count = -1;
72         while ((count = in.read(data, 0, 128)) != -1)
73             outStream.write(data, 0, count);
74 
75         data = null;
76         return new String(outStream.toByteArray());
77     }
78 
79     @Override
80     public void setupTest(JavaSamplerContext context) {
81         super.setupTest(context);
82         Arguments arguments= getDefaultParameters();
83         
84         
85     }
86 
87     @Override
88     public void teardownTest(JavaSamplerContext context) {
89         // TODO Auto-generated method stub
90         super.teardownTest(context);
91 
92     }
93 }

 

其中 的SampleResult  可用來打印顯示 信息: 譬如:

因此 須要  對 SampleResult 中 的 信息進行設定。

相關文章
相關標籤/搜索