KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > performance > timedtask > TimedSingleQueueThroughputTestApp


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.timedtask;
5
6 import com.tc.object.config.ConfigVisitor;
7 import com.tc.object.config.DSOApplicationConfig;
8 import com.tc.object.config.DSOClientConfigHelper;
9 import com.tc.simulator.app.ApplicationConfig;
10 import com.tc.simulator.listener.ListenerProvider;
11 import com.tctest.runner.AbstractTransparentApp;
12
13 import java.util.concurrent.BlockingQueue JavaDoc;
14 import java.util.concurrent.LinkedBlockingQueue JavaDoc;
15
16 public final class TimedSingleQueueThroughputTestApp extends AbstractTransparentApp {
17
18   private static final int VOLUME = 10000;
19   private static final double NANOSEC = 1000000000D;
20   private final BlockingQueue JavaDoc rootQueue;
21
22   public TimedSingleQueueThroughputTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
23     super(appId, cfg, listenerProvider);
24     rootQueue = new LinkedBlockingQueue JavaDoc(100);
25   }
26
27   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
28     String JavaDoc className = TimedSingleQueueThroughputTestApp.class.getName();
29     config.addRoot("rootQueue", className + ".rootQueue");
30     String JavaDoc methodExpression = "* " + className + "*.*(..)";
31     config.addWriteAutolock(methodExpression);
32   }
33   
34   public static void visitDSOApplicationConfig(ConfigVisitor visitor, DSOApplicationConfig config) {
35     String JavaDoc className = TimedSingleQueueThroughputTestApp.class.getName();
36     config.addRoot("rootQueue", className + ".rootQueue");
37     String JavaDoc methodExpression = "* " + className + "*.*(..)";
38     config.addWriteAutolock(methodExpression);
39   }
40
41   public void run() {
42     Thread JavaDoc producer = new Thread JavaDoc() {
43       public void run() {
44         try {
45           for (int i = 0; i < 100; i++) {
46             populate(new Object JavaDoc());
47           }
48           long start = System.nanoTime();
49           for (int i = 0; i < VOLUME; i++) {
50             populate(new Object JavaDoc());
51           }
52           long end = System.nanoTime();
53           for (int i = 0; i < 100; i++) {
54             populate(new Object JavaDoc());
55           }
56
57           printResult(start, end);
58
59         } catch (Throwable JavaDoc t) {
60           notifyError(t);
61         }
62       }
63     };
64     producer.setDaemon(true);
65
66     Thread JavaDoc consumer = new Thread JavaDoc() {
67       public void run() {
68         try {
69           while (true) {
70             retrieve();
71           }
72         } catch (Throwable JavaDoc t) {
73           notifyError(t);
74         }
75       }
76     };
77     consumer.setDaemon(true);
78
79     producer.start();
80     consumer.start();
81
82     try {
83       producer.join();
84     } catch (Throwable JavaDoc t) {
85       notifyError(t);
86     }
87   }
88
89   private void populate(Object JavaDoc data) throws InterruptedException JavaDoc {
90     rootQueue.put(data);
91   }
92
93   private void retrieve() throws InterruptedException JavaDoc {
94     rootQueue.take();
95   }
96
97   private void printResult(long start, long end) {
98     double time = (end - start);
99     long result = Math.round(VOLUME / (time / NANOSEC));
100     System.out.println("**%% TERRACOTTA TEST STATISTICS %%**: value=" + result + " obj/sec");
101   }
102 }
103
Popular Tags