|
| 1 | +// The contents of this file are subject to the Mozilla Public License |
| 2 | +// Version 1.1 (the "License"); you may not use this file except in |
| 3 | +// compliance with the License. You may obtain a copy of the License |
| 4 | +// at http://www.mozilla.org/MPL/ |
| 5 | +// |
| 6 | +// Software distributed under the License is distributed on an "AS IS" |
| 7 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 8 | +// the License for the specific language governing rights and |
| 9 | +// limitations under the License. |
| 10 | +// |
| 11 | +// The Original Code is RabbitMQ. |
| 12 | +// |
| 13 | +// The Initial Developer of the Original Code is VMware, Inc. |
| 14 | +// Copyright (c) 2007-2012 VMware, Inc. All rights reserved. |
| 15 | +// |
| 16 | + |
| 17 | +package com.rabbitmq.client.test.performance; |
| 18 | + |
| 19 | +import com.rabbitmq.client.AMQP; |
| 20 | +import com.rabbitmq.client.Channel; |
| 21 | +import com.rabbitmq.client.Connection; |
| 22 | +import com.rabbitmq.client.ConnectionFactory; |
| 23 | +import com.rabbitmq.client.MessageProperties; |
| 24 | +import org.apache.commons.cli.CommandLine; |
| 25 | +import org.apache.commons.cli.Option; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | + |
| 29 | +public class StressManagement { |
| 30 | + protected static class Parameters { |
| 31 | + String host; |
| 32 | + int port; |
| 33 | + int queueCount; |
| 34 | + int channelCount; |
| 35 | + |
| 36 | + public static CommandLine parseCommandLine(String[] args) { |
| 37 | + CLIHelper helper = CLIHelper.defaultHelper(); |
| 38 | + helper.addOption(new Option("q", "queues", true, "number of queues")); |
| 39 | + helper.addOption(new Option("c", "channels", true, "number of channels")); |
| 40 | + return helper.parseCommandLine(args); |
| 41 | + } |
| 42 | + |
| 43 | + public Parameters(CommandLine cmd) { |
| 44 | + host = cmd.getOptionValue("h", "localhost"); |
| 45 | + port = CLIHelper.getOptionValue(cmd, "p", AMQP.PROTOCOL.PORT); |
| 46 | + queueCount = CLIHelper.getOptionValue(cmd, "q", 5000); |
| 47 | + channelCount = CLIHelper.getOptionValue(cmd, "c", 100); |
| 48 | + } |
| 49 | + |
| 50 | + public String toString() { |
| 51 | + StringBuilder b = new StringBuilder(); |
| 52 | + b.append("host=" + host); |
| 53 | + b.append(",port=" + port); |
| 54 | + b.append(",queues=" + queueCount); |
| 55 | + b.append(",channels=" + channelCount); |
| 56 | + return b.toString(); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + protected final Parameters params; |
| 62 | + protected final ConnectionFactory connectionFactory = |
| 63 | + new ConnectionFactory(); |
| 64 | + protected Connection connection; |
| 65 | + protected Channel publishChannel; |
| 66 | + protected Channel[] channels; |
| 67 | + |
| 68 | + public StressManagement(Parameters p) { |
| 69 | + params = p; |
| 70 | + } |
| 71 | + |
| 72 | + public long run() throws IOException { |
| 73 | + connectionFactory.setHost(params.host); |
| 74 | + connectionFactory.setPort(params.port); |
| 75 | + connection = connectionFactory.newConnection(); |
| 76 | + publishChannel = connection.createChannel(); |
| 77 | + |
| 78 | + System.out.println("Declaring..."); |
| 79 | + |
| 80 | + channels = new Channel[params.channelCount]; |
| 81 | + for (int i = 0; i < params.channelCount; i++) { |
| 82 | + channels[i] = connection.createChannel(); |
| 83 | + } |
| 84 | + |
| 85 | + for (int i = 0; i < params.queueCount; i++) { |
| 86 | + publishChannel.queueDeclare("queue-" + i, false, true, false, null); |
| 87 | + publishChannel.queueBind("queue-" + i, "amq.fanout", ""); |
| 88 | + } |
| 89 | + |
| 90 | + System.out.println("Declaration complete, running..."); |
| 91 | + |
| 92 | + while (true) { |
| 93 | + for (int i = 0; i < params.channelCount; i++) { |
| 94 | + publishChannel.basicPublish("amq.fanout", "", MessageProperties.BASIC, "".getBytes()); |
| 95 | + for (int j = 0; j < params.queueCount; j++) { |
| 96 | + while (channels[i].basicGet("queue-" + j, true) == null) { |
| 97 | + try { |
| 98 | + Thread.sleep(10); |
| 99 | + } catch (InterruptedException e) { |
| 100 | + |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public static void main(String[] args) throws Exception { |
| 109 | + CommandLine cmd = Parameters.parseCommandLine(args); |
| 110 | + if (cmd == null) return; |
| 111 | + Parameters params = new Parameters(cmd); |
| 112 | + System.out.println(params.toString()); |
| 113 | + StressManagement test = new StressManagement(params); |
| 114 | + test.run(); |
| 115 | + } |
| 116 | +} |
0 commit comments