KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > StressTester


1 package org.jgroups.tests;
2
3 import java.io.InputStream JavaDoc;
4 import java.net.HttpURLConnection JavaDoc;
5 import java.net.URL JavaDoc;
6
7 /**
8  * Test driver for testing JGroups-based HttpSession replication.
9  *
10  * @author Brian Stansberry
11  * @version $Id: StressTester.java,v 1.4 2006/09/07 18:35:29 belaban Exp $
12  */

13 public class StressTester implements Runnable JavaDoc
14 {
15    private URL JavaDoc url;
16    private String JavaDoc name;
17
18    StressTester(URL JavaDoc url, String JavaDoc name)
19    {
20       this.url = url;
21       this.name = name;
22    }
23
24    public void run()
25    {
26       System.out.println(name + " starting");
27
28       String JavaDoc cookie = null;
29       int count = 0;
30       while (true)
31       {
32          count++;
33
34          try
35          {
36             HttpURLConnection JavaDoc conn = (HttpURLConnection JavaDoc) url.openConnection();
37             if (cookie != null)
38                conn.addRequestProperty("Cookie", cookie);
39             conn.connect();
40
41             int rc = conn.getResponseCode();
42
43             if (cookie == null)
44             {
45                String JavaDoc header = conn.getHeaderField("Set-Cookie");
46                if (header != null)
47                {
48                   int semi = header.indexOf(';');
49                   if (semi > 0)
50                   {
51                      cookie = header.substring(0, semi);
52                   }
53                   else
54                   {
55                      cookie = header;
56                   }
57                }
58             }
59             InputStream JavaDoc is = conn.getInputStream();
60              while (is.read() != -1) {
61                  ;
62              }
63              is.close();
64
65             if (rc != 200 || count % 1000 == 0)
66             {
67                StringBuffer JavaDoc sb = new StringBuffer JavaDoc(name);
68                sb.append('-');
69                sb.append(count);
70                sb.append('-');
71                sb.append(rc);
72                sb.append('-');
73                sb.append(cookie);
74                System.out.println(sb.toString());
75             }
76          }
77          catch (Exception JavaDoc e)
78          {
79             System.out.println(e.getMessage());
80          }
81
82       }
83    }
84
85    /**
86     * Args are:
87     *
88     * 0) # of client threads to create
89     * 1) hostname:port of node 0; e.g 192.168.1.164:8080
90     * 2) hostname:port of node 1
91     * x) and so on for as many servers as there are
92     *
93     * @param args
94     */

95    public static void main(String JavaDoc[] args)
96    {
97       try
98       {
99          int threadCount = Integer.parseInt(args[0]);
100
101          int serverCount = args.length - 1;
102
103          URL JavaDoc[] urls = new URL JavaDoc[serverCount];
104          for (int i = 1; i < args.length; i++)
105          {
106             urls[i -1] = new URL JavaDoc("http://" + args[i] + "/jbento-httpsession/SetListOfString16");
107          }
108
109
110          for (int i = 0; i < threadCount; i++)
111          {
112             StressTester tester = new StressTester(urls[i % serverCount], "Thread"+i);
113             Thread JavaDoc t = new Thread JavaDoc(tester);
114             t.start();
115          }
116       }
117       catch (Exception JavaDoc e)
118       {
119          e.printStackTrace();
120       }
121    }
122
123 }
124
Popular Tags