1 package org.shiftone.cache.test; 2 3 4 5 import org.shiftone.cache.Cache; 6 import org.shiftone.cache.CacheFactory; 7 import org.shiftone.cache.decorator.sync.SyncCache; 8 import org.shiftone.cache.policy.fifo.FifoCacheFactory; 9 import org.shiftone.cache.util.Log; 10 11 import java.util.Random ; 12 13 14 20 public class Thrasher 21 { 22 23 private static final Log LOG = new Log(Thrasher.class); 24 private Thread [] threads; private Cache cache; 26 private int cycles = 100000; 27 28 public Thrasher(Cache cache, int threadCount) 29 { 30 31 LOG.message("new Thrasher for " + cache.getClass().getName() + " with " + threadCount + " threads"); 32 33 this.cache = cache; 34 35 this.cache = new SyncCache(this.cache); 37 this.threads = new Thread [threadCount]; 38 } 39 40 41 public void thrash() 42 { 43 44 for (int i = 0; i < threads.length; i++) 45 { 46 threads[i] = new Thread (new ThrasherRunnable()); 47 48 LOG.message("starting thread #" + i); 49 threads[i].start(); 50 } 51 52 for (int i = 0; i < threads.length; i++) 53 { 54 try 55 { 56 LOG.message("waiting for thread #" + i + " to complete"); 57 threads[i].join(); 58 } 59 catch (Exception e) 60 { 61 LOG.error("join failed for thread #" + i, e); 62 } 63 } 64 } 65 66 67 class ThrasherRunnable implements Runnable 68 { 69 70 private Random random = new Random (); 71 72 public void run() 73 { 74 75 try 76 { 77 for (int i = 0; i < cycles; i++) 78 { 79 cache.addObject(new Integer (random.nextInt() % 100), "obj"); 80 cache.getObject(new Integer (random.nextInt() % 100)); 81 82 if (i % 5000 == 0) 83 { 84 85 } 87 } 88 } 89 catch (Exception e) 90 { 91 92 } 94 } 95 } 96 97 public static void main(String [] args) throws Exception 98 { 99 100 Cache cache; 101 CacheFactory cacheFactory; 102 Class factoryClass; 103 String factoryClassName = FifoCacheFactory.class.getName(); 104 int size = 100; 105 int ttl = 2000; 106 int threads = 1; int cycles = 100; int gpc = 5; int ppc = 5; Thrasher thrasher = null; 111 112 for (int i = 0; i < args.length; i++) 113 { 114 LOG.message("arg[ " + i + " ] = " + args[i]); 115 } 116 117 for (int i = 0; i < args.length; i++) 118 { 119 if ("-factory".equalsIgnoreCase(args[i])) 120 { 121 factoryClassName = args[++i]; 122 } 123 124 if ("-size".equalsIgnoreCase(args[i])) 125 { 126 size = Integer.parseInt(args[++i]); 127 } 128 129 if ("-ttl".equalsIgnoreCase(args[i])) 130 { 131 ttl = Integer.parseInt(args[++i]); 132 } 133 134 if ("-threads".equalsIgnoreCase(args[i])) 135 { 136 threads = Integer.parseInt(args[++i]); 137 } 138 139 if ("-cycles".equalsIgnoreCase(args[i])) 140 { 141 cycles = Integer.parseInt(args[++i]); 142 } 143 144 if ("-gpc".equalsIgnoreCase(args[i])) 145 { 146 gpc = Integer.parseInt(args[++i]); 147 } 148 149 if ("-ppc".equalsIgnoreCase(args[i])) 150 { 151 ppc = Integer.parseInt(args[++i]); 152 } 153 } 154 155 factoryClass = Class.forName(factoryClassName); 156 cacheFactory = (CacheFactory) factoryClass.newInstance(); 157 cache = cacheFactory.newInstance("thrasher", ttl, size); 158 thrasher = new Thrasher(cache, threads); 159 160 thrasher.thrash(); 161 } 162 } 163 | Popular Tags |