1 50 51 package org.openlaszlo.iv.flash; 52 53 import java.io.*; 54 import java.util.*; 55 import java.net.*; 56 57 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 msg ) { 76 System.err.println( msg ); 77 help(); 78 } 79 80 public static void main( String [] args ) throws MalformedURLException { 81 82 String url = null; 83 int users = 50; 84 boolean verbose = false; 85 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 st = new User( url ); 108 st.start(); 109 } 110 111 new Thread () { 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 e ) { 131 } 132 } 133 }.start(); 134 135 } 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 { 150 private URL url; 151 private byte[] buffer = new byte[4096*4]; 152 153 public User( String 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 |