View Javadoc
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: URLTester.java,v 1.7 2003/01/21 22:43:10 michaelh Exp $ 20 */ 21 22 package juju.reattore.loadtest.controller.impl; 23 24 import java.io.IOException; 25 import java.net.URL; 26 import java.nio.channels.*; 27 import java.nio.*; 28 import org.apache.commons.logging.*; 29 30 import juju.reattore.core.reactor.*; 31 import juju.reattore.util.*; 32 33 /*** Very simple URL tester that gets the contents of the given URL 34 from the given socket. 35 */ 36 public class URLTester 37 implements ClientSocketHandler { 38 39 private static Log log = LogFactory.getLog(URLTester.class); 40 41 private static final DurationStat CONNECT_STAT = 42 new DurationStat(URLTester.class, "Connect"); 43 private static final DurationStat WRITE_STAT = 44 new DurationStat(URLTester.class, "Write"); 45 private static final DurationStat READ_STAT = 46 new DurationStat(URLTester.class, "Read"); 47 private static final DurationStat RUN_STAT = 48 new DurationStat(URLTester.class, "Run"); 49 50 private static final CounterStat COMPLETED_STAT = 51 new CounterStat(URLTester.class, "Completed"); 52 private static final CounterStat ERRORS_STAT = 53 new CounterStat(URLTester.class, "Errors"); 54 55 private static RateStat spawnRateStat = 56 new RateStat(URLTester.class, "SpawnRate"); 57 private static RateStat finishRateStat = 58 new RateStat(URLTester.class, "FinishRate"); 59 60 private long runRef; 61 private long connectRef; 62 63 private static final int STATE_CONNECTING = 1; 64 private static final int STATE_WRITING = 2; 65 private static final int STATE_READING = 3; 66 67 private URL url; 68 private SocketChannel ch; 69 private int state = STATE_CONNECTING; 70 71 private ByteBuffer buf = ByteBuffer.allocate(32768); 72 73 /*** Create a new tester for later use. 74 75 @param ch An existing unused socket channel. 76 @param url The URL to connect to later. 77 */ 78 public URLTester(SocketChannel ch, URL url) { 79 this.ch = ch; 80 this.url = url; 81 82 runRef = RUN_STAT.start(); 83 connectRef = CONNECT_STAT.start(); 84 } 85 86 /*** @see ClientSocketHandler */ 87 public boolean handleConnected() 88 throws IOException { 89 90 state = STATE_WRITING; 91 92 CONNECT_STAT.end(connectRef); 93 spawnRateStat.inc(); 94 95 return false; 96 } 97 98 /*** @see ClientSocketHandler */ 99 public boolean handleReadable() 100 throws IOException { 101 102 long ref = READ_STAT.start(); 103 104 try { 105 buf.clear(); 106 int got = ch.read(buf); 107 108 if (got == -1) { 109 ch.close(); 110 111 RUN_STAT.end(runRef); 112 COMPLETED_STAT.inc(); 113 finishRateStat.inc(); 114 return true; 115 } 116 else { 117 return false; 118 } 119 } 120 finally { 121 READ_STAT.end(ref); 122 } 123 } 124 125 /*** @see ClientSocketHandler */ 126 public boolean handleWritable() 127 throws IOException { 128 129 long ref = WRITE_STAT.start(); 130 buf.put("GET / HTTP/1.1\r\n\r\n".getBytes()); 131 buf.flip(); 132 ch.write(buf); 133 134 state = STATE_READING; 135 136 WRITE_STAT.end(ref); 137 138 return false; 139 } 140 141 /*** @see ClientSocketHandler */ 142 public int getInterestOps() { 143 switch (state) { 144 case STATE_CONNECTING: 145 return SelectionKey.OP_CONNECT; 146 case STATE_WRITING: 147 return SelectionKey.OP_WRITE; 148 case STATE_READING: 149 return SelectionKey.OP_READ; 150 default: 151 assert false; 152 return 0; 153 } 154 } 155 156 /*** @see ClientSocketHandler */ 157 public void handleError() { 158 ERRORS_STAT.inc(); 159 } 160 }

This page was automatically generated by Maven