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: ABTester.java,v 1.3 2003/03/05 04:31:56 michaelh Exp $ 20 */ 21 22 package juju.reattore.perfcap.tester.impl; 23 24 import java.text.MessageFormat; 25 import java.io.*; 26 import java.util.regex.*; 27 28 import org.apache.commons.logging.*; 29 30 import juju.reattore.perfcap.tester.*; 31 32 /*** Tests a HTTP server using ApacheBench. 33 34 @tag abtester 35 @group Tester 36 */ 37 public class ABTester 38 implements Tester { 39 40 private static Log log = LogFactory.getLog(ABTester.class); 41 42 private int concurrent = 10; 43 private int timeLimit = 1; 44 private String url = "http://127.0.0.1:8080/index.html"; 45 private boolean keepAlive = true; 46 47 private String cmd = "ab -c {1,number,#} -t {2,number,#} {3} {0}"; 48 49 /*** Enable using HTTP keep-alive during the test. 50 51 @param argKeepAlive Value to assign to this.keepAlive 52 @default true 53 */ 54 public void setKeepAlive(boolean argKeepAlive) { 55 this.keepAlive = argKeepAlive; 56 } 57 58 /*** Alternative way of enabling keep-alive. Use with an integer 59 variable that changes between 0 and non-zero. 60 61 @param val 0 for off, non-zero for on. 62 @default 1 63 */ 64 public void setKeepAlive(int val) { 65 this.keepAlive = val != 0; 66 } 67 68 /*** The number of concurrent connections to use with the server. 69 70 @param argConcurrent Value to assign to this.concurrent 71 @default 10 72 */ 73 public void setConcurrent(int argConcurrent) { 74 this.concurrent = argConcurrent; 75 } 76 77 /*** Time in seconds to run the test for. 78 79 @param argTimeLimit Value to assign to this.timeLimit 80 @default 1 81 */ 82 public void setTimeLimit(int argTimeLimit) { 83 this.timeLimit = argTimeLimit; 84 } 85 86 /*** The URL to test against. 87 88 @param argUrl Value to assign to this.url 89 @default http://127.0.0.1:8080/index.html 90 */ 91 public void setUrl(String argUrl) { 92 this.url = argUrl; 93 } 94 95 /*** The command to use to run ApacheBench, in Message format 96 form. The arguments are: 97 98 <ul> 99 <li><tt>0</tt> - The URL</li> 100 <li><tt>1</tt> - Concurrent connections</li> 101 <li><tt>2</tt> - Run time</li> 102 <li><tt>3</tt> - '-k' if keep-alive should be used.</li> 103 </ul> 104 105 @param cmd The command to run. 106 @default ab -c {1,number,#} -t {2,number,#} {3} {0} 107 */ 108 public void setCmd(String cmd) { 109 this.cmd = cmd; 110 } 111 112 private static void dump(InputStream from) 113 throws Exception { 114 115 StringBuffer out = new StringBuffer(); 116 117 BufferedReader in = new BufferedReader(new InputStreamReader(from)); 118 119 String got; 120 while ((got = in.readLine()) != null) { 121 out.append(got); 122 out.append("\n"); 123 } 124 125 log.error("Read:\n" + out.toString()); 126 } 127 128 /*** @see Tester */ 129 public Results go() 130 throws Exception { 131 132 String toRun = MessageFormat.format(cmd, 133 new Object[] { 134 url, 135 new Integer(concurrent), 136 new Integer(timeLimit), 137 keepAlive ? "-k" : "" 138 }); 139 140 log.info("Running " + toRun); 141 142 try { 143 Process proc = Runtime.getRuntime().exec(toRun); 144 145 int retCode = proc.waitFor(); 146 147 if (retCode != 0) { 148 dump(proc.getInputStream()); 149 dump(proc.getErrorStream()); 150 throw new IOException("Process '" + toRun + "' returned " + retCode); 151 } 152 153 BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); 154 155 String got; 156 Pattern pat = Pattern.compile("^([a-zA-z //-]+)://s+(//S+).*"); 157 158 ABResults ret = new ABResults(); 159 160 while ((got = in.readLine()) != null) { 161 Matcher mat = pat.matcher(got); 162 163 if (mat.matches()) { 164 ret.add(mat.group(1), mat.group(2)); 165 } 166 } 167 in.close(); 168 169 return ret; 170 } 171 catch (Exception ex) { 172 log.error("Threw", ex); 173 return new ABResults(); 174 } 175 } 176 }

This page was automatically generated by Maven