01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.test.server.appserver.load;
05:
06: import org.apache.commons.httpclient.HttpClient;
07:
08: import java.net.URL;
09: import java.util.List;
10:
11: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
12:
13: public class RequestGenerator extends Thread {
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 url;
23: private final LinkedQueue requestQueue;
24: private final Object[] clients;
25: private final long test_duration;
26: private final List requests;
27: private final int clientsPerNode;
28: private final Thread requestQueueHandler;
29:
30: public RequestGenerator(int generationRate, int clientsPerNode,
31: int appserverID, URL url, List clients, long duration,
32: List requests) {
33: this .generationRate = generationRate;
34: this .clientsPerNode = clientsPerNode;
35: this .appserverID = appserverID;
36: this .url = url;
37: this .clients = clients.toArray();
38: this .test_duration = duration;
39: this .requests = requests;
40: this .requestQueue = new LinkedQueue();
41: this .requestQueueHandler = new RequestQueueHandler(
42: this .requestQueue);
43: this .requestQueueHandler.start();
44: }
45:
46: public void run() {
47: // nanosec per request
48: // long creationInterval = NANOSEC_PER_SEC / this.generationRate;
49:
50: // millisec per request
51: long creationInterval = MILLISEC_PER_SEC / this .generationRate;
52:
53: int curIndex = 0;
54: long startTime, endTime, diff, sleepTime, dur = this .test_duration;
55:
56: do {
57: // startTime = System.nanoTime();
58: startTime = System.currentTimeMillis();
59:
60: Request r = new DataKeeperRequest(
61: (HttpClient) this .clients[curIndex],
62: this .appserverID, this .url);
63: curIndex = (curIndex + 1) % clientsPerNode;
64: this .requests.add(r);
65:
66: try {
67: r.setEnterQueueTime();
68: this .requestQueue.put(r);
69: } catch (Exception e) {
70: throw new RuntimeException(e);
71: }
72:
73: // endTime = System.nanoTime();
74: endTime = System.currentTimeMillis();
75: diff = endTime - startTime;
76: dur -= diff;
77: sleepTime = creationInterval - diff;
78: if (sleepTime < 0) {
79: continue;
80: }
81:
82: try {
83: // Thread.sleep(NANO_PER_MILLISEC / sleepTime, (int) (NANO_PER_MILLISEC % sleepTime));
84: Thread.sleep(sleepTime);
85: } catch (InterruptedException e) {
86: throw new RuntimeException(e);
87: }
88:
89: dur -= sleepTime;
90: } while (dur > 0);
91:
92: try {
93: this .requestQueue.put(new ExitRequest());
94: this .requestQueueHandler.join();
95: } catch (InterruptedException e) {
96: throw new RuntimeException(e);
97: }
98: }
99: }
|