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:
25: public class SocketReceive {
26: static long start = 0;
27: static double mb = 0;
28: static byte[] buf = new byte[8192 * 4];
29: static boolean first = true;
30: static int count = 0;
31: static DecimalFormat df = new DecimalFormat("##.00");
32: static BigDecimal total = new BigDecimal(0);
33: static BigDecimal bytes = new BigDecimal(32871);
34:
35: public static void main(String[] args) throws Exception {
36:
37: ServerSocket srvSocket = new ServerSocket(9999);
38: System.out.println("Listening on 9999");
39: Socket socket = srvSocket.accept();
40: socket.setReceiveBufferSize(43800);
41: InputStream in = socket.getInputStream();
42: Thread t = new Thread() {
43: public void run() {
44: while (true) {
45: try {
46: Thread.sleep(1000);
47: printStats(start, mb, count, df, total);
48: } catch (Exception x) {
49: }
50: }
51: }
52: };
53: t.setDaemon(true);
54: t.start();
55:
56: while (true) {
57: if (first) {
58: first = false;
59: start = System.currentTimeMillis();
60: }
61: int len = in.read(buf);
62: if (len == -1) {
63: printStats(start, mb, count, df, total);
64: System.exit(1);
65: }
66: if (bytes.intValue() != len)
67: bytes = new BigDecimal((double) len);
68: total = total.add(bytes);
69: mb += ((double) len) / 1024 / 1024;
70: if (((++count) % 10000) == 0) {
71: printStats(start, mb, count, df, total);
72: }
73: }
74:
75: }
76:
77: private static void printStats(long start, double mb, int count,
78: DecimalFormat df, BigDecimal total) {
79: long time = System.currentTimeMillis();
80: double seconds = ((double) (time - start)) / 1000;
81: System.out.println("Throughput " + df.format(mb / seconds)
82: + " MB/seconds messages " + count + ", total " + mb
83: + " MB, total " + total + " bytes.");
84: }
85: }
|