基於Jmeter的thrift-RPC接口測試

根據需求,產品部分功能採用thrift-RPC協議進行接口的增、刪、改、查,前期採用Junit對其進行測試,爲了提升RPC接口測試的簡潔化和後期的性能測試需求,打算經過Jmeter的java類測試實現。java

前期準備:spring

開發提供了IDL描述的Thrift文件:dataService.thrift,其中定義了相關接口函數及數據類型,以下:apache

enum VStatus {
    ON,
    OFF
}

struct VData {
    1: required string tablename;
    2: required string time;
    3: required string jsons;
    4: optional VStatus status;
}

struct VCondition {
    1: required string tablename;
    2: required i64 starttime;
    3: required i64 endtime;
}

service DataService {
    string write(1: VData data);
    string read(1: VCondition condition);
}

在本地部署thrift.exe並設置環境變量,以後在dataService.thrift所在的目錄下執行:json

 thrift -r -gen java dataService.thrift              //備註:我這裏是用java語言;
執行成功後在該目錄下產生gen-java文件夾,打開該文件夾至api目錄下,生成4個文件:DataService.java、VCondition.java、VData.java和VStatus.java;
 
基於Jmeter的jar代碼編寫:
建立Maven項目,編輯pom.xml添加依賴,以下所示:
<dependencies>
    <dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.11.0</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>   
       <dependency>
           <groupId>org.apache.logging.log4j</groupId>
           <artifactId>log4j-api</artifactId>
           <version>2.8.2</version>
       </dependency>
       <dependency>
           <groupId>org.apache.logging.log4j</groupId>
           <artifactId>log4j-core</artifactId>
           <version>2.8.2</version>
       </dependency>           

   </dependencies>

以後手動加載本地Jmeter版本目錄\Jmeter_Home\lib\ext中的ApacheJMeter_core.jar和ApacheJMeter_java.jar做爲外部依賴包(注:直接maven加載相關版本的依賴包執行時存在衝突)api

將以前生成的4個java文件導入項目,設置包名爲:com.test.thrift.api,在項目包名下建立ReadRPC.java類,根據Jmeter中java類要求須要繼承 AbstractJavaSamplerClient,代碼以下:maven

package com.test.RPCTest;

import com.test.thrift.api.DataService;
import com.test.thrift.api.VCondition;

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;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class ReadRPC extends AbstractJavaSamplerClient{
    private TTransport tTransport;
    private DataService.Client dataService;
    private TProtocol tProtocol;
    private VCondition vCondition;

    @Override      
    public Arguments getDefaultParameters(){  //默認參數值,包括服務ip、端口、目標表、起止時間等
        Arguments arguments=new Arguments();
        arguments.addArgument("IP", "127.0.0.1");
        arguments.addArgument("Port", "8888");
        arguments.addArgument("TableName", "vle::test.trpc");
        arguments.addArgument("StartTime", "1539752449");
        arguments.addArgument("EndTime", "1539752449");
        return arguments;
    }
    
    @Override
    public void setupTest(JavaSamplerContext context) { //初始化參數,並經過TTransport模式TBinaryProtocol二進制協議與服務端創建鏈接 // TODO Auto-generated method stub
        String ip=context.getParameter("IP");
        int port=context.getIntParameter("Port");
        String tableName=context.getParameter("TableName");
        long startTime=context.getLongParameter("StartTime");
        long endTime=context.getLongParameter("EndTime");
        tTransport=new TSocket(ip,port,30000);
        vCondition=new VCondition();
        try {
            tTransport.open();
            tProtocol=new tBinaryProtocol(tTransport);
            dataService=new DataService.Client(tProtocol);
            vCondition.setTablename(tableName);
            vCondition.setStarttime(startTime);
            vCondition.setEndtime(endTime);
        } catch (Exception e) {
            // TODO: handle exception
        }
        super.setupTest(context);
    }
    
    @Override
    public SampleResult runTest(JavaSamplerContext context) {
        // TODO Auto-generated method stub
        SampleResult sampleResult = new SampleResult();
        sampleResult.setDataEncoding("utf8");
        sampleResult.sampleStart();
        try {
            String msg=dataService.read(tCondition);//執行讀操做,返回響應結果信息到msg if(null==msg){//爲空判斷
                sampleResult.setSuccessful(false);
                sampleResult.setResponseMessage("響應結果爲空!");
                sampleResult.setSampleLabel("RPC讀取");
                sampleResult.setResponseOK();
                sampleResult.setResponseData("響應結果爲空!");
                sampleResult.setDataType(SampleResult.TEXT);
                return sampleResult;
            }
            else {
                sampleResult.setSuccessful(true);
                sampleResult.setSampleLabel("RPC讀取");
                sampleResult.setResponseOK();
                sampleResult.setResponseData(msg);
                System.out.println("msg="+msg);
                sampleResult.setDataType(SampleResult.TEXT);
                return sampleResult;
            }
            
        } catch (Exception e) {
            // TODO: handle exception
            sampleResult.setSuccessful(false);
            e.printStackTrace();
        }
        finally{
            sampleResult.sampleEnd();
        }
        return sampleResult;
    }
    
    public void teardownTest(JavaSamplerContext context){
        tTransport.close();//關閉鏈接 super.teardownTest(context);
    }

}

在main函數中調用ReadRPC相關函數,代碼以下:ide

    public static void main( String[] args )
    {
        JmeterRPC jrpc=new JmeterRPC();
        jrpc.read();

    }
    
    public void read(){
        Arguments arguments=new Arguments();
        arguments.addArgument("IP", "127.0.0.1");
        arguments.addArgument("Port", "8888");
        arguments.addArgument("TableName", "vle::test.trpc");
        arguments.addArgument("StartTime", "1539752449");
        arguments.addArgument("EndTime", "1539752449");
        JavaSamplerContext context=new JavaSamplerContext(arguments);
        ReadRPC rpc=new ReadRPC();
        rpc.setupTest(context);
        rpc.runTest(context);
        rpc.teardownTest(context);
    }

運行程序輸出msg=[{".........相關響應數據.............."}]函數

調試成功後保存後,選中項目右鍵「Export」->「Java」->「Runnable JAR file」,點擊「Next」,設置輸出路徑和依賴庫,以下圖所示:spring-boot

備註:選擇第一種方式將依賴包打在裏面;性能

將打好的RPC.jar包拷貝到JMeter_Home\lib\ext下,啓動jmeter控制檯打印該jar信息,以下圖所示:

啓動完成後添加Java請求,顯示讀取類名,配置相關參數,以下圖所示:

 

執行結果以下:

以後即可以經過該jar進行RPC相關接口測試,並且保證其餘測試人員無需寫代碼即可進行測試。

相關文章
相關標籤/搜索