Rabbitmq优先级队列写入性能随着优先级的增加而减少

时间:2018-02-06 05:51:01

标签: rabbitmq priority-queue

我正在将Rabbitmq 3.7.3与Java客户端5.1.2 [amqp-client-5.1.2.jar]一起用于优先级队列。在我的用例中,我将在一个非持久性队列中拥有最多60个优先级,其中只有少数最多可使用10-15个。

案例1.如果我已经将队列定义为具有10个优先级,并且将0到9个优先级的消息推送到队列,则每秒获得12500个写入。

案例2.如果我已将队列定义为具有60个优先级,并且将0-9个优先级范围内的消息推送到队列,则每秒可获得4200个写入。

案例3:如果我已经将队列定义为具有250个优先级,并且将0-9个优先级范围内的消息推送到队列,那么我每秒只能获得1500个写入。

这里观察到的是,当我们增加队列的优先级容量时,尽管只使用了很少的队列,但写入性能会下降。

以下是代码片段:[使用单线程完成写入]

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.AMQP.BasicProperties;
import java.io.*;
import java.util.*;

public class Write implements Runnable{
    int start;
    int count;
    int end;
    private long unixTime;
    private long timeTaken;
    private long totalTimeTaken;
    private long totalRequests;
    private long minLatency;
    private long maxLatency;
    private double avgLatency;
    private FileOutputStream out;

    int parity = 1;
    int ndnc = 0;
    int mnp = 1001;
    int blist = 0;
    String value;
    ConnectionFactory factory;
    Connection connection;
    Channel channel;
    String QUEUE_NAME;  



    public  Write(int s, int c){
        this.start = s;
        this.count = c;
        this.end = this.count;
        this.totalTimeTaken = 0;
        this.totalRequests = 0;
        this.minLatency = 1000000;
        this.maxLatency = 0;

        try{    
            this.QUEUE_NAME = "queuedata_4";
            this.factory = new ConnectionFactory();
            factory.setHost("192.168.1.100");
            factory.setUsername("user");
            factory.setPassword("pass");
            this.connection = factory.newConnection();
            this.channel = this.connection.createChannel();
            Map<String, Object> args = new HashMap<String, Object>();
            args.put("x-max-priority", 60);
            this.channel.queueDeclare(QUEUE_NAME, false, false, false, args);
        }catch(Exception e){
            System.out.println("Create Exception"+e);
        }   
    }

    public void run(){
        String message;
        byte[] data = null;

        for(int k=this.start; k<=(this.end); k++){
            message = "Message_" + k;
            unixTime = System.nanoTime();
            try{
                    this.channel.basicPublish(
                        "", 
                        this.QUEUE_NAME, 
                        new BasicProperties.Builder()
                        .deliveryMode(1)
                        .priority(k%10+1)
                        .build(),
                        message.getBytes("UTF-8")
                    );
                }catch(Exception e){
                    System.out.println("New connection made"+e);
            }

            timeTaken = System.nanoTime() - unixTime;
            totalTimeTaken += timeTaken;
            if(timeTaken < minLatency){
                minLatency = timeTaken;
            }
            if(timeTaken > maxLatency){
                maxLatency = timeTaken;
            }
            totalRequests ++;
        }

        avgLatency = totalTimeTaken / totalRequests;
        System.out.println("TotalReqs:" + totalRequests + " 
        TotalTime:" + ((float)totalTimeTaken/1000000.0) + " 
        MinLatency:" + ((float)minLatency/1000000.0) + " MaxLatency:" 
        + ((float)maxLatency/1000000.0) + " AvgLatency:" + 
        ((float)avgLatency/1000000.0));
        try{
            channel.close();
                connection.close(); 
        }catch(Exception e){
             System.out.println("Close Exception");
        }
    }



}

0 个答案:

没有答案