├── README └── src └── info └── jeffkit └── rabbitmq ├── BytesMaker.java └── benchmark ├── Consumer.java └── Producer.java /README: -------------------------------------------------------------------------------- 1 | 本项目用于对rabbitmq做性能测试。使用rabbitMQ的java 客户API。 2 | 3 | 依赖包: 4 | commons-cl-1.1.jar 5 | commons-io-1.2.jar 6 | rabbitmq-client.jar 7 | 8 | 使用方法: 9 | 10 | 一、入队列测试命令: 11 | cd rabbitmq-benchmark/bin/ 12 | java -cp .:../lib/commons-io-1.2.jar:../lib/commons-cli-1.1.jar:../lib/rabbitmq-client.jar info.jeffkit.rabbitmq.benchmark.Producer 13 | 接受下面四个参数: 14 | 15 | 1、队列名,默认为single。 16 | 2、每条消息大小,,默认值为1kByte。 17 | 3、消息是否持久化,默认为不持久化。 18 | 4、发送消息总条数,默认为10万条消息。 19 | 使用示例:Producer myqueue 1024 true 100000 20 | 21 | 二、出队测试命令: 22 | cd rabbitmq-benchmark/bin/ 23 | java -cp .:../lib/commons-io-1.2.jar:../lib/commons-cli-1.1.jar:../lib/rabbitmq-client.jar info.jeffkit.rabbitmq.benchmark.Consumer 24 | 接受下面5个参数: 25 | 一、队列名,默认为single。 26 | 二、消费者个数,默认为1个,每个消费者使用一条线程。 27 | 三、QOS,表示每消费者同一时间能接收到消息数,默认为0,即无限制。 28 | 四、autoAck,默认为true,即自动发送回执,无事务。 29 | 五、是否持久化,默认为否。 30 | 使用示例 :Consumer myqueue 2 10 true true 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/info/jeffkit/rabbitmq/BytesMaker.java: -------------------------------------------------------------------------------- 1 | package info.jeffkit.rabbitmq; 2 | 3 | public class BytesMaker { 4 | 5 | /** 6 | * @param args 7 | */ 8 | public static void main(String[] args) { 9 | System.out.println("Size is " + make1k().length); 10 | } 11 | 12 | public static byte[] make512(){ 13 | return makeBytes(512); 14 | } 15 | 16 | public static byte[] make1k(){ 17 | return makeBytes(1024); 18 | } 19 | 20 | public static byte[] make10k(){ 21 | return makeBytes(10240); 22 | } 23 | 24 | public static byte[] makeBytes(Integer size){ 25 | byte[] bytes = new byte[size]; 26 | for (int i = 0;i < size ;i ++){ 27 | bytes[i] = new Byte("1"); 28 | } 29 | return bytes; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/info/jeffkit/rabbitmq/benchmark/Consumer.java: -------------------------------------------------------------------------------- 1 | package info.jeffkit.rabbitmq.benchmark; 2 | 3 | import com.rabbitmq.client.Channel; 4 | import com.rabbitmq.client.Connection; 5 | import com.rabbitmq.client.ConnectionFactory; 6 | import com.rabbitmq.client.QueueingConsumer; 7 | 8 | public class Consumer { 9 | 10 | private Channel channel; 11 | 12 | private String queue; 13 | 14 | private boolean autoAck; 15 | 16 | public boolean isPersistence() { 17 | return persistence; 18 | } 19 | 20 | public void setPersistence(boolean persistence) { 21 | this.persistence = persistence; 22 | } 23 | 24 | private boolean persistence; 25 | 26 | private int qos; 27 | 28 | private String id; 29 | 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | public void setId(String id) { 35 | this.id = id; 36 | } 37 | 38 | public String getQueue() { 39 | return queue; 40 | } 41 | 42 | public void setQueue(String queue) { 43 | this.queue = queue; 44 | } 45 | 46 | public Channel getChannel() { 47 | return channel; 48 | } 49 | 50 | public boolean isAutoAck() { 51 | return autoAck; 52 | } 53 | 54 | public void setAutoAck(boolean autoAck) { 55 | this.autoAck = autoAck; 56 | } 57 | 58 | public int getQos() { 59 | return qos; 60 | } 61 | 62 | public void setQos(int qos) { 63 | this.qos = qos; 64 | } 65 | 66 | public void setChannel(Channel channel) { 67 | this.channel = channel; 68 | } 69 | 70 | 71 | public Consumer(String queue) { 72 | super(); 73 | this.queue = queue; 74 | } 75 | 76 | public void init() throws Exception{ 77 | if(null == channel){ 78 | ConnectionFactory factory = new ConnectionFactory(); 79 | factory.setHost("localhost"); 80 | Connection connection = factory.newConnection(); 81 | channel = connection.createChannel(); 82 | } 83 | 84 | channel.queueDeclare(queue, persistence, false, false, null); 85 | 86 | channel.basicQos(qos); 87 | 88 | QueueingConsumer consumer = new QueueingConsumer(channel); 89 | channel.basicConsume(queue, autoAck, consumer); 90 | 91 | //取100000条数据每次 92 | long start = System.currentTimeMillis(); 93 | int count = 0; 94 | 95 | while(true){ 96 | count ++; 97 | try{ 98 | QueueingConsumer.Delivery delivery = consumer.nextDelivery(); 99 | proccessMessage(delivery.getBody()); 100 | if (!autoAck) 101 | channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); 102 | }catch(InterruptedException e){ 103 | System.out.println("interrupted!"); 104 | continue; 105 | } 106 | if(count%10000 == 0){ 107 | long end = System.currentTimeMillis(); 108 | System.out.println(this.id + " : it took " + (end - start) + " ms to consum " + count + " messages"); 109 | //start = System.currentTimeMillis(); 110 | } 111 | } 112 | } 113 | 114 | public void proccessMessage(byte[] msg){ 115 | new String(msg); 116 | } 117 | 118 | /** 119 | * @param args 120 | * @throws Exception 121 | * 从队列中接收消息。接受五个参数: 122 | * 一、队列名,默认为single 123 | * 二、消费者个数,默认为1个 124 | * 三、QOS,默认为0,即无限制 125 | * 四、autoAck,默认为true,即自动发送回执,无事务。 126 | * 五、是否持久化,默认为否。 127 | */ 128 | public static void main(String[] args) throws Exception { 129 | String queue = "single"; 130 | Integer cc = 1; 131 | Integer qos = 0; 132 | Boolean autoAck = true; 133 | Boolean persistence = false; 134 | 135 | if(args.length > 0){ 136 | queue = args[0]; 137 | System.out.println(queue); 138 | } 139 | 140 | if(args.length > 1){ 141 | cc = Integer.valueOf(args[1]); 142 | } 143 | 144 | if(args.length > 2) 145 | qos = Integer.valueOf(args[2]); 146 | 147 | if(args.length > 3){ 148 | autoAck = Boolean.valueOf(args[3]); 149 | } 150 | 151 | if(args.length > 4){ 152 | persistence = Boolean.valueOf(args[4]); 153 | } 154 | 155 | System.out.println("there are " + cc + " comsumers"); 156 | 157 | ConnectionFactory factory = new ConnectionFactory(); 158 | factory.setHost("localhost"); 159 | Connection connection = factory.newConnection(); 160 | 161 | for(int i = 0 ; i < cc ;i ++){ 162 | final Consumer sr = new Consumer(queue); 163 | sr.setId(queue + i); 164 | sr.setQos(qos); 165 | sr.setAutoAck(autoAck); 166 | sr.setPersistence(persistence); 167 | 168 | Channel channel = connection.createChannel(); 169 | sr.setChannel(channel); 170 | Runnable ra = new Runnable(){ 171 | @Override 172 | public void run() { 173 | try { 174 | sr.init(); 175 | } catch (Exception e) { 176 | e.printStackTrace(); 177 | } 178 | } 179 | 180 | }; 181 | Thread t = new Thread(ra); 182 | t.start(); 183 | } 184 | 185 | } 186 | 187 | } 188 | -------------------------------------------------------------------------------- /src/info/jeffkit/rabbitmq/benchmark/Producer.java: -------------------------------------------------------------------------------- 1 | package info.jeffkit.rabbitmq.benchmark; 2 | 3 | import info.jeffkit.rabbitmq.BytesMaker; 4 | 5 | import com.rabbitmq.client.Channel; 6 | import com.rabbitmq.client.Connection; 7 | import com.rabbitmq.client.ConnectionFactory; 8 | import com.rabbitmq.client.MessageProperties; 9 | import com.rabbitmq.client.AMQP.BasicProperties; 10 | 11 | public class Producer { 12 | /** 13 | * @param args 14 | * @throws Exception 15 | * 消息入队列测试,接受三个参数: 16 | * 1、队列名,默认为single。 17 | * 2、每条消息大小,,默认值为1kByte。 18 | * 3、消息是否持久化,默认为不持久化。 19 | * 使用示例:SignleSender myqueue 1024 true 20 | */ 21 | public static void main(String[] args) throws Exception { 22 | ConnectionFactory factory = new ConnectionFactory(); 23 | factory.setHost("localhost"); 24 | Connection connection = factory.newConnection(); 25 | Channel channel = connection.createChannel(); 26 | 27 | byte[] data = BytesMaker.make1k(); 28 | boolean durable = false; 29 | String queue = "single"; 30 | int count = 100000; 31 | 32 | if (args.length > 0){ 33 | queue = args[0]; 34 | } 35 | 36 | if (args.length > 1){ 37 | data = BytesMaker.makeBytes(Integer.valueOf(args[1])); 38 | System.out.println(args[1] + " Bytes per msg"); 39 | } 40 | 41 | if (args.length > 2){ 42 | durable = Boolean.valueOf(args[2]); 43 | } 44 | 45 | if (args.length > 3){ 46 | count = Integer.valueOf(args[3]); 47 | } 48 | 49 | BasicProperties props = durable ? MessageProperties.PERSISTENT_TEXT_PLAIN : null; 50 | 51 | channel.queueDeclare(queue, durable, false, false, null); 52 | 53 | 54 | long start = System.currentTimeMillis(); 55 | for (int i = 0 ; i < count/10000 ; i ++){ 56 | long inner_start = System.currentTimeMillis(); 57 | for (int j = 0; j < 10000; j ++) 58 | channel.basicPublish("", queue, props, data); 59 | long inner_end = System.currentTimeMillis(); 60 | System.out.println("round " + (i + 1) + " takes " + (inner_end - inner_start) + " millseconds "); 61 | } 62 | long end = System.currentTimeMillis(); 63 | long time = end - start; 64 | System.out.println("It takes " + time + " millseconds to send " + count + " message to queue"); 65 | channel.close(); 66 | connection.close(); 67 | } 68 | 69 | } 70 | --------------------------------------------------------------------------------