KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > performance > generate > load > LinearTransitionLoadGenerator


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.tctest.performance.generate.load;
5
6 import com.tc.util.Assert;
7 import com.tctest.performance.simulate.type.SimulatedType;
8
9 /**
10  * @ThreadSafe
11  */

12 public final class LinearTransitionLoadGenerator implements LoadGenerator {
13
14   private static final int BUFFER_SIZE_SECONDS = 10;
15   private static final int WORKQUEUE_SIZE_SECONDS = 10;
16   private volatile boolean started;
17   private MonitoredWorkQueue workQueue;
18   private Measurement[] waitTimes;
19
20   public synchronized void start(int duration, int minLoad, int maxLoad, SimulatedType factory, int percentUnique) {
21     Assert.assertTrue(!started);
22     started = true;
23     Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
24
25     LoadBuffer buffer = new LoadBuffer(maxLoad * BUFFER_SIZE_SECONDS);
26     workQueue = new MonitoredWorkQueue(maxLoad * WORKQUEUE_SIZE_SECONDS, true);
27
28     LoadProducer producer = new LoadProducer(buffer, duration, minLoad, maxLoad, factory, percentUnique);
29     LoadConsumer consumer = new LoadConsumer(buffer, workQueue);
30
31     producer.start();
32     try {
33       Thread.sleep(10000);
34     } catch (InterruptedException JavaDoc e) {
35       throw new RuntimeException JavaDoc(e);
36     }
37     consumer.start();
38   }
39
40   public Object JavaDoc getNext() throws InterruptedException JavaDoc, WorkQueueOverflowException {
41     Assert.assertTrue(started);
42     try {
43       Object JavaDoc obj = workQueue.get();
44       if (obj == null) waitTimes = workQueue.getWaitTimes();
45       return obj;
46     } catch (WorkQueueOverflowException e) {
47       waitTimes = workQueue.getWaitTimes();
48       throw e;
49     }
50   }
51
52   public Measurement[] getWaitTimes() {
53     return waitTimes;
54   }
55 }
56
Popular Tags