Jmeter-Java請求實戰

1.1. jmeter-java插件實現接口測試

(linux /mysql/rabbit-mq)java

 

本次須要準備環境python

Eclipse+jdk8mysql

Jmeterlinux

Pythonweb

1.1.1. Rabbit-mq web客戶端簡單介紹

Rabbit-mq的安裝參見上一次的演示視頻sql

 

生產速度:消息的產生速度,隊列消息的寫入apache

消費速度:消息的消費速度,隊列消息的傳出json

 

 

 

 

1.1.2. nmon監控使用

(1) 以管理員身份在各節點服務器安裝nmonwindows

yum install nmon

 

(2) 運行nmon監控服務器

nmon –f –s 10 –c 1200

 

參數說明:

-f 輸出文件

-s 間隔多少秒獲取一次監控數據

-c 一共要獲取多少次數據

後臺運行

nohup nmon –f –s 10 –c 1200 &

 

(3) 監控結束後運行

sort …..nmon >> ….csv

 

nmon監控文件轉化成csv格式,而後經過ssh-ftp工具取到windows下,利用nmon analyser工具將csv文件轉換成excel格式文件,並保存,能夠打開文件查看監控數據。

監控輸出文件excel文件分析說明

主要查看SYS_SUM頁內容(查看cpu利用率,磁盤IOPS,以及IO-Wait%

(cpu利用率均值在70%如下,普通磁盤iops4000如下,IO-Wait%均值在10%如下)

 

 

 

 

 

1.1.3. python屏幕截圖

因爲rabbitmq客戶端是實時更新數據,因此要保留測試過程當中的數據,須要對客戶端屏幕進行截圖。

採用 python腳本進行自動截圖

(1) 配置本地python環境

(2) 安裝 python截圖模塊

pip install image

 

(3) 編寫python截圖腳本,保存爲image.py文件

腳本示例:

import time

from PIL import ImageGrab

 

for i range(1200):

    im = ImageGrab.grab()

    addr = str(time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()))+str('.jpg')

    im.save(addr,'jpeg')

time.sleep(5)

 

(4) 將腳本拷貝到一個可寫的目錄(注意空目錄最好,而且有足夠空間),運行腳本

python image.py

在腳本目錄將生成屏幕截圖,每隔5秒一張圖片

1.1.4. 編寫rabbitmq生產者插件

(1) 本地環境eclipse

(2) 建立工程

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

建立libs文件夾並導入jmeter的核心libsjava請求lib

 

 

 

 

導入rabbitmq-java客戶端libjson數據lib

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

代碼示例以下:

package RabbitMQJmeter;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.samplers.SampleResult;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class LCS2Logic extends AbstractJavaSamplerClient {
    // 定義rabbitmq服務器的配置信息
    public static String MQ_serverip;
    public static String MQ_serverport;
    public static String MQ_serveruser;
    public static String MQ_serveruserpwd;
    public static String MQ_queue;
    public static String MQ_exchange;
    public static String MQ_rkey;
    // 定義消息發送次數
    public static String send_count;
    // 定義mq的隊列消息ec信息
    public static String ec_sessionId;
    public static String ec_ecid;
    public static String ec_routInfo;
    public static String ec_priority;
    public static String ec_groupId;
    // 定義mq的隊列消息ec msg信息
    public static String msg_templateId;
    public static String msg_templateContent;
    public static String msg_content;
    public static String msg_mobiles;
    public static String msg_sign;
    public static String msg_extendCode;
    public static String msg_needMo;

    //
    public ConnectionFactory factory;
    public Connection conn;
    public Channel channel;

    // 設置可用參數及的默認值;
    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("MQ_serverip", "192.168.32.204");
        params.addArgument("MQ_serverport", "5672");
        params.addArgument("MQ_serveruser", "mas");
        params.addArgument("MQ_serveruserpwd", "mas");
        params.addArgument("MQ_queue", "downQueue.queue2");
        params.addArgument("MQ_exchange", "downQueue.queue2");
        params.addArgument("MQ_rkey", "downQueue.queue2");
        params.addArgument("send_count", "1");
        params.addArgument("ec_sessionId", "3");
        params.addArgument("ec_ecid", "EC-1");
        params.addArgument("ec_routInfo", "127.0.0.1");
        params.addArgument("ec_priority", "1");
        params.addArgument("ec_groupId", "3");
        params.addArgument("msg_templateId", "");
        params.addArgument("msg_templateContent", "");
        params.addArgument("msg_content", "test message jmeter");
        params.addArgument("msg_mobiles", "13525102870,13671381066,18827103637");
        params.addArgument("msg_sign", "ABVFGH1");
        params.addArgument("msg_extendCode", "afsd");
        params.addArgument("msg_needMo", "1");
        return params;
    }

    // 每一個線程測試前執行一次,作一些初始化工做;
    @Override
    public void setupTest(JavaSamplerContext arg0) {
        MQ_serveruser = arg0.getParameter("MQ_serveruser", "");
        MQ_serveruserpwd = arg0.getParameter("MQ_serveruserpwd", "");
        MQ_serverip = arg0.getParameter("MQ_serverip", "");
        MQ_serverport = arg0.getParameter("MQ_serverport", "");
        MQ_queue = arg0.getParameter("MQ_queue", "");
        MQ_exchange = arg0.getParameter("MQ_exchange", "");
        MQ_rkey = arg0.getParameter("MQ_rkey", "");
        int server_port = Integer.parseInt(MQ_serverport);
        factory = new ConnectionFactory();
        factory.setUsername(MQ_serveruser);
        factory.setPassword(MQ_serveruserpwd);
        factory.setHost(MQ_serverip);
        factory.setPort(server_port);
        // Connection conn;
        try {
            conn = factory.newConnection();
            channel = conn.createChannel();
            channel.exchangeDeclare(MQ_exchange, "direct", true);// EXCHANGE
            // 定義交換機
            String queueName = MQ_queue;// message-queue獲得消息隊列
            channel.queueBind(queueName, MQ_exchange, MQ_rkey);// route-bind
            // 定義相似路由器的東西路由交換機VS隊列
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

        // long start = System.currentTimeMillis();
        // System.out.println("start:" + start);
    }

    // 開始測試,從arg0參數能夠得到參數值;
    @Override
    public SampleResult runTest(JavaSamplerContext arg0) {
        send_count = arg0.getParameter("send_count", "");
        ec_sessionId = arg0.getParameter("ec_sessionId", "");
        ec_ecid = arg0.getParameter("ec_ecid", "");
        ec_routInfo = arg0.getParameter("ec_routInfo", "");
        ec_priority = arg0.getParameter("ec_priority", "");
        ec_groupId = arg0.getParameter("ec_groupId", "");
        msg_templateId = arg0.getParameter("msg_templateId", "");
        msg_templateContent = arg0.getParameter("msg_templateContent", "");
        msg_content = arg0.getParameter("msg_content", "");
        msg_mobiles = arg0.getParameter("msg_mobiles", "");
        msg_sign = arg0.getParameter("msg_sign", "");
        msg_extendCode = arg0.getParameter("msg_extendCode", "");
        msg_needMo = arg0.getParameter("msg_needMo", "");
        //消息內容格式
        int sessionId = Integer.parseInt(ec_sessionId);
        String ecId = ec_ecid;
        String routInfo = ec_routInfo;
        int priority = Integer.parseInt(ec_priority);
        int groupId = Integer.parseInt(ec_groupId);
        String templateId = msg_templateId;
        String[] templateContent = msg_templateContent.split(",");
        String content = msg_content;
        String[] mobiles = msg_mobiles.split(",");
        String sign = msg_sign;
        String extendCode = msg_extendCode;
        int needMo = Integer.parseInt(msg_needMo);
        //轉換成json格式
        JSONObject jsonObject = new JSONObject();
        JSONObject msgObject = new JSONObject();
        jsonObject.put("sessionId", sessionId);
        jsonObject.put("ecId", ecId);
        jsonObject.put("routInfo", routInfo);
        jsonObject.put("priority", priority);
        jsonObject.put("groupId", groupId);
        //
        msgObject.put("templateId", templateId);
        msgObject.put("templateContent", templateContent);
        
        msgObject.put("content", content);
        msgObject.put("mobiles", mobiles);
        
        msgObject.put("sign", sign);
        msgObject.put("extendCode", extendCode);
        msgObject.put("needMo", needMo);
        jsonObject.put("msg", msgObject);
        //
        String messageBody = jsonObject.toString();
        byte[] messageBodyBytes = messageBody.getBytes();
        // 長鏈接服務器發送過來的消息頭,12個字節,
        byte[] mqHeadBytes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
        byte[] messageBytes = new byte[mqHeadBytes.length + messageBodyBytes.length];
        System.arraycopy(mqHeadBytes, 0, messageBytes, 0, mqHeadBytes.length);
        System.arraycopy(messageBodyBytes, 0, messageBytes, mqHeadBytes.length, messageBodyBytes.length);
        // 打印輸出調試
        // System.out.println("生產者:" + messageBody + " in thread:" +
        // Thread.currentThread().getName());
        int count = Integer.parseInt(send_count);
        ///
        SampleResult sr = new SampleResult();
        sr.setSampleLabel("JavaTest_RabbitMQ_MSG");

        try {
            // jmeter開始統計響應時間標記
            sr.sampleStart();

            // publish / sub 生產者的做用就是將消息推送到消息隊列裏面去 實現相似於publish的功能
            for (int i = 0; i < count; i++) {
                channel.basicPublish(MQ_exchange, MQ_rkey, null, messageBytes);
            }
            // sr.isSuccessful();
            sr.setSuccessful(true);
        } catch (IOException e) {
            sr.setSuccessful(false);
            e.printStackTrace();
        } finally {
            sr.sampleEnd();// jmeter 結束統計響應時間標記
        }
        return sr;
    }

    // 測試結束時調用;
    @Override
    public void teardownTest(JavaSamplerContext arg0) {
        try {
            channel.close();
            conn.close();
            // long end = System.currentTimeMillis();
            // System.out.println("end:" + end);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }

}
相關文章
相關標籤/搜索