| 1 | |
package org.webslinger.containers; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.PrintStream; |
| 6 | |
import java.net.URL; |
| 7 | |
import java.net.URLClassLoader; |
| 8 | |
import java.util.Collection; |
| 9 | |
import java.util.Hashtable; |
| 10 | |
import java.util.List; |
| 11 | |
|
| 12 | |
import gnu.getopt.LongOpt; |
| 13 | |
import gnu.getopt.Getopt; |
| 14 | |
|
| 15 | |
import com.meterware.servletunit.InvocationContext; |
| 16 | |
import com.meterware.servletunit.ServletRunner; |
| 17 | |
import com.meterware.servletunit.ServletUnitClient; |
| 18 | |
import com.meterware.httpunit.ClientProperties; |
| 19 | |
import com.meterware.httpunit.GetMethodWebRequest; |
| 20 | |
import com.meterware.httpunit.WebResponse; |
| 21 | |
|
| 22 | |
import org.webslinger.servlet.WebslingerServlet; |
| 23 | |
import org.webslinger.servlet.WebslingerServletContextFactoryHardcoded; |
| 24 | |
|
| 25 | |
import org.webslinger.launcher.AbstractGnuoptContainer; |
| 26 | |
import org.webslinger.launcher.Container; |
| 27 | |
|
| 28 | |
public class RunContainer extends AbstractGnuoptContainer { |
| 29 | |
private String mountPath; |
| 30 | 0 | private String hostName = "localhost"; |
| 31 | 0 | private ServletRunner runner = new ServletRunner(); |
| 32 | 0 | private ServletUnitClient suc = runner.newClient(); |
| 33 | 0 | private ClientProperties cp = suc.getClientProperties(); |
| 34 | |
private int count; |
| 35 | |
private String path; |
| 36 | |
|
| 37 | 0 | public RunContainer() { |
| 38 | 0 | } |
| 39 | |
|
| 40 | |
public void setMountPath(String mountPath) { |
| 41 | 0 | this.mountPath = mountPath; |
| 42 | 0 | } |
| 43 | |
|
| 44 | |
public String getMountPath() { |
| 45 | 0 | return mountPath; |
| 46 | |
} |
| 47 | |
|
| 48 | |
public void setHostName(String hostName) { |
| 49 | 0 | this.hostName = hostName; |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
public String getHostName() { |
| 53 | 0 | return hostName; |
| 54 | |
} |
| 55 | |
|
| 56 | |
public void setCount(int count) { |
| 57 | 0 | this.count = count; |
| 58 | 0 | } |
| 59 | |
|
| 60 | |
public int getCount() { |
| 61 | 0 | return count; |
| 62 | |
} |
| 63 | |
|
| 64 | |
public void setPath(String path) { |
| 65 | 0 | this.path = path; |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public String getPath() { |
| 69 | 0 | return path; |
| 70 | |
} |
| 71 | |
|
| 72 | |
public void showHelp(PrintStream out) throws IOException { |
| 73 | 0 | out.println("run help"); |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
protected Getopt getGetopt(String[] args) { |
| 77 | 0 | LongOpt[] longOpts = new LongOpt[] { |
| 78 | |
new LongOpt("mount-path", LongOpt.REQUIRED_ARGUMENT, null, 'm'), |
| 79 | |
new LongOpt("count", LongOpt.REQUIRED_ARGUMENT, null, 'c'), |
| 80 | |
new LongOpt("hostname", LongOpt.REQUIRED_ARGUMENT, null, 'H'), |
| 81 | |
}; |
| 82 | 0 | return new Getopt("webslinger=run", args, "H:m:c:", longOpts); |
| 83 | |
} |
| 84 | |
|
| 85 | |
protected int processOneArg(int c, Getopt g) { |
| 86 | 0 | switch (c) { |
| 87 | |
case 'H': |
| 88 | 0 | setHostName(g.getOptarg()); |
| 89 | 0 | break; |
| 90 | |
case 'm': |
| 91 | 0 | setMountPath(g.getOptarg()); |
| 92 | 0 | break; |
| 93 | |
case 'c': |
| 94 | 0 | setCount(Integer.parseInt(g.getOptarg())); |
| 95 | 0 | return -1; |
| 96 | |
default: |
| 97 | |
} |
| 98 | 0 | return 0; |
| 99 | |
} |
| 100 | |
|
| 101 | |
protected boolean processRest(List<String> rest) { |
| 102 | 0 | setPath(rest.remove(0)); |
| 103 | 0 | return true; |
| 104 | |
} |
| 105 | |
|
| 106 | |
public void init() throws Exception { |
| 107 | 0 | Hashtable<String, String> params = new Hashtable<String, String>(); |
| 108 | 0 | params.put("mount-path", getMountPath()); |
| 109 | 0 | params.put(WebslingerServlet.class.getName() + ".WebslingerServletContextFactory", WebslingerServletContextFactoryHardcoded.class.getName()); |
| 110 | 0 | runner.registerServlet("/*", WebslingerServlet.class.getName(), params); |
| 111 | 0 | cp.setAcceptCookies(false); |
| 112 | 0 | cp.setAcceptGzip(false); |
| 113 | 0 | cp.setAutoRedirect(false); |
| 114 | 0 | cp.setAutoRefresh(false); |
| 115 | 0 | cp.setIframeSupported(false); |
| 116 | 0 | } |
| 117 | |
|
| 118 | |
public void run() throws Exception { |
| 119 | 0 | GetMethodWebRequest gmwr = new GetMethodWebRequest("http://" + getHostName() + "/" + getPath()); |
| 120 | 0 | WebResponse res = suc.getResponse(gmwr); |
| 121 | 0 | if (res.getContentType().startsWith("text/")) { |
| 122 | 0 | System.out.println(res.getText()); |
| 123 | |
} |
| 124 | 0 | if (getCount() != 0) { |
| 125 | 0 | int count = getCount(); |
| 126 | 0 | long start = System.currentTimeMillis(); |
| 127 | 0 | for (int i = 0; i < count; i++) { |
| 128 | 0 | suc.newInvocation(gmwr).service(); |
| 129 | |
} |
| 130 | 0 | long end = System.currentTimeMillis(); |
| 131 | 0 | System.err.println("count=" + count + ", time=" + (end - start) + ", pages/sec=" + ((float) count / (end - start) * 1000)); |
| 132 | |
} |
| 133 | 0 | } |
| 134 | |
} |