KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > WorkQueueTestApp


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;
5
6 import com.tc.object.config.ConfigVisitor;
7 import com.tc.object.config.DSOClientConfigHelper;
8 import com.tc.object.config.TransparencyClassSpec;
9 import com.tc.simulator.app.ApplicationConfig;
10 import com.tc.simulator.listener.ListenerProvider;
11 import com.tctest.util.AbstractTransparentAppMultiplexer;
12 import com.tctest.util.DSOConfigUtil;
13 import com.tctest.util.TestUtil;
14 import com.tctest.util.Timer;
15
16 import java.util.concurrent.BlockingQueue JavaDoc;
17 import java.util.concurrent.CyclicBarrier JavaDoc;
18 import java.util.concurrent.LinkedBlockingQueue JavaDoc;
19
20 public class WorkQueueTestApp extends AbstractTransparentAppMultiplexer
21 {
22     private static final int NUM_ITEMS = 100;
23     private static final int SIZE_ITEMS = 10;
24
25     private final BlockingQueue JavaDoc<Object JavaDoc> queue;
26     private final CyclicBarrier JavaDoc readBarrier;
27     private final Object JavaDoc poison;
28
29     private ItemGenerator itemGenerator = new ItemGenerator();
30
31     public static class Item
32     {
33         public byte[] data;
34     }
35
36     public static class ItemGenerator
37     {
38         public Object JavaDoc next()
39         {
40             Item item = new Item();
41             item.data = new byte[SIZE_ITEMS];
42             return item;
43         }
44
45     }
46
47     public WorkQueueTestApp(String JavaDoc appId, ApplicationConfig cfg,
48                     ListenerProvider listenerProvider)
49     {
50         super(appId, cfg, listenerProvider);
51         queue = new LinkedBlockingQueue JavaDoc<Object JavaDoc>();
52         readBarrier = new CyclicBarrier JavaDoc(Math.min(getParticipantCount(), 2));
53         poison = new Object JavaDoc();
54     }
55
56     public void run(CyclicBarrier JavaDoc barrier, int index) throws Throwable JavaDoc
57     {
58         if (index == 0) {
59             doPuts();
60             return;
61         }
62
63         doReads();
64     }
65
66     private void doPuts() throws Exception JavaDoc
67     {
68         Timer t = new Timer();
69
70         System.out.println("Warming up...");
71
72         for (int i = 0; i < NUM_ITEMS; i++) {
73             queue.put(itemGenerator.next());
74         }
75
76         // put the read barrier in the queue so that we
77
// wait for the last warmup item to be read
78
queue.put(readBarrier);
79         readBarrier.await();
80
81         // dump the items
82
System.out.println("Putting items...");
83         int total = NUM_ITEMS*getIntensity();
84         t.start();
85         for (int i = 0; i < total; i++) {
86             queue.put(itemGenerator.next());
87         }
88
89         // put the read barrier in the queue so that we
90
// wait for the last item to be read
91
queue.put(readBarrier);
92         readBarrier.await();
93
94         // stop the timer
95
t.stop();
96
97         // add one more object to the total to account
98
// for the read barrier
99
total++;
100         
101         // send poison to one reader (each reader will requeue it to
102
// kill them all)
103
queue.put(poison);
104         
105         TestUtil.printStats("" + getParticipantCount(), "nodes");
106         TestUtil.printStats("" + total, "transactions");
107         TestUtil.printStats("" + t.elapsed(), "milliseconds");
108         TestUtil.printStats("" + t.tps(total), "tps");
109     }
110
111     private void doReads() throws Exception JavaDoc
112     {
113         System.out.println("Getting items...");
114
115         while (true) {
116             Object JavaDoc item = queue.take();
117             if (item instanceof CyclicBarrier JavaDoc) {
118                 ((CyclicBarrier JavaDoc) item).await();
119                 continue;
120             }
121             if (item == poison) {
122                 break;
123             }
124         }
125
126         queue.put(poison);
127     }
128
129     public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config)
130     {
131         TransparencyClassSpec spec = config.getOrCreateSpec(WorkQueueTestApp.class.getName());
132
133         AbstractTransparentAppMultiplexer.visitL1DSOConfig(visitor, config);
134
135         DSOConfigUtil.autoLockAndInstrumentClass(config, WorkQueueTestApp.class);
136
137         DSOConfigUtil.addRoot(spec, "queue");
138         DSOConfigUtil.addRoot(spec, "readBarrier");
139         DSOConfigUtil.addRoot(spec, "poison");
140     }
141 }
142
Popular Tags