summaryrefslogtreecommitdiff
path: root/java/client/src/old_test/java/org/apache/qpid/codec/BasicDeliverTest.java
blob: 1db7e200bded1b4a3b88b9aebf13ca6f3bfaabf8 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 *
 * 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.codec;

import org.apache.qpid.framing.*;
import org.apache.mina.common.*;
import org.apache.mina.common.support.BaseIoSession;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

import java.net.SocketAddress;

/**
 */
public class BasicDeliverTest
{
    public static void main(String[] argv) throws Exception
    {
        BasicDeliverTest test = new BasicDeliverTest();

        //warm up:
        test.encode(512, 100000);

        //real tests:
        test.encode(16, 10000, 15);
        test.encode(32, 10000, 15);
        test.encode(64, 10000, 15);
        test.encode(128, 10000, 15);
        test.encode(256, 10000, 15);
        test.encode(512, 10000, 15);
        test.encode(1024, 10000, 15);
        test.encode(2048, 10000, 15);

        test.decode(16, 10000, 15);
        test.decode(32, 10000, 15);
        test.decode(64, 10000, 15);
        test.decode(128, 10000, 15);
        test.decode(256, 10000, 15);
        test.decode(512, 10000, 15);
        test.decode(1024, 10000, 15);
        test.decode(2048, 10000, 15);
    }

    void decode(int size, int count, int iterations) throws Exception
    {
        long min = Long.MAX_VALUE;
        long max = 0;
        long total = 0;
        for (int i = 0; i < iterations; i++)
        {
            long time = decode(size, count);
            total += time;
            if (time < min)
            {
                min = time;
            }
            if (time > max)
            {
                max = time;
            }
        }
        System.out.println("Decoded " + count + " messages of " + size +
                " bytes: avg=" + (total / iterations) + ", min=" + min + ", max=" + max);
    }


    long decode(int size, int count) throws Exception
    {
        AMQDataBlock block = getDataBlock(size);
        ByteBuffer data = ByteBuffer.allocate((int) block.getSize()); // XXX: Is cast a problem?
        block.writePayload(data);
        data.flip();
        AMQDecoder decoder = new AMQDecoder(false);
        long start = System.currentTimeMillis();
        for (int i = 0; i < count; i++)
        {
            decoder.decode(session, data, decoderOutput);
            data.rewind();
        }
        return System.currentTimeMillis() - start;
    }

    void encode(int size, int count, int iterations) throws Exception
    {
        long min = Long.MAX_VALUE;
        long max = 0;
        long total = 0;
        for (int i = 0; i < iterations; i++)
        {
            long time = encode(size, count);
            total += time;
            if (time < min)
            {
                min = time;
            }
            if (time > max)
            {
                max = time;
            }
        }
        System.out.println("Encoded " + count + " messages of " + size +
                " bytes: avg=" + (total / iterations) + ", min=" + min + ", max=" + max);
    }

    long encode(int size, int count) throws Exception
    {
        IoSession session = null;
        AMQDataBlock block = getDataBlock(size);
        AMQEncoder encoder = new AMQEncoder();
        long start = System.currentTimeMillis();
        for (int i = 0; i < count; i++)
        {
            encoder.encode(session, block, encoderOutput);
        }
        return System.currentTimeMillis() - start;
    }

    private final ProtocolEncoderOutput encoderOutput = new ProtocolEncoderOutput()
    {

        public void write(ByteBuffer byteBuffer)
        {
        }

        public void mergeAll()
        {
        }

        public WriteFuture flush()
        {
            return null;
        }
    };

    private final ProtocolDecoderOutput decoderOutput = new ProtocolDecoderOutput()
    {
        public void write(Object object)
        {
        }

        public void flush()
        {
        }
    };

    private final IoSession session = new BaseIoSession()
    {

        protected void updateTrafficMask()
        {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        public IoService getService()
        {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public IoServiceConfig getServiceConfig()
        {
            return null;
        }

        public IoHandler getHandler()
        {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public IoSessionConfig getConfig()
        {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public IoFilterChain getFilterChain()
        {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public TransportType getTransportType()
        {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public SocketAddress getRemoteAddress()
        {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public SocketAddress getLocalAddress()
        {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public SocketAddress getServiceAddress()
        {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public int getScheduledWriteRequests()
        {
            return 0;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public int getScheduledWriteBytes()
        {
            return 0;  //To change body of implemented methods use File | Settings | File Templates.
        }
    };

    private static final char[] DATA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

    static CompositeAMQDataBlock getDataBlock(int size)
    {
        //create a frame representing message delivery
        AMQFrame[] frames = new AMQFrame[3];
        frames[0] = wrapBody(createBasicDeliverBody());
        frames[1] = wrapBody(createContentHeaderBody());
        frames[2] = wrapBody(createContentBody(size));

        return new CompositeAMQDataBlock(frames);
    }

    static AMQFrame wrapBody(AMQBody body)
    {
        AMQFrame frame = new AMQFrame(1, body);
        return frame;
    }

    static ContentBody createContentBody(int size)
    {
        ContentBody body = new ContentBody();
        body.payload = ByteBuffer.allocate(size);
        for (int i = 0; i < size; i++)
        {
            body.payload.put((byte) DATA[i % DATA.length]);
        }
        return body;
    }

    static ContentHeaderBody createContentHeaderBody()
    {
        ContentHeaderBody body = new ContentHeaderBody();
        body.properties = new BasicContentHeaderProperties();
        body.weight = 1;
        body.classId = 6;
        return body;
    }

    static BasicDeliverBody createBasicDeliverBody()
    {
        BasicDeliverBody body = new BasicDeliverBody((byte) 8, (byte) 0,
                                                     BasicDeliverBody.getClazz((byte) 8, (byte) 0),
                                                     BasicDeliverBody.getMethod((byte) 8, (byte) 0),                                                     
                                                     new AMQShortString("myConsumerTag"), 1,
                                                     new AMQShortString("myExchange"), false,
                                                     new AMQShortString("myRoutingKey"));
        return body;
    }
}