VB icon

WebTest

Email
Submitted on: 1/2/2015 6:40:00 PM
By: nfs (from psc cd)  
Level: Advanced
User Rating: By 3 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 692
 
     So you have built a cool web site with code you wrote or downloaded from great sites like planet-source-code, the next thing you might want to do is testing it: Can your code handle more than, say 10 concurrent users? Does it have a memory leak that will eventually lead to crash if you don't reboot? Here is a java program I wrote that can query a web site continuously. If you want to simulate more than one user, just start as many instances of this program as you need. The command line to run it is: java WebTest "http://www.yahoo.com/" 20000 This will query the www.yahoo.com site continuously, sleeping a random amount of time from 0 up to 20000 milliseconds between consecutive requests. If you omit the second parameter, the default number 10000 will be used.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: WebTest
// Description:So you have built a cool web site with code you wrote or downloaded from great sites like planet-source-code, the next thing you might want to do is testing it: Can your code handle more than, say 10 concurrent users? Does it have a memory leak that will eventually lead to crash if you don't reboot? 
Here is a java program I wrote that can query a web site continuously. If you want to simulate more than one user, just start as many instances of this program as you need. The command line to run it is: 
java WebTest "http://www.yahoo.com/" 20000 
This will query the www.yahoo.com site continuously, sleeping a random amount of time from 0 up to 20000 milliseconds between consecutive requests. If you omit the second parameter, the default number 10000 will be used.
// By: nfs (from psc cd)
//**************************************

import java.net.*; 
import java.io.*; 
import java.util.*; 
public class WebTest 
{ 
public static void main (String[] args) 
{ 
// initialize random seed 
Random randomGen = new Random(new Date().getTime()); 
while(true) 
{ 
try 
{ 
// sleep for a random interval 
Thread.currentThread().sleep(randomGen.nextInt(args.length>1?(new Integer(args[1]).intValue()):10000)); 
if(args.length>0) 
{ 
// print the user supplied URL to the console window 
System.out.println(args[0]); 
// open URL connection 
URLConnection urlConn = new URL(args[0]).openConnection(); 
urlConn.setUseCaches(false); 
// read and print the content to the console window 
InputStream in = urlConn.getInputStream(); 
byte buf[] = new byte[4096]; 
int nSize = in.read(buf); 
while(nSize>=0) 
{ 
System.out.print(new String(buf,0,nSize)); 
nSize = in.read(buf); 
} 
System.out.print("\r\n"); 
} 
else return; 
} 
catch(Exception e) 
{ 
// print error message 
System.out.println("Exception: "+e.getMessage()); 
} 
} 
} 
} 


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.