Jmeter介面測試

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   



一、建立project、引包1、建立JAVAproject2、引入Jmeter中lib\ext基礎包:ApacheJMeter_java.jar、ApacheJMeter_core.jar3、引入Jmeter日誌包:jorphan.jar,logkit-2.0.jar,commons-logging-1.1.1.jar,avalon-framework-4.1.4.jar4、引入httpclient-4.3.4.jar(是基於httpcore的一套用戶端)、httpcore-4.3.2.jar(是一套http協議實現包)Jmeter用到的4個類:

Arguments

getDefaultParameters() 用於擷取介面的參數

SampleResult

runTest(JavaSamplerContext context) 類似於LR的Action

void

setupTest(JavaSamplerContext context) 初始化方法,類似於LR的init和Junit中的setUp()

void

teardownTest(JavaSamplerContext context) 類似於LR的end和Junit中的tearDown()

啟動並執行先後順序為: getDefaultParameters() -->  setupTest(JavaSamplerContext context) -->  runTest(JavaSamplerContext context) -->  teardownTest (JavaSamplerContext  context)
經常使用的方法:①、addArgument("name", "value")    定義參數②、sampleStart()    定義事務的開始,類似於LR的lr_start_transaction,和LR一樣事務間不要放無關代碼③、sampleEnd()    定義事務的結束,類似於LR的lr_end_transaction④、setSuccessful(true、false)    設定執行結果的成功或失敗,Jmeter統計成功失敗的次數,在彙總報告中可以體現。
二、用例1、TestApiGetSearchSuggestion.java,該代碼實現對Api介面的請求,對返回的資料進行推斷,並寫檔案。2、Jmeter_GetSearchSuggestion.java,為本節介紹的重點
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.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 ";    //定義label名稱,顯示在jmeter的結果表單private String url;private String data;public void setupTest(){//定義測試初始值,setupTest僅僅在測試開始前使用System.out.println("setupTest");}@Overridepublic 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 {//調用被壓測介面的方法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 結束統計回應時間標記return sr;}public void teardownTest(JavaSamplerContext arg0){super.teardownTest(arg0);}public Arguments getDefaultParameters(){//參數定義,顯示在前台,也能夠不定義Arguments params = new Arguments();params.addArgument("url", "http://gapp.test.com/merchandise/GetSearchSuggestion");params.addArgument("data", "data={\"token\": \"aaaaaaaaaa\",\"body\": {\"keywords\": \"藍月亮\"}}");return params;}}


3、main函數測試成功後,打成jar包,放到%Jmeter_Home%\lib\ext檔案夾中就可以

三、執行用例1、執行%Jmeter_Home%\bin\jmeter.bat2、加入線程組,Java請求、查看結果樹、彙總報告3、運行後的結果為
Tips:快速鍵Ctrl+R執行用例、Ctrl+E清除執行結果

參考資料:http://wangym.iteye.com/blog/731729http://www.taobaotest.com/blogs/2293



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.