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.net.ServerSocket;
20: import java.net.Socket;
21: import java.io.InputStream;
22: import java.text.DecimalFormat;
23: import java.math.BigDecimal;
24: import org.apache.catalina.tribes.io.XByteBuffer;
25:
26: public class SocketTribesReceive {
27: static long start = 0;
28: static double mb = 0;
29: //static byte[] buf = new byte[32871];
30: static byte[] buf = new byte[32871];
31: static boolean first = true;
32: static int count = 0;
33: static DecimalFormat df = new DecimalFormat("##.00");
34: static BigDecimal total = new BigDecimal((double) 0);
35: static BigDecimal bytes = new BigDecimal((double) 32871);
36:
37: public static void main(String[] args) throws Exception {
38: int size = 43800;
39: if (args.length > 0)
40: try {
41: size = Integer.parseInt(args[0]);
42: } catch (Exception x) {
43: }
44: XByteBuffer xbuf = new XByteBuffer(43800, true);
45: ServerSocket srvSocket = new ServerSocket(9999);
46: System.out.println("Listening on 9999");
47: Socket socket = srvSocket.accept();
48: socket.setReceiveBufferSize(size);
49: InputStream in = socket.getInputStream();
50: Thread t = new Thread() {
51: public void run() {
52: while (true) {
53: try {
54: Thread.sleep(1000);
55: printStats(start, mb, count, df, total);
56: } catch (Exception x) {
57: }
58: }
59: }
60: };
61: t.setDaemon(true);
62: t.start();
63:
64: while (true) {
65: if (first) {
66: first = false;
67: start = System.currentTimeMillis();
68: }
69: int len = in.read(buf);
70: if (len == -1) {
71: printStats(start, mb, count, df, total);
72: System.exit(1);
73: }
74: xbuf.append(buf, 0, len);
75: if (bytes.intValue() != len)
76: bytes = new BigDecimal((double) len);
77: total = total.add(bytes);
78: while (xbuf.countPackages(true) > 0) {
79: xbuf.extractPackage(true);
80: count++;
81: }
82: mb += ((double) len) / 1024 / 1024;
83: if (((count) % 10000) == 0) {
84: printStats(start, mb, count, df, total);
85: }
86: }
87:
88: }
89:
90: private static void printStats(long start, double mb, int count,
91: DecimalFormat df, BigDecimal total) {
92: long time = System.currentTimeMillis();
93: double seconds = ((double) (time - start)) / 1000;
94: System.out.println("Throughput " + df.format(mb / seconds)
95: + " MB/seconds messages " + count + ", total " + mb
96: + " MB, total " + total + " bytes.");
97: }
98: }
|