01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.catalina.tribes.test.transport;
18:
19: import java.io.OutputStream;
20: import java.net.Socket;
21: import java.text.DecimalFormat;
22: import org.apache.catalina.tribes.membership.MemberImpl;
23: import org.apache.catalina.tribes.io.XByteBuffer;
24: import org.apache.catalina.tribes.Member;
25: import org.apache.catalina.tribes.io.ChannelData;
26: import org.apache.catalina.tribes.Channel;
27: import java.math.BigDecimal;
28:
29: public class SocketSend {
30:
31: public static void main(String[] args) throws Exception {
32:
33: Member mbr = new MemberImpl("localhost", 9999, 0);
34: ChannelData data = new ChannelData();
35: data.setOptions(Channel.SEND_OPTIONS_BYTE_MESSAGE);
36: data.setAddress(mbr);
37: byte[] buf = new byte[8192 * 4];
38: data.setMessage(new XByteBuffer(buf, false));
39: buf = XByteBuffer.createDataPackage(data);
40: int len = buf.length;
41: System.out.println("Message size:" + len + " bytes");
42: BigDecimal total = new BigDecimal((double) 0);
43: BigDecimal bytes = new BigDecimal((double) len);
44: Socket socket = new Socket("localhost", 9999);
45: System.out.println("Writing to 9999");
46: OutputStream out = socket.getOutputStream();
47: long start = 0;
48: double mb = 0;
49: boolean first = true;
50: int count = 0;
51: DecimalFormat df = new DecimalFormat("##.00");
52: while (count < 1000000) {
53: if (first) {
54: first = false;
55: start = System.currentTimeMillis();
56: }
57: out.write(buf, 0, buf.length);
58: mb += ((double) buf.length) / 1024 / 1024;
59: total = total.add(bytes);
60: if (((++count) % 10000) == 0) {
61: long time = System.currentTimeMillis();
62: double seconds = ((double) (time - start)) / 1000;
63: System.out.println("Throughput "
64: + df.format(mb / seconds)
65: + " MB/seconds messages " + count + ", total "
66: + mb + " MB, total " + total + " bytes.");
67: }
68: }
69: out.flush();
70: System.out.println("Complete, sleeping 5 seconds");
71: Thread.sleep(5000);
72:
73: }
74: }
|