| 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.object.config.TransparencyClassSpec; 10 import com.tc.simulator.app.ApplicationConfig; 11 import com.tc.simulator.listener.ListenerProvider; 12 import com.tctest.runner.AbstractTransparentApp; 13 14 import java.util.concurrent.CyclicBarrier ; 15 16 public final class TimedChangeReplicationTestApp extends AbstractTransparentApp { 17 18 private static final int SIZE = 100; 19 private static final int VOLUME = 1000; 20 private static final double NANOSEC = 1000000000D; 21 private boolean isLocalWriter; 22 private boolean isSharedReader; 23 private final DataRoot root; 24 private final CyclicBarrier barrier; 25 26 public TimedChangeReplicationTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 27 super(appId, cfg, listenerProvider); 28 barrier = new CyclicBarrier (getParticipantCount()); 29 root = new DataRoot(); 30 } 31 32 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 33 TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier .class.getName()); 34 String className = TimedChangeReplicationTestApp.class.getName(); 35 spec = config.getOrCreateSpec(className); 36 37 config.addIncludePattern(className + "$*"); 38 39 String methodExpression = "* " + className + "*.*(..)"; 40 config.addWriteAutolock(methodExpression); 41 config.addWriteAutolock("* " + CyclicBarrier .class.getName() + "*.*(..)"); 42 config.addWriteAutolock("* " + TimedChangeReplicationTestApp.DataRoot.class.getName() + "increment*(..)"); 43 config.addWriteAutolock("* " + TimedChangeReplicationTestApp.DataRoot.class.getName() + "waitForWriter*(..)"); 44 config.addReadAutolock("* " + TimedChangeReplicationTestApp.DataRoot.class.getName() + "touch*(..)"); 45 46 spec.addRoot("isSharedReader", "isSharedReader"); 47 spec.addRoot("root", "root"); 48 spec.addRoot("barrier", "barrier"); 49 } 50 51 public static void visitDSOApplicationConfig(ConfigVisitor visitor, DSOApplicationConfig config) { 52 config.addIncludePattern(CyclicBarrier .class.getName()); 53 String className = TimedChangeReplicationTestApp.class.getName(); 54 config.addIncludePattern(className); 55 56 config.addIncludePattern(className + "$*"); 57 58 String methodExpression = "* " + className + "*.*(..)"; 59 config.addWriteAutolock(methodExpression); 60 config.addWriteAutolock("* " + CyclicBarrier .class.getName() + "*.*(..)"); 61 config.addWriteAutolock("* " + TimedChangeReplicationTestApp.DataRoot.class.getName() + "increment*(..)"); 62 config.addWriteAutolock("* " + TimedChangeReplicationTestApp.DataRoot.class.getName() + "waitForWriter*(..)"); 63 config.addReadAutolock("* " + TimedChangeReplicationTestApp.DataRoot.class.getName() + "touch*(..)"); 64 65 config.addRoot("isSharedReader", className + ".isSharedReader"); 66 config.addRoot("root", className + ".root"); 67 config.addRoot("barrier", className + ".barrier"); 68 } 69 70 public void run() { 71 System.out.println("root hydrated: " + root); 73 synchronized (root) { 74 if (!isSharedReader) { 75 isSharedReader = true; isLocalWriter = true; 77 } 78 } 79 80 try { 81 replicateChanges(); 82 } catch (Throwable t) { 83 notifyError(t); 84 } 85 } 86 87 private void replicateChanges() throws Throwable { 88 barrier.await(); 89 90 long start = System.nanoTime(); 91 for (int i = 0; i < VOLUME; i++) { 92 if (isLocalWriter) root.increment(); 93 else root.touch(); 94 95 barrier.await(); 96 97 if (isLocalWriter) root.waitForWriter(); 98 } 99 100 if (isLocalWriter) { 101 long end = System.nanoTime(); 102 103 printResult(start, end); 104 } 105 } 106 107 private class DataRoot { 108 109 private boolean waitForWriter = true; 110 private Element[] elements = new Element[SIZE]; 111 112 private DataRoot() { 113 for (int i = 0; i < SIZE; i++) { 114 elements[i] = new Element(); 115 } 116 } 117 118 private synchronized void increment() { 119 for (int i = 0; i < SIZE; i++) { 120 elements[i].counter++; 121 } 122 waitForWriter = false; 123 notifyAll(); 124 } 125 126 private synchronized void touch() throws Exception { 127 while (waitForWriter) 128 wait(); 129 } 131 132 private synchronized void waitForWriter() { 133 waitForWriter = true; 134 } 135 } 136 137 private class Element { 138 private int counter; 139 } 140 141 private void printResult(long start, long end) { 142 double time = (end - start); 143 long result = Math.round(VOLUME / (time / NANOSEC)); 144 System.out.println("**%% TERRACOTTA TEST STATISTICS %%**: value=" + result + " replications/sec with " + SIZE 145 + " field deltas"); 146 } 147 } 148 | Popular Tags |