KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xmladder > LoadTest


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package xmladder;
16
17 import java.io.*;
18 import java.net.*;
19 import java.util.*;
20
21 public class LoadTest {
22     private Agent agents[] = null;
23     private ThreadGroup JavaDoc agentGroup = null;
24     private List times = null;
25
26     public void init(int count, String JavaDoc host, int port) {
27         System.out.print("Starting init..");
28         agentGroup = new ThreadGroup JavaDoc("AgentGroup");
29         times = new ArrayList(count);
30         agents = new Agent[count];
31         for(int i=0;i<count;i++) {
32             agents[i] = new Agent(agentGroup, times, host, port);
33             agents[i].start();
34         }
35         System.out.println(" "+agentGroup.activeCount()+" ready.");
36     }
37
38     public void test() throws InterruptedException JavaDoc {
39         int count = agents.length;
40         int half = count/2;
41
42         System.out.print("Starting test..");
43         long stime = System.currentTimeMillis();
44         long htime = -1;
45         long hcount = -1;
46
47         /*
48         for(int i=0;i<count;i++)
49             agents[i].start();
50         */

51         synchronized(agentGroup) {
52             agentGroup.notifyAll();
53         }
54
55         int ac = times.size();
56         while(ac<count) {
57             if(htime<0 && half>=ac) {
58                 htime = System.currentTimeMillis();
59                 hcount = count-ac;
60             }
61             Thread.sleep(5);
62             ac = times.size();
63         }
64
65         long etime = System.currentTimeMillis();
66         System.out.println("Done\n");
67
68
69         long time = etime - stime;
70         System.out.println("Total Time : "+time+"ms");
71
72         time = time/count;
73         System.out.println("Avg. Time : "+time+"ms\n");
74
75         /*
76         if(htime!=-1) {
77             time = htime - stime;
78             System.out.println("Half Time ("+hcount+"): "+time+"ms");
79             time = time/hcount;
80             System.out.println("Half Avg Time: "+time+"ms");
81         }
82         */

83
84         /*
85         System.out.println("\nEach Client time..\n");
86         for(int i=0;i<count;) {
87             System.out.print(times.get(i)+",");
88             if(++i%10==0) System.out.println("");
89         }
90         */

91     }
92
93     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
94         LoadTest lt = new LoadTest();
95
96         String JavaDoc host = "127.0.0.1";
97         int port = 2222;
98         int count = 50;
99
100         if(args.length>0)
101             count = Integer.parseInt(args[0]);
102         if(args.length>1)
103             host = args[1];
104         if(args.length>2)
105             port = Integer.parseInt(args[2]);
106             
107         lt.init(count, host, port);
108         
109         Thread.sleep(200); //let all thread get ready
110
lt.test();
111     }
112 }
113
114 class Agent extends Thread JavaDoc {
115     private static int count = 0;
116
117     XmlAdderClient client = null;
118     List list = null;
119
120     public Agent(ThreadGroup JavaDoc threadGroup, List list, String JavaDoc host, int port) {
121         super(threadGroup, null, "Agent:"+ ++count);
122         client = new XmlAdderClient(host, port);
123         this.list = list;
124     }
125
126     public void run() {
127         synchronized(getThreadGroup()) {
128             try {
129                 getThreadGroup().wait();
130             } catch(Exception JavaDoc e) {
131                 System.err.println("Error is wait! "+e);
132             }
133         }
134         client.test();
135         list.add(""+client.getTime());
136     }
137 }
138
Popular Tags