KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @LivenessTuned
12  */

13 final class LoadProducer extends Thread JavaDoc {
14
15   private final LoadBuffer loadBuffer;
16   private final SimulatedType factory;
17   private final int percentUnique;
18   private final double duration, minLoad, maxLoad;
19   private long counter;
20   private boolean started;
21
22   LoadProducer(LoadBuffer loadBuffer, int duration, int minLoad, int maxLoad, SimulatedType factory, int percentUnique) {
23     Assert.assertTrue(percentUnique >= 0);
24     Assert.assertTrue(percentUnique <= 100);
25     Assert.assertTrue(minLoad <= maxLoad);
26     Assert.assertTrue(duration > 0);
27     Assert.assertTrue(minLoad > 0);
28     Assert.assertNotNull(factory);
29     this.loadBuffer = loadBuffer;
30     this.duration = duration;
31     this.minLoad = minLoad;
32     this.maxLoad = maxLoad;
33     this.factory = factory;
34     this.percentUnique = percentUnique;
35     this.counter = minLoad;
36     setDaemon(true);
37     setPriority(Thread.MIN_PRIORITY);
38   }
39
40   public synchronized void run() {
41     if (started) throw new IllegalStateException JavaDoc(this.getClass().getName() + " has already been started.");
42     started = true;
43
44     try {
45       activateWorkers();
46       for (int i = 1; i <= duration; i++) {
47         // magic load formula
48
setCounter(Math.round((((maxLoad - minLoad) / duration) * i) + minLoad));
49         while (counter > 0)
50           wait();
51         if (!loadBuffer.put(factory.clone(percentUnique), true)) {
52           setCounter(0);
53           System.err
54               .println("The CPU did not have the available resources to produce the maximum obj/sec specified by the load generator.");
55           return;
56         }
57       }
58     } catch (InterruptedException JavaDoc e) {
59       throw new RuntimeException JavaDoc(e);
60     }
61   }
62
63   private void activateWorkers() {
64     new Worker().start();
65   }
66
67   private synchronized Object JavaDoc getWork() throws InterruptedException JavaDoc {
68     while (counter < 1)
69       wait();
70     --counter;
71     notifyAll();
72     return factory.clone(percentUnique);
73   }
74
75   private synchronized void setCounter(long magnitude) {
76     counter = magnitude;
77     notifyAll();
78   }
79
80   private class Worker extends Thread JavaDoc {
81
82     private Worker() {
83       setDaemon(true);
84     }
85
86     public void run() {
87       try {
88         while (true) {
89           loadBuffer.put(getWork(), false);
90         }
91       } catch (InterruptedException JavaDoc e) {
92         throw new RuntimeException JavaDoc(e);
93       }
94     }
95   }
96 }
97
Popular Tags