1 22 package org.jboss.tm.recovery.test; 23 24 import org.jboss.tm.recovery.BatchRecoveryLogger; 25 import org.jboss.tm.recovery.BatchWriter; 26 import org.jboss.tm.recovery.RecoveryLogTerminator; 27 import org.jboss.tm.XidFactory; 28 29 import java.io.RandomAccessFile ; 30 import java.io.IOException ; 31 import java.io.File ; 32 import java.nio.channels.FileChannel ; 33 import java.nio.ByteBuffer ; 34 35 import EDU.oswego.cs.dl.util.concurrent.Latch; 36 37 import javax.transaction.xa.Xid ; 38 39 45 public class TestForceTime 46 { 47 private static final int RECORD_SIZE = Xid.MAXGTRIDSIZE * 2; 48 private static int NUM_DIRS = 2; 49 private static final int ITERATIONS = 100; 50 private static int numThreads = 500; 51 private static long average; 52 private static int count; 53 private static long startTime; 54 private static XidFactory factory = new XidFactory(); 55 private static int tx_per_sec; 56 57 58 private static class BatchThread implements Runnable 59 { 60 private BatchRecoveryLogger logger; 61 private int id; 62 63 public BatchThread(int id, BatchRecoveryLogger logger) 64 { 65 this.logger = logger; 66 this.id = id; 67 } 68 69 public void run() 70 { 71 synchronized (startLock) 72 { 73 try 74 { 75 startLock.wait(); 76 } 77 catch (InterruptedException e) 78 { 79 throw new RuntimeException (e); 80 } 81 } 82 83 try 84 { 85 87 long start = System.currentTimeMillis(); 88 for (int i = 0; i < ITERATIONS; i++) 89 { 90 Xid xid = factory.newXid(); 91 RecoveryLogTerminator term = logger.committing(xid); 92 term.committed(xid); 93 } 94 long time = System.currentTimeMillis() - start; 95 stats("batch: ", time); 97 } 98 catch (Exception e) 99 { 100 e.printStackTrace(); 101 } 102 } 103 104 } 105 106 private static void stats(String tag, long time) 107 { 108 synchronized (startLock) 109 { 110 count++; 111 if (count == numThreads) 112 { 113 long end = System.currentTimeMillis() - startTime; 114 System.out.println(tag + " TOTAL TIME TOOK: " + end); 115 tx_per_sec = (int) (((double) (numThreads * ITERATIONS) / (double) end) * 1000); 116 System.out.println("tx per sec = " + tx_per_sec); 117 double avg = (double) average / (double) numThreads; 118 } 120 average += time; 121 } 122 } 123 124 public static void main(String [] args) throws Exception 125 { 126 127 128 for (int i = 1; i <= 1024; i = i * 2) 129 { 130 System.out.println("*** threads: " + i); 131 NUM_DIRS = 1; 132 numThreads = i; 133 average = 0; 134 count = 0; 135 runLogger(); 136 } 137 138 139 155 } 156 157 private static void runLogger() 158 throws IOException , InterruptedException 159 { 160 average = 0; 161 File dir = new File ("/tmp/batchRecovery"); 162 dir.mkdirs(); 163 String [] dirs = new String [NUM_DIRS]; 164 for (int i = 0; i < dirs.length; i++) 165 { 166 dirs[i] = "/tmp/batchRecovery/dir" + i; 167 } 168 BatchRecoveryLogger logger = new BatchRecoveryLogger(); 169 logger.setDirectoryList(dirs); 170 logger.setMaxLogSize(ITERATIONS * numThreads); 171 try 172 { 173 logger.start(); 174 } 175 catch (Exception e) 176 { 177 throw new RuntimeException (e); 178 } 179 180 Thread [] workers = new Thread [numThreads]; 181 182 for (int i = 0; i < numThreads; i++) 183 { 184 workers[i] = new Thread (new BatchThread(i, logger)); 185 workers[i].start(); 186 } 187 188 Thread.sleep(1000); 189 190 count = 0; 192 startTime = System.currentTimeMillis(); 193 194 synchronized (startLock) 195 { 196 startLock.notifyAll(); 197 } 198 199 for (int i = 0; i < numThreads; i++) 200 { 201 workers[i].join(); 202 } 203 204 try 206 { 207 logger.stop(); 208 } 209 catch (Exception e) 210 { 211 throw new RuntimeException (e); 212 } 213 } 214 215 private static void simple() 216 throws Exception 217 { 218 average = 0; 219 Thread [] workers = new Thread [numThreads]; 220 221 for (int i = 0; i < numThreads; i++) 222 { 223 workers[i] = new Thread (new SimpleThread(i)); 224 workers[i].start(); 225 } 226 227 Thread.sleep(1000); 228 229 231 startTime = System.currentTimeMillis(); 232 count = 0; 233 234 synchronized (startLock) 235 { 236 startLock.notifyAll(); 237 } 238 239 for (int i = 0; i < numThreads; i++) 240 { 241 workers[i].join(); 242 } 243 } 244 245 private static Object startLock = new Object (); 246 247 private static class SimpleThread implements Runnable 248 { 249 private FileChannel channel; 250 private RandomAccessFile raf; 251 private int id; 252 private BatchWriter logger; 253 254 public SimpleThread(int id) throws IOException 255 { 256 257 File dir = new File ("/tmp/SimpleRecovery/dir" + id); 258 dir.mkdirs(); 259 logger = new BatchWriter("hello", 100, dir, RECORD_SIZE, ITERATIONS * numThreads); 260 } 261 262 public void run() 263 { 264 synchronized (startLock) 265 { 266 try 267 { 268 startLock.wait(); 269 } 270 catch (InterruptedException e) 271 { 272 throw new RuntimeException (e); 273 } 274 } 275 276 278 long start = System.currentTimeMillis(); 279 boolean failed = false; 280 for (int i = 0; i < ITERATIONS; i++) 281 { 282 byte[] bytes = new byte[RECORD_SIZE]; 283 ByteBuffer buf = ByteBuffer.wrap(bytes); 284 try 285 { 286 logger.committing(buf); 287 } 288 catch (Exception e) 289 { 290 failed = true; 291 } 292 } 293 long time = (System.currentTimeMillis() - start); 294 stats("SIMPLE: " + failed + " ", time); 296 logger.cleanup(); 297 } 298 } 299 } 300 | Popular Tags |