Jmeter-Java real request

1.1.  Jmeter-the Java plug-in implementation interface test

(linux /mysql/rabbit-mq)

 

The need to prepare the environment

Eclipse+jdk8

Jmeter

Python

1.1.1.  R & lt abbit-MQ Web client brief 

Rabbit-mq a demo video of the installation See

 

Production speed: generating a speed of the message, write the message queue

Consumption rate: rate of consumption of the message, the message queue of outgoing

 

 

 

 

1.1.2.  Nmon monitor usage

(1)  as an administrator installed in each server node nmon

yum install nmon

 

(2)  run nmon monitoring

nmon –f –s 10 –c 1200

 

Parameter Description:

-f output file

-s interval of time how many seconds to obtain monitoring data

To obtain a total of how many times -c data

Background process

nohup nmon –f –s 10 –c 1200 &

 

(3) monitor the end of the run

sort …..nmon >> ….csv

 

The nmon monitor file is converted into csv format, and then by ssh-ftp tool to take windows under use nmon Analyzer tool csv file into a excel file format, and saved, the file can be opened view monitoring data.

Monitors the output file excel file analysis shows

The main view SYS_SUM pages (see the cpu utilization, disk IOPS , and IO-the Wait% )

(cpu utilization Means 70% or less, a normal disk iops in 4000 or less, the IO-the Wait% average at 10% or less )

 

 

 

 

 

1.1.3.  Python Screenshots

Since rabbitmq client data is updated in real time, so you want to keep the data in the testing process, the need for the client screen shots.

Using python script to automatically capture

(1)  Configure the local python environment

(2)  Installation python theme module

pip install image

 

(3)  write python Screenshot script, save it as Image .py file

Script Example:

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) a copy of the script to a writable directory (note empty directory best, and there is enough space), run the script

python image.py

In the script directory will generate screenshot every 5 Miao picture

1.1.4.  Write rabbitmq producers plug

(1)  local environment eclipse

(2) Create Project

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Create libs folder and import jmeter core libs and java request lib ;

 

 

 

 

Introducing rabbitmq-java client lib and json data lib

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Code examples are as follows:

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
     // configuration information defining rabbitmq server 
    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;
     // definition of message the number of 
    public  static String send_count How many;
     // queue of messages defined mq ec information 
    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;

    voidpublic    @Override
    perform a test before each thread to do some initialization work;//
    }
 
        MQ_serveruserSetupTest (JavaSamplerContext arg0) {= 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);//the bind-route
             // define something similar routing switch router queue VS 
        } the catch (IOException E) { 
            e.printStackTrace (); 
        } the catch (a TimeoutException E) { 
            e.printStackTrace (); 
        } 

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

    // start the test, parameter values can be obtained from the parameter arg0; 
    @Override
     public SampleResult the runTest (JavaSamplerContext arg0) { 
        send_count How many = arg0.getParameter ( "send_count How many "," " ); 
        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 the messageBody = jsonObject.toString ();
         byte [] = messageBodyBytes messageBody.getBytes ();
         // long message sent by the server connected to the first, 12 bytes, 
        byte [] = {0 mqHeadBytes,. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9, 10,. 11 };
         byte [] = messageBytes new new  byte [mqHeadBytes.length + messageBodyBytes.length]; 
        System.arraycopy (mqHeadBytes, 0, messageBytes, 0 , mqHeadBytes.length); 
        System.arraycopy (messageBodyBytes, 0, MessageBytes, mqHeadBytes.length, messageBodyBytes.length);
         // print out debugging
         // System.out.println ( "producer:" + messageBody + "in the Thread:" +
         // Thread.currentThread () getName (). ); 
        int COUNT = the Integer.parseInt (send_count How many);
         // / 
        SampleResult SR = new new SampleResult (); 
        sr.setSampleLabel ( "JavaTest_RabbitMQ_MSG" ); 

        the try {
             // JMeter response time counting start flag 
            sr.sampleStart (); 

            // publish / sub producer action is to push the message to the message queue inside the publish to achieve similar functionality 
            for ( int0 = I; I <COUNT; I ++ ) {
                channel.basicPublish (MQ_exchange, MQ_rkey, null , messageBytes); 
            } 
            // sr.isSuccessful (); 
            sr.setSuccessful ( to true ); 
        } the catch (IOException E) { 
            sr.setSuccessful ( to false ); 
            e.printStackTrace (); 
        } the finally { 
            sr.sampleEnd (); // JMeter end tag response time statistics 
        }
         return SR; 
    } 

    // end of the test call; 
    @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();
        }

    }

}

 

Guess you like

Origin www.cnblogs.com/longronglang/p/11487141.html