001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.impl;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.apache.commons.logging.LogFactory;
022: import org.apache.commons.logging.Log;
023: import org.directwebremoting.extend.WaitController;
024:
025: /**
026: * @author Joe Walker [joe at getahead dot org]
027: */
028: public class DefaultServerLoadMonitorHarness {
029: public static void main(String[] args) {
030: DefaultServerLoadMonitorHarness test = new DefaultServerLoadMonitorHarness();
031: test.exec();
032: }
033:
034: public void exec() {
035: dslm.setMaxWaitingThreads(100);
036: dslm.setMaxHitsPerSecond(100);
037:
038: List<Client> clients = new ArrayList<Client>();
039:
040: //*
041: for (int i = 0; i < 300; i++) {
042: Client client = new Client(clients.size());
043: client.start();
044: clients.add(client);
045: }
046: //*/
047:
048: try {
049: while (true) {
050: Thread.sleep(5000);
051: Client client = new Client(clients.size());
052: client.start();
053: clients.add(client);
054:
055: float hitsPerSecond = (float) dslm.hitMonitor
056: .getHitsInLastPeriod()
057: / DefaultServerLoadMonitor.SECONDS_MONITORED;
058:
059: log.debug("------------------------------------");
060: log.debug("Num. of clients: " + clients.size());
061: log.debug("Hits per second: " + hitsPerSecond);
062: log.debug("Waiting threads: " + dslm.waitingThreads);
063: log.debug("Disconnect time: " + dslm.disconnectedTime);
064: log.debug("Connected time: " + dslm.connectedTime);
065: }
066: } catch (InterruptedException ex) {
067: log.warn("Interrupted: ", ex);
068: }
069: }
070:
071: private class Client extends Thread {
072: Client(int id) {
073: setName("Client:" + id);
074: }
075:
076: @Override
077: public void run() {
078: try {
079: Thread.sleep((int) (Math.random() * 60000));
080: WaitController waitController = new CustomWaitController();
081:
082: while (true) {
083: dslm.threadWaitStarting(waitController);
084: long connectedTime = dslm.getConnectedTime();
085: waitWithCatch(connectedTime);
086: dslm.threadWaitEnding(waitController);
087: int timeToNextPoll = dslm.getDisconnectedTime();
088: waitWithCatch(timeToNextPoll);
089: }
090: } catch (Exception ex) {
091: log.warn("", ex);
092: } finally {
093: log.warn("Client dying");
094: }
095: }
096:
097: private void waitWithCatch(long waitTime) {
098: if (waitTime == 0) {
099: waitTime = waitTime + 1;
100: }
101:
102: try {
103: synchronized (lock) {
104: lock.wait(waitTime);
105: }
106: } catch (InterruptedException ex) {
107: interrupt();
108: System.out.print('x');
109: System.out.flush();
110: }
111: }
112:
113: protected class CustomWaitController implements WaitController {
114: public void shutdown() {
115: synchronized (lock) {
116: lock.notifyAll();
117: }
118: }
119:
120: public boolean isShutdown() {
121: return false;
122: }
123: }
124:
125: protected final Object lock = new Object();
126: }
127:
128: /**
129: * The log stream
130: */
131: protected static final Log log = LogFactory
132: .getLog(DefaultServerLoadMonitorHarness.class);
133:
134: protected DefaultServerLoadMonitor dslm = new DefaultServerLoadMonitor();
135: }
|