1. 程式人生 > >Java模擬壓測裝置傳送訊息到伺服器(Rabbitmq) python模擬上報訊息到rabbitMQ(protobuf)

Java模擬壓測裝置傳送訊息到伺服器(Rabbitmq) python模擬上報訊息到rabbitMQ(protobuf)

進入idea,新建一個maven專案

主要是模擬150個裝置同時併發,併發時間持續15min

1.建立客戶端,構造請求傳送到對應的rabbitmq的佇列,用的protobuf協議。

import com.google.protobuf.ByteString;
import com.rabbitmq.client.*;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import java.io.IOException;
import
java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.TimeoutException; public class Producer { //上傳的命令欄位值引數化 private final static String[] COMMANDS = new String[]{ "XXXXX", "YYYYYY" "ZZZZZ", }; private int index; public Producer(int index) { this
.index = index; } public byte[] message(byte[] command) { //根據proto文字訊息生成的slot3編輯指令碼,構建一個訊息 Slot3.SlotMessage.Builder slots = Slot3.SlotMessage.newBuilder(); slots.setOpenId("XXX"); slots.setProductId("YYYY"); //長整型 String NO = String.valueOf(70000000000l + index); slots.setNodeEui(NO); slots.setCommand(Slot3.SlotMessage.Command.DOWNLINK); SimpleDateFormat f
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); String time1 = f.format(date); slots.setTimestamp(time1); Slot3.SlotMessage.Payload.Builder payload = Slot3.SlotMessage.Payload.newBuilder(); ByteString command2 = ByteString.copyFrom(command); payload.setData(command2); slots.addPayload(payload); slots.setAppMessageType(0); Slot3.SlotMessage msg = slots.build(); System.out.println("before:" + msg); System.out.println("===msg Byte:"); byte[] msgbyteArray = msg.toByteArray(); System.out.println(msgbyteArray); return msgbyteArray; } /** * 建構函式 * RabbitMQ客戶端相關配置 * 連線AMQP broker並初始化佇列 */ public void produce() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); //設定RabbitMQ相關資訊 factory.setHost("10.10.XX.XX"); factory.setPort(5671); factory.setUsername("rabbitmq使用者名稱"); factory.setPassword("rabbitmq密碼"); factory.setVirtualHost("/"); //建立傳送訊息rabbitmq資訊的連線 Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); String exchange = "互動機名稱"; String queue = "佇列名稱"; for (String input : COMMANDS) { byte[] body = message(decodeHex(input)); channel.basicPublish(exchange, queue, null, body); try { //生成0~1秒的隨機 // Double code = (Math.random() * 9 + 1) * 100; // Long ms =code.longValue(); //每個命令請求間隔1s Thread.sleep(1000l); } catch (Exception e) { e.printStackTrace(); } } channel.close(); connection.close(); } /** * Hex解碼. */ public static byte[] decodeHex(String input) { try { return Hex.decodeHex(input.toCharArray()); } catch (DecoderException e) { throw new RuntimeException(e); } } }
View Code

2.模擬150個裝置同時併發,傳送訊息

 1 public class ConsumerClient {
 2 
 3     public static void main(String[] args) throws Exception {
 4         //併發150次
 5         for (int i = 0; i < 150; i++) {
 6             final int index = i;
 7             new Thread(() -> {
 8                 //建立producer
 9                 Producer producer = new Producer(index);
10                 Long start = System.currentTimeMillis();
11 
12                 while (true) {
13                     try {
14                         //傳送訊息
15                         producer.produce();
16                     } catch (Exception e) {
17                         e.printStackTrace();
18                     }
19                     //持續執行15分鐘
20                     Long end = System.currentTimeMillis();
21                     if (end - start >= 15 * 60 * 1000l) {
22                         break;
23                     }
24                 }
25 
26             }).start();
27 
28         }
29     }
30 }
View Code

3.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>Commandxingneng</groupId>
    <artifactId>Commandxingneng</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>3.6.5</version>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-core</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>

</project>
View Code

4.執行ConsumerClient.java就好了

5.proto3文字內容,怎麼生成編輯指令碼,參考python模擬上報訊息到rabbitMQ(protobuf) ,java一樣

syntax = "proto3";
message SlotMessage
{
string openId = 1;
string productId = 2; 
string nodeEui = 3; 
Command command = 4; 
string timestamp = 5;
bool encrypted = 6; 
repeated Payload payload = 7; 
int32 appMessageType = 8; 
enum Command {
U = 0; 
D = 1;
C = 2; 
O_RESULT = 3;
}
message Payload {
bytes data = 1; 
Connect connect = 2; 
repeated OResult oResult = 3;
enum Connect{
OFF = 0; 
ON = 1;
HE = 2;
}
message OResult {
string d = 1; 
int32 r = 2; 
string s = 3; 
}
}
}