1 /* Reattore HTTP Server
2
3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 $Id: BaseController.java,v 1.6 2003/02/02 05:29:23 michaelh Exp $
20 */
21
22 package juju.reattore.loadtest.controller.impl;
23
24 import java.io.IOException;
25 import java.net.*;
26 import java.nio.channels.*;
27 import org.apache.commons.logging.*;
28
29 import juju.reattore.loadtest.controller.*;
30 import juju.reattore.core.reactor.*;
31 import juju.reattore.util.*;
32
33 /*** Base implementation of a controller.
34 */
35 public class BaseController
36 implements Controller {
37
38 private static Log log = LogFactory.getLog(BaseController.class);
39
40 private static DurationStat openStat =
41 new DurationStat(BaseController.class, "Open");
42
43 private Chooser chooser;
44 private ClientSocketReactor reactor;
45 private int errors;
46
47 /*** Create a new controller.
48
49 @param chooser The chooser to pull URLs from
50 @param reactor The reactor to attach test clients to.
51 */
52 public BaseController(Chooser chooser, ClientSocketReactor reactor) {
53 this.chooser = chooser;
54 this.reactor = reactor;
55 }
56
57 private static final double getNow() {
58 return (double)System.currentTimeMillis() / 1000.0;
59 }
60
61 /*** @see Controller */
62 public void go(double perSec, double runTime) {
63 go((int)(runTime * perSec), 1 / perSec);
64 }
65
66 /*** @see Controller */
67 public void go(int num, double interval) {
68 assert num >= 0;
69 assert interval >= 0;
70
71 /* We assume that somehow the reactor is running away. */
72 double next = getNow();
73
74 do {
75 double now = getNow();
76
77 if (now >= next) {
78 URL toTest = chooser.getNext();
79 next += interval;
80
81 try {
82 /* Create and connect the socket */
83 long ref = openStat.start();
84
85 SocketChannel ch = SocketChannel.open();
86 ch.configureBlocking(false);
87 openStat.end(ref);
88
89 reactor.connect(ch,
90 new InetSocketAddress(toTest.getHost(), toTest.getPort()),
91 new URLTester(ch, toTest));
92 }
93 catch (IOException ex) {
94 log.error("Unable to connect", ex);
95 errors++;
96 }
97
98 num--;
99 }
100
101 now = getNow();
102 /* Sleep until the next client is ready */
103 if (now < next) {
104 try {
105 Thread.sleep((long)((next - now) * 1000));
106 }
107 catch (InterruptedException ex) {
108 /* Eh? */
109 log.debug("Woken up but didn't expect to be", ex);
110 }
111 }
112
113 } while (num > 0);
114 }
115 }
This page was automatically generated by Maven