KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > server > appserver > load > RequestGenerator


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.test.server.appserver.load;
5
6 import org.apache.commons.httpclient.HttpClient;
7
8 import java.net.URL JavaDoc;
9 import java.util.List JavaDoc;
10
11 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
12
13 public class RequestGenerator extends Thread JavaDoc {
14
15   // private static final long NANOSEC_PER_SEC = 1000000000;
16
// private static final long NANOSEC_PER_MILLISEC = 1000000;
17
private static final long MILLISEC_PER_SEC = 1000;
18
19   // generation rate unit: # req/sec
20
private final int generationRate;
21   private final int appserverID;
22   private final URL JavaDoc url;
23   private final LinkedQueue requestQueue;
24   private final Object JavaDoc[] clients;
25   private final long test_duration;
26   private final List JavaDoc requests;
27   private final int clientsPerNode;
28   private final Thread JavaDoc requestQueueHandler;
29
30   public RequestGenerator(int generationRate, int clientsPerNode, int appserverID, URL JavaDoc url, List JavaDoc clients,
31                           long duration, List JavaDoc requests) {
32     this.generationRate = generationRate;
33     this.clientsPerNode = clientsPerNode;
34     this.appserverID = appserverID;
35     this.url = url;
36     this.clients = clients.toArray();
37     this.test_duration = duration;
38     this.requests = requests;
39     this.requestQueue = new LinkedQueue();
40     this.requestQueueHandler = new RequestQueueHandler(this.requestQueue);
41     this.requestQueueHandler.start();
42   }
43
44   public void run() {
45     // nanosec per request
46
// long creationInterval = NANOSEC_PER_SEC / this.generationRate;
47

48     // millisec per request
49
long creationInterval = MILLISEC_PER_SEC / this.generationRate;
50
51     int curIndex = 0;
52     long startTime, endTime, diff, sleepTime, dur = this.test_duration;
53
54     do {
55       // startTime = System.nanoTime();
56
startTime = System.currentTimeMillis();
57
58       Request r = new DataKeeperRequest((HttpClient) this.clients[curIndex], this.appserverID, this.url);
59       curIndex = (curIndex + 1) % clientsPerNode;
60       this.requests.add(r);
61
62       try {
63         r.setEnterQueueTime();
64         this.requestQueue.put(r);
65       } catch (Exception JavaDoc e) {
66         throw new RuntimeException JavaDoc(e);
67       }
68
69       // endTime = System.nanoTime();
70
endTime = System.currentTimeMillis();
71       diff = endTime - startTime;
72       dur -= diff;
73       sleepTime = creationInterval - diff;
74       if (sleepTime < 0) {
75         continue;
76       }
77
78       try {
79         // Thread.sleep(NANO_PER_MILLISEC / sleepTime, (int) (NANO_PER_MILLISEC % sleepTime));
80
Thread.sleep(sleepTime);
81       } catch (InterruptedException JavaDoc e) {
82         throw new RuntimeException JavaDoc(e);
83       }
84
85       dur -= sleepTime;
86     } while (dur > 0);
87
88     try {
89       this.requestQueue.put(new ExitRequest());
90       this.requestQueueHandler.join();
91     } catch (InterruptedException JavaDoc e) {
92       throw new RuntimeException JavaDoc(e);
93     }
94   }
95 }
96
Popular Tags