5,781,349 members and growing! (216 online)
Email Password   helpLost your password?
Web Development » Applications & Tools » General     Beginner

Stress Testing Your Website

By Xiangyang Liu 刘向阳

A simple Java program to test your own website
Java, Java, NT4, Win2K, Windows, Dev

Posted: 14 Jun 2001
Updated: 14 Jun 2001
Views: 98,928
Bookmarked: 27 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
31 votes for this Article.
Popularity: 6.33 Rating: 4.24 out of 5
3 votes, 50.0%
1
1 vote, 16.7%
2
0 votes, 0.0%
3
1 vote, 16.7%
4
1 vote, 16.7%
5

Introduction

So you have built a cool web site with code you wrote or downloaded from great sites like CodeProject, 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?

Using Java For Stress Testing

Here is a simple 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 XYWebTest the_web_page number_of_milliseconds

The second parameter, number_of_milliseconds, is used to determine a random time interval to sleep/pause between two requests. For example, the following command

java XYWebTest "http://www.yahoo.com/"  20000

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. If you put the following commands in a batch file and save it in the same directory as the class file, then you will be able to run multiple instances of this program by double clicking the batch file:

set ClassPath=.
start java XYWebTest "www.somesite.com" 60000
start java XYWebTest "www.somesite.com" 60000
start java XYWebTest "www.somesite.com" 60000
start java XYWebTest "www.somesite.com" 60000
start java XYWebTest "www.somesite.com" 60000

The java code is simple. You can use or modify it freely. I don't pretend to be an expert in this field, I am sure if you look around, you can find some other code or tools that do exactly the same thing.

Disclaimer: You are supposed to test only your own web site with this code.  I am not responsible for anything you do.  By the way, the code won't work if you need to go through a proxy server to access web sites outside your company firewall.

Please visit my home page for my other articles and software tools.  Thank you.

The Code

//************** XYWebTest.java *********************** 


import java.net.*; 
import java.io.*; 
import java.util.*; 

public class XYWebTest 
{ 
  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()); 
      } 
    } 
  } 
} 

//******************* end ******************************** 


License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Xiangyang Liu 刘向阳



Location: United States United States

Other popular Applications & Tools articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralSet proxy setting if somebody is behind firewallmemberashishk801:48 25 Aug '06  
GeneralWhy I got nothing when using URLConnection to a URL like google?memberjospeh wang12:49 9 Jul '03  
GeneralRe: Why I got nothing when using URLConnection to a URL like google?memberXiangyang Liu13:48 9 Jul '03  
GeneralRe: Why I got nothing when using URLConnection to a URL like google?memberjosephwang30@yahoo.com16:02 10 Jul '03  
GeneralRe: Why I got nothing when using URLConnection to a URL like google?memberXiangyang Liu2:16 11 Jul '03  
GeneralRe: Why I got nothing when using URLConnection to a URL like google?memberrafael.nunes10:06 6 Feb '07  
GeneralVery Bad Approach -- This is NOT a Valid Test!memberTV Mogul5:57 17 Apr '03  
GeneralRe: Very Bad Approach -- This is NOT a Valid Test!memberXiangyang Liu11:11 17 Apr '03  
GeneralRe: Very Bad Approach -- This is NOT a Valid Test!memberAson20:16 28 Jul '03  
GeneralXML Parsingmembervinuk14:05 27 Dec '01  
GeneralRe: XML ParsingmemberXiangYangLiu14:18 27 Dec '01  
GeneralRe: XML ParsingmemberNemanja Trifunovic14:33 27 Dec '01  
GeneralRe: XML ParsingmemberTim Smith15:46 27 Dec '01  
GeneralRe: Why not use microsoft Web Application StressmemberNET3:24 18 Jun '01  
GeneralWhy not use microsoft Web Application StressmemberAnonymous3:09 18 Jun '01  
GeneralRe: Why not use microsoft Web Application StressmemberA.Very PissedOfGeeza6:58 23 Jul '01  
GeneralRe: Why not use microsoft Web Application StressmemberAnonymous9:11 18 Jun '01  
GeneralDoes this work?memberMasaaki Onishi7:06 17 Jun '01  
GeneralYes, it does (Re: Does this work?)memberAnonymous7:42 15 Aug '01  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Jun 2001
Editor: Marc Clifton
Copyright 2001 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2009
Java | Advertise on the Code Project