Click here to Skip to main content
Licence 
First Posted 14 Jun 2001
Views 132,786
Bookmarked 38 times

Stress Testing Your Website

By | 14 Jun 2001 | Article
A simple Java program to test your own website

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 刘向阳



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSet proxy setting if somebody is behind firewall Pinmemberashishk800:48 25 Aug '06  
QuestionWhy I got nothing when using URLConnection to a URL like google? Pinmemberjospeh wang11:49 9 Jul '03  
AnswerRe: Why I got nothing when using URLConnection to a URL like google? PinmemberXiangyang Liu12:48 9 Jul '03  
GeneralRe: Why I got nothing when using URLConnection to a URL like google? Pinmemberjosephwang30@yahoo.com15:02 10 Jul '03  
GeneralRe: Why I got nothing when using URLConnection to a URL like google? PinmemberXiangyang Liu1:16 11 Jul '03  
GeneralRe: Why I got nothing when using URLConnection to a URL like google? Pinmemberrafael.nunes9:06 6 Feb '07  
GeneralVery Bad Approach -- This is NOT a Valid Test! PinmemberTV Mogul4:57 17 Apr '03  
GeneralRe: Very Bad Approach -- This is NOT a Valid Test! PinmemberXiangyang Liu10:11 17 Apr '03  
GeneralRe: Very Bad Approach -- This is NOT a Valid Test! PinmemberAson19:16 28 Jul '03  
GeneralXML Parsing Pinmembervinuk13:05 27 Dec '01  
GeneralRe: XML Parsing PinmemberXiangYangLiu13:18 27 Dec '01  
GeneralRe: XML Parsing PinmemberNemanja Trifunovic13:33 27 Dec '01  
GeneralRe: XML Parsing PinmemberTim Smith14:46 27 Dec '01  
GeneralRe: Why not use microsoft Web Application Stress PinmemberNET2:24 18 Jun '01  
GeneralWhy not use microsoft Web Application Stress PinmemberAnonymous2:09 18 Jun '01  
GeneralRe: Why not use microsoft Web Application Stress PinmemberA.Very PissedOfGeeza5:58 23 Jul '01  
GeneralRe: Why not use microsoft Web Application Stress PinmemberAnonymous8:11 18 Jun '01  
QuestionDoes this work? PinmemberMasaaki Onishi6:06 17 Jun '01  
AnswerYes, it does (Re: Does this work?) PinmemberAnonymous6:42 15 Aug '01  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 15 Jun 2001
Article Copyright 2001 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid