001: package org.shiftone.cache.test;
002:
003: import org.shiftone.cache.Cache;
004: import org.shiftone.cache.CacheFactory;
005: import org.shiftone.cache.decorator.sync.SyncCache;
006: import org.shiftone.cache.policy.fifo.FifoCacheFactory;
007: import org.shiftone.cache.util.Log;
008:
009: import java.util.Random;
010:
011: /**
012: * Concurrent cache tester.
013: *
014: * @version $Revision: 1.7 $
015: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
016: */
017: public class Thrasher {
018:
019: private static final Log LOG = new Log(Thrasher.class);
020: private Thread[] threads; // running worker threads
021: private Cache cache;
022: private int cycles = 100000;
023:
024: public Thrasher(Cache cache, int threadCount) {
025:
026: LOG.message("new Thrasher for " + cache.getClass().getName()
027: + " with " + threadCount + " threads");
028:
029: this .cache = cache;
030:
031: // this.cache = new StatCache(this.cache);
032: this .cache = new SyncCache(this .cache);
033: this .threads = new Thread[threadCount];
034: }
035:
036: public void thrash() {
037:
038: for (int i = 0; i < threads.length; i++) {
039: threads[i] = new Thread(new ThrasherRunnable());
040:
041: LOG.message("starting thread #" + i);
042: threads[i].start();
043: }
044:
045: for (int i = 0; i < threads.length; i++) {
046: try {
047: LOG
048: .message("waiting for thread #" + i
049: + " to complete");
050: threads[i].join();
051: } catch (Exception e) {
052: LOG.error("join failed for thread #" + i, e);
053: }
054: }
055: }
056:
057: class ThrasherRunnable implements Runnable {
058:
059: private Random random = new Random();
060:
061: public void run() {
062:
063: try {
064: for (int i = 0; i < cycles; i++) {
065: cache.addObject(
066: new Integer(random.nextInt() % 100), "obj");
067: cache
068: .getObject(new Integer(
069: random.nextInt() % 100));
070:
071: if (i % 5000 == 0) {
072:
073: /// LOG.info(i + " cycles complete " + cache);
074: }
075: }
076: } catch (Exception e) {
077:
078: /// LOG.error("run failed",e);
079: }
080: }
081: }
082:
083: public static void main(String[] args) throws Exception {
084:
085: Cache cache;
086: CacheFactory cacheFactory;
087: Class factoryClass;
088: String factoryClassName = FifoCacheFactory.class.getName();
089: int size = 100;
090: int ttl = 2000;
091: int threads = 1; // running worker threads
092: int cycles = 100; // cycles per thread
093: int gpc = 5; // gets per cycle
094: int ppc = 5; // puts per cycle
095: Thrasher thrasher = null;
096:
097: for (int i = 0; i < args.length; i++) {
098: LOG.message("arg[ " + i + " ] = " + args[i]);
099: }
100:
101: for (int i = 0; i < args.length; i++) {
102: if ("-factory".equalsIgnoreCase(args[i])) {
103: factoryClassName = args[++i];
104: }
105:
106: if ("-size".equalsIgnoreCase(args[i])) {
107: size = Integer.parseInt(args[++i]);
108: }
109:
110: if ("-ttl".equalsIgnoreCase(args[i])) {
111: ttl = Integer.parseInt(args[++i]);
112: }
113:
114: if ("-threads".equalsIgnoreCase(args[i])) {
115: threads = Integer.parseInt(args[++i]);
116: }
117:
118: if ("-cycles".equalsIgnoreCase(args[i])) {
119: cycles = Integer.parseInt(args[++i]);
120: }
121:
122: if ("-gpc".equalsIgnoreCase(args[i])) {
123: gpc = Integer.parseInt(args[++i]);
124: }
125:
126: if ("-ppc".equalsIgnoreCase(args[i])) {
127: ppc = Integer.parseInt(args[++i]);
128: }
129: }
130:
131: factoryClass = Class.forName(factoryClassName);
132: cacheFactory = (CacheFactory) factoryClass.newInstance();
133: cache = cacheFactory.newInstance("thrasher", ttl, size);
134: thrasher = new Thrasher(cache, threads);
135:
136: thrasher.thrash();
137: }
138: }
|