package org.jgroups.tests;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Test driver for testing JGroups-based HttpSession replication.
 * 
 * @author Brian Stansberry
 * @version $Id: StressTester.java,v 1.2 2006/09/05 02:22:08 bstansberry Exp $
 */
public class StressTester implements Runnable
{
   private URL url;
   private String name;

   StressTester(URL url, String name)
   {
      this.url = url;
      this.name = name;
   }

   public void run()
   {
      System.out.println(name + " starting");

      String cookie = null;
      int count = 0;
      while (true)
      {
         count++;

         try
         {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            if (cookie != null)
               conn.addRequestProperty("Cookie", cookie);
            conn.connect();

            int rc = conn.getResponseCode();

            if (cookie == null)
            {
               String header = conn.getHeaderField("Set-Cookie");
               if (header != null)
               {
                  int semi = header.indexOf(';');
                  if (semi > 0)
                  {
                     cookie = header.substring(0, semi);
                  }
                  else
                  {
                     cookie = header;
                  }
               }
            }
            InputStream is = conn.getInputStream();
             while (is.read() != -1) {
                 ;
             }
             is.close();

            if (rc != 200 || count % 100 == 0)
            {
               StringBuffer sb = new StringBuffer(name);
               sb.append('-');
               sb.append(count);
               sb.append('-');
               sb.append(rc);
               sb.append('-');
               sb.append(cookie);
               System.out.println(sb.toString());
            }
         }
         catch (Exception e)
         {
            System.out.println(e.getMessage());
         }

      }
   }

   /**
    * Args are:
    *
    * 1) hostname:port of node 0; e.g 192.168.1.164:8080
    * 2) hostname:port of node 1
    * 3) # of client threads to create
    *
    * @param args
    */
   public static void main(String[] args)
   {
      try
      {
         URL  u1 = new URL("http://" + args[0] + "/jbento-httpsession/SetListOfString16");
         URL  u2 = new URL("http://" + args[1] + "/jbento-httpsession/SetListOfString16");

         int count = Integer.parseInt(args[2]);

         for (int i = 0; i < count; i++)
         {
            StressTester tester = new StressTester((i % 2 == 0 ? u1 : u2), "Thread"+i);
            Thread t = new Thread(tester);
            t.start();
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }

}

@@ -247,17 +247,17 @@
 ava,v 1.
-2
+3
  2006/09
@@ -265,15 +265,15 @@
 05 0
-2:22:08
+3:50:55
  bst
@@ -2063,24 +2063,63 @@
  are:%0A    *%0A
+    * 0) # of client threads to create%0A
     * 1) hos
@@ -2206,39 +2206,52 @@
   * 
-3) # of client threads to creat
+x) and so on for as many servers as there ar
 e%0A  
@@ -2357,106 +2357,232 @@
     
-URL  u1 = new URL(%22http://%22 + args%5B0%5D + %22/jbento-httpsession/SetListOfString16%22);%0A         URL  u2
+int threadCount = Integer.parseInt(args%5B0%5D);%0A%0A         int serverCount = args.length - 1;%0A         %0A         URL%5B%5D urls = new URL%5BserverCount%5D;%0A         for (int i = 1; i %3C args.length; i++)%0A         %7B%0A            urls%5Bi -1%5D
  = n
@@ -2605,17 +2605,17 @@
  + args%5B
-1
+i
 %5D + %22/jb
@@ -2648,25 +2648,24 @@
 String16%22);%0A
-%0A
          int
@@ -2665,46 +2665,19 @@
     
-int count = Integer.parseInt(args%5B2%5D);
+%7D%0A         
 %0A%0A  
@@ -2703,17 +2703,23 @@
  0; i %3C 
-c
+threadC
 ount; i+
@@ -2787,30 +2787,29 @@
 ter(
-(i %25 2 == 0 ? u1 : u2)
+urls%5Bi %25 serverCount%5D
 , %22T

Allow an arbitrary number of servers