001: /*
002: * $Id: SiteTest.java,v 1.2 2002/02/15 23:44:27 skavish Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash;
052:
053: import java.io.*;
054: import java.util.*;
055: import java.net.*;
056:
057: /**
058: */
059: public final class SiteTest {
060:
061: public static void help() {
062: System.err.println("Site test v1.0");
063: System.err
064: .println("Copyright (c) Dmitry Skavish, 2000. All rights reserved.");
065: System.err.println("");
066: System.err.println("Usage: sitetest [options] <url>");
067: System.err.println("");
068: System.err.println("Options:");
069: System.err
070: .println(" -help displays usage text");
071: System.err
072: .println(" -users <num> number of users (20 default)");
073: System.err.println(" -verbose verbose output");
074: System.err.println("");
075: System.exit(1);
076: }
077:
078: public static void err(String msg) {
079: System.err.println(msg);
080: help();
081: }
082:
083: public static void main(String[] args) throws MalformedURLException {
084:
085: String url = null;
086: int users = 50;
087: boolean verbose = false;
088: // parse options
089: int l = args.length - 1;
090: for (int i = 0; i <= l; i++) {
091: if (args[i].equals("-help")) {
092: help();
093: } else if (args[i].equals("-users")) {
094: if (i + 1 > l)
095: err("Number of users is not specified");
096: users = Integer.parseInt(args[++i]);
097: } else if (args[i].equals("-verbose")) {
098: verbose = true;
099: } else {
100: url = args[i];
101: if (i != l)
102: err("Too many parameters");
103: }
104: }
105:
106: if (url == null)
107: err("Url is not specified");
108:
109: start = System.currentTimeMillis();
110:
111: final boolean myVerbose = verbose;
112: for (int i = 0; i < users; i++) {
113: Thread st = new User(url);
114: st.start();
115: }
116:
117: new Thread() {
118: public void run() {
119: try {
120: for (;;) {
121: Thread.sleep(5000);
122: int size;
123: long time;
124: int num;
125: synchronized (SiteTest.class) {
126: size = totalSize;
127: time = totalTime;
128: num = number;
129: }
130: System.err
131: .println("----------------------------------------------------------------------");
132: System.err.println("total size: " + size
133: + ", total time: " + time
134: + "ms, number: " + num);
135: System.err.println("avg size: " + (size / num)
136: + ", avg time: " + (time / num) + "ms");
137: double nps = (num * 1000.0)
138: / (System.currentTimeMillis() - start);
139: System.err.println("requests per second: "
140: + nps);
141: }
142: } catch (InterruptedException e) {
143: }
144: }
145: }.start();
146:
147: // System.exit(0);
148: }
149:
150: private static long start = 0;
151: private static int totalSize = 0;
152: private static long totalTime = 0;
153: private static int number = 0;
154:
155: public synchronized static void addData(int size, long time) {
156: totalSize += size;
157: totalTime += time;
158: number++;
159: }
160:
161: public static class User extends Thread {
162: private URL url;
163: private byte[] buffer = new byte[4096 * 4];
164:
165: public User(String urlStr) throws MalformedURLException {
166: url = new URL(urlStr);
167: }
168:
169: public void run() {
170: for (;;) {
171: try {
172: long time = System.currentTimeMillis();
173: URLConnection conn = url.openConnection();
174: conn.connect();
175: InputStream is = conn.getInputStream();
176: int this Size = 0;
177: int size;
178: while ((size = is.read(buffer, 0, buffer.length)) > 0) {
179: this Size += size;
180: }
181: time = System.currentTimeMillis() - time;
182: addData(this Size, time);
183: is.close();
184: } catch (IOException e) {
185: err(e.getMessage());
186: }
187: }
188: }
189: }
190: }
|