summaryrefslogtreecommitdiff
path: root/java/tools/src/main/java/org/apache/qpid/tools/PerfBase.java
blob: 121e94cea1a40bbb6654eea1b1a4a96a180bca2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.tools;

import java.net.InetAddress;
import java.text.DecimalFormat;
import java.util.UUID;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.qpid.client.AMQAnyDestination;
import org.apache.qpid.client.AMQConnection;
import org.apache.qpid.client.AMQDestination;
import org.apache.qpid.client.AMQSession_0_10;
import org.apache.qpid.framing.AMQShortString;
import org.apache.qpid.messaging.Address;

public class PerfBase
{
    public final static String CODE = "CODE";
    public final static String ID = "ID";
    public final static String REPLY_ADDR = "REPLY_ADDR";
    public final static String MAX_LATENCY = "MAX_LATENCY";
    public final static String MIN_LATENCY = "MIN_LATENCY";
    public final static String AVG_LATENCY = "AVG_LATENCY";
    public final static String STD_DEV = "STD_DEV";
    public final static String CONS_RATE = "CONS_RATE";
    public final static String PROD_RATE = "PROD_RATE";
    public final static String MSG_COUNT = "MSG_COUNT";
    public final static String TIMESTAMP = "Timestamp";

    String CONTROLLER_ADDR = System.getProperty("CONT_ADDR","CONTROLLER;{create: always, node:{x-declare:{auto-delete:true}}}");

    TestParams params;
    Connection con;
    Session session;
    Session controllerSession;
    Destination dest;
    Destination myControlQueue;
    Destination controllerQueue;
    DecimalFormat df = new DecimalFormat("###.##");
    String id;
    String myControlQueueAddr;

    MessageProducer sendToController;
    MessageConsumer receiveFromController;
    String prefix = "";

    enum OPCode {
        REGISTER_CONSUMER, REGISTER_PRODUCER,
        PRODUCER_STARTWARMUP, CONSUMER_STARTWARMUP,
        CONSUMER_READY, PRODUCER_READY,
        PRODUCER_START,
        RECEIVED_END_MSG, CONSUMER_STOP,
        RECEIVED_PRODUCER_STATS, RECEIVED_CONSUMER_STATS,
        CONTINUE_TEST, STOP_TEST
    };

    enum MessageType {
        BYTES, TEXT, MAP, OBJECT;

        public static MessageType getType(String s) throws Exception
        {
            if ("text".equalsIgnoreCase(s))
            {
                return TEXT;
            }
            else if ("bytes".equalsIgnoreCase(s))
            {
                return BYTES;
            }
            /*else if ("map".equalsIgnoreCase(s))
            {
                return MAP;
            }
            else if ("object".equalsIgnoreCase(s))
            {
                return OBJECT;
            }*/
            else
            {
                throw new Exception("Unsupported message type");
            }
        }
    };

    MessageType msgType = MessageType.BYTES;

    public PerfBase(String prefix)
    {
        params = new TestParams();
        String host = "";
        try
        {
            host = InetAddress.getLocalHost().getHostName();
        }
        catch (Exception e)
        {
        }
        id = host + "-" + UUID.randomUUID().toString();
        this.prefix = prefix;
        this.myControlQueueAddr = id + ";{create: always}";
    }

    public void setUp() throws Exception
    {
        if (params.getHost().equals("") || params.getPort() == -1)
        {
            con = new AMQConnection(params.getUrl());
        }
        else
        {
            con = new AMQConnection(params.getHost(),params.getPort(),"guest","guest","test","test");
        }
        con.start();
        session = con.createSession(params.isTransacted(),
                                    params.isTransacted()? Session.SESSION_TRANSACTED:params.getAckMode());

        controllerSession = con.createSession(false, Session.AUTO_ACKNOWLEDGE);

        dest = createDestination();
        controllerQueue = new AMQAnyDestination(CONTROLLER_ADDR);
        myControlQueue = session.createQueue(myControlQueueAddr);
        msgType = MessageType.getType(params.getMessageType());
        System.out.println("Using " + msgType + " messages");

        sendToController = controllerSession.createProducer(controllerQueue);
        receiveFromController = controllerSession.createConsumer(myControlQueue);
    }

    private Destination createDestination() throws Exception
    {
        if (params.isUseUniqueDests())
        {
            System.out.println("Prefix : " + prefix);
            Address addr = Address.parse(params.getAddress());
            AMQAnyDestination temp = new AMQAnyDestination(params.getAddress());
            int type = ((AMQSession_0_10)session).resolveAddressType(temp);

            if ( type == AMQDestination.TOPIC_TYPE)
            {
                addr = new Address(addr.getName(),addr.getSubject() + "." + prefix,addr.getOptions());
                System.out.println("Setting subject : " + addr);
            }
            else
            {
                addr = new Address(addr.getName() + "_" + prefix,addr.getSubject(),addr.getOptions());
                System.out.println("Setting name : " + addr);
            }

            return new AMQAnyDestination(addr);
        }
        else
        {
            return new AMQAnyDestination(params.getAddress());
        }
    }

    public synchronized void sendMessageToController(MapMessage m) throws Exception
    {
        m.setString(ID, id);
        m.setString(REPLY_ADDR,myControlQueueAddr);
        sendToController.send(m);
    }

    public void receiveFromController(OPCode expected) throws Exception
    {
        MapMessage m = (MapMessage)receiveFromController.receive();
        OPCode code = OPCode.values()[m.getInt(CODE)];
        System.out.println("Received Code : " + code);
        if (expected != code)
        {
            throw new Exception("Expected OPCode : " + expected + " but received : " + code);
        }

    }

    public boolean continueTest() throws Exception
    {
        MapMessage m = (MapMessage)receiveFromController.receive();
        OPCode code = OPCode.values()[m.getInt(CODE)];
        System.out.println("Received Code : " + code);
        return (code == OPCode.CONTINUE_TEST);
    }

    public void tearDown() throws Exception
    {
        session.close();
        controllerSession.close();
        con.close();
    }

    public void handleError(Exception e,String msg)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(msg);
        sb.append(" ");
        sb.append(e.getMessage());
        System.err.println(sb.toString());
        e.printStackTrace();
    }
}