01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.poweroff;
07:
08: import java.io.DataInputStream;
09: import java.io.IOException;
10: import java.net.ServerSocket;
11: import java.net.Socket;
12:
13: /**
14: * The listener application for the power off test.
15: * The listener runs on a computer that stays on during the whole test.
16: */
17: public class Listener implements Runnable {
18:
19: public static void main(String[] args) throws Exception {
20: new Listener().test(args);
21: }
22:
23: volatile int maxValue;
24:
25: void test(String[] args) throws Exception {
26: int port = 9099;
27: for (int i = 0; i < args.length; i++) {
28: if (args[i].equals("-port")) {
29: port = Integer.parseInt(args[++i]);
30: }
31: }
32: listen(port);
33: }
34:
35: public void run() {
36: while (true) {
37: try {
38: Thread.sleep(10000);
39: } catch (Exception e) {
40: // ignore
41: }
42: System.out.println("Max=" + maxValue);
43: }
44: }
45:
46: void listen(int port) throws Exception {
47: new Thread(this ).start();
48: ServerSocket serverSocket = new ServerSocket(port);
49: System.out.println("Listening on " + serverSocket.toString());
50: long time;
51: maxValue = 0;
52: while (true) {
53: Socket socket = serverSocket.accept();
54: DataInputStream in = new DataInputStream(socket
55: .getInputStream());
56: System.out.println("Connected");
57: time = System.currentTimeMillis();
58: try {
59: while (true) {
60: int value = in.readInt();
61: if (value < 0) {
62: break;
63: }
64: maxValue = Math.max(maxValue, value);
65: }
66: } catch (IOException e) {
67: System.out.println("Closed with Exception: " + e);
68: }
69: time = System.currentTimeMillis() - time;
70: int operationsPerSecond = (int) (1000 * maxValue / time);
71: System.out.println("Max=" + maxValue + " operations/sec="
72: + operationsPerSecond);
73: }
74: }
75:
76: }
|