1 4 package com.tc.test.server.appserver.load; 5 6 import org.apache.commons.httpclient.HttpClient; 7 8 import java.net.URL ; 9 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 MILLISEC_PER_SEC = 1000; 18 19 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, int appserverID, URL url, List clients, 31 long duration, List 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 48 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.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 e) { 66 throw new RuntimeException (e); 67 } 68 69 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(sleepTime); 81 } catch (InterruptedException e) { 82 throw new RuntimeException (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 e) { 92 throw new RuntimeException (e); 93 } 94 } 95 } 96 | Popular Tags |