1 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 ; 14 import java.util.concurrent.LinkedBlockingQueue ; 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 rootQueue; 21 22 public TimedSingleQueueThroughputTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 23 super(appId, cfg, listenerProvider); 24 rootQueue = new LinkedBlockingQueue (100); 25 } 26 27 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 28 String className = TimedSingleQueueThroughputTestApp.class.getName(); 29 config.addRoot("rootQueue", className + ".rootQueue"); 30 String methodExpression = "* " + className + "*.*(..)"; 31 config.addWriteAutolock(methodExpression); 32 } 33 34 public static void visitDSOApplicationConfig(ConfigVisitor visitor, DSOApplicationConfig config) { 35 String className = TimedSingleQueueThroughputTestApp.class.getName(); 36 config.addRoot("rootQueue", className + ".rootQueue"); 37 String methodExpression = "* " + className + "*.*(..)"; 38 config.addWriteAutolock(methodExpression); 39 } 40 41 public void run() { 42 Thread producer = new Thread () { 43 public void run() { 44 try { 45 for (int i = 0; i < 100; i++) { 46 populate(new Object ()); 47 } 48 long start = System.nanoTime(); 49 for (int i = 0; i < VOLUME; i++) { 50 populate(new Object ()); 51 } 52 long end = System.nanoTime(); 53 for (int i = 0; i < 100; i++) { 54 populate(new Object ()); 55 } 56 57 printResult(start, end); 58 59 } catch (Throwable t) { 60 notifyError(t); 61 } 62 } 63 }; 64 producer.setDaemon(true); 65 66 Thread consumer = new Thread () { 67 public void run() { 68 try { 69 while (true) { 70 retrieve(); 71 } 72 } catch (Throwable 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 t) { 85 notifyError(t); 86 } 87 } 88 89 private void populate(Object data) throws InterruptedException { 90 rootQueue.put(data); 91 } 92 93 private void retrieve() throws InterruptedException { 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 |