KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > SiteTest


1 /*
2  * $Id: SiteTest.java,v 1.2 2002/02/15 23:44:27 skavish Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash;
52
53 import java.io.*;
54 import java.util.*;
55 import java.net.*;
56
57 /**
58  */

59 public final class SiteTest {
60
61     public static void help() {
62         System.err.println( "Site test v1.0" );
63         System.err.println( "Copyright (c) Dmitry Skavish, 2000. All rights reserved." );
64         System.err.println( "" );
65         System.err.println( "Usage: sitetest [options] <url>" );
66         System.err.println( "" );
67         System.err.println( "Options:" );
68         System.err.println( " -help displays usage text" );
69         System.err.println( " -users <num> number of users (20 default)" );
70         System.err.println( " -verbose verbose output" );
71         System.err.println( "" );
72         System.exit(1);
73     }
74
75     public static void err( String JavaDoc msg ) {
76         System.err.println( msg );
77         help();
78     }
79
80     public static void main( String JavaDoc[] args ) throws MalformedURLException {
81
82         String JavaDoc url = null;
83         int users = 50;
84         boolean verbose = false;
85         // parse options
86
int l = args.length-1;
87         for( int i=0; i<=l; i++ ) {
88             if( args[i].equals("-help") ) {
89                 help();
90             } else if( args[i].equals("-users") ) {
91                 if( i+1 > l ) err( "Number of users is not specified" );
92                 users = Integer.parseInt( args[++i] );
93             } else if( args[i].equals("-verbose") ) {
94                 verbose = true;
95             } else {
96                 url = args[i];
97                 if( i != l ) err( "Too many parameters" );
98             }
99         }
100
101         if( url == null ) err( "Url is not specified" );
102
103         start = System.currentTimeMillis();
104
105         final boolean myVerbose = verbose;
106         for( int i=0; i<users; i++ ) {
107             Thread JavaDoc st = new User( url );
108             st.start();
109         }
110
111         new Thread JavaDoc() {
112             public void run() {
113                 try {
114                     for(;;) {
115                         Thread.sleep(5000);
116                         int size;
117                         long time;
118                         int num;
119                         synchronized( SiteTest.class ) {
120                             size = totalSize;
121                             time = totalTime;
122                             num = number;
123                         }
124                         System.err.println( "----------------------------------------------------------------------" );
125                         System.err.println( "total size: "+size+", total time: "+time+"ms, number: "+num );
126                         System.err.println( "avg size: "+(size/num)+", avg time: "+(time/num)+"ms" );
127                         double nps = (num*1000.0)/(System.currentTimeMillis()-start);
128                         System.err.println( "requests per second: "+nps );
129                     }
130                 } catch( InterruptedException JavaDoc e ) {
131                 }
132             }
133         }.start();
134
135 // System.exit(0);
136
}
137
138     private static long start = 0;
139     private static int totalSize = 0;
140     private static long totalTime = 0;
141     private static int number = 0;
142
143     public synchronized static void addData( int size, long time ) {
144         totalSize += size;
145         totalTime += time;
146         number++;
147     }
148
149     public static class User extends Thread JavaDoc {
150         private URL url;
151         private byte[] buffer = new byte[4096*4];
152
153         public User( String JavaDoc urlStr ) throws MalformedURLException {
154             url = new URL( urlStr );
155         }
156
157         public void run() {
158             for(;;) {
159                 try {
160                     long time = System.currentTimeMillis();
161                     URLConnection conn = url.openConnection();
162                     conn.connect();
163                     InputStream is = conn.getInputStream();
164                     int thisSize = 0;
165                     int size;
166                     while( (size=is.read(buffer, 0, buffer.length))>0 ) {
167                         thisSize += size;
168                     }
169                     time = System.currentTimeMillis() - time;
170                     addData( thisSize, time );
171                     is.close();
172                 } catch( IOException e ) {
173                     err( e.getMessage() );
174                 }
175             }
176         }
177     }
178 }
179
Popular Tags