001: package org.shiftone.cache.test.benchmark;
002:
003: import com.opensymphony.oscache.base.algorithm.FIFOCache;
004: import com.opensymphony.oscache.base.algorithm.LRUCache;
005:
006: import org.shiftone.cache.Cache;
007: import org.shiftone.cache.adaptor.*;
008: import org.shiftone.cache.policy.fifo.FifoCacheFactory;
009: import org.shiftone.cache.policy.lfu.LfuCacheFactory;
010: import org.shiftone.cache.policy.lru.LruCacheFactory;
011: import org.shiftone.cache.policy.zero.ZeroCacheFactory;
012: import org.shiftone.cache.policy.single.SingleCacheFactory;
013:
014: import java.io.PrintStream;
015:
016: import java.util.HashMap;
017: import java.util.Hashtable;
018: import java.util.Random;
019:
020: /**
021: * @version $Revision: 1.15 $
022: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
023: */
024: public class BenchmarkRunner {
025:
026: private static final int TIMEOUT_MS = 60 * 60 * 1000; // 1 hour
027: private static final int MILLION = 1000000;
028: private static final int KEY_COUNT = MILLION;
029: private static final Object[] SEQUENCIAL_KEYS = new Object[KEY_COUNT];
030: private static final Object[] PSUDORANDOM_KEYS = new Object[KEY_COUNT];
031: private Benchmark[] benchmarks;
032: private Cache[] caches;
033:
034: public BenchmarkRunner() throws Exception {
035:
036: Random random = new Random(0);
037:
038: for (int i = 0; i < SEQUENCIAL_KEYS.length; i++) {
039: SEQUENCIAL_KEYS[i] = new Integer(i);
040: PSUDORANDOM_KEYS[i] = new Integer(random.nextInt());
041: }
042:
043: Benchmark sequencialAdd = new AddBenchmark("S-Add",
044: SEQUENCIAL_KEYS);
045: Benchmark randomGet = new GetBenchmark("R-Get",
046: PSUDORANDOM_KEYS);
047: Benchmark sequencialGet = new GetBenchmark("S-Get",
048: SEQUENCIAL_KEYS);
049: Benchmark sequencialRemove = new RemoveBenchmark("S-Rem",
050: SEQUENCIAL_KEYS);
051:
052: benchmarks = new Benchmark[] { sequencialAdd, sequencialAdd,
053: sequencialAdd, //
054: sequencialGet, sequencialGet, sequencialGet, //
055: randomGet, randomGet, randomGet, //
056: sequencialRemove, sequencialRemove, sequencialRemove };
057: caches = new Cache[] {
058: new MapCache(new HashMap(KEY_COUNT)), //
059: new MapCache(new Hashtable(KEY_COUNT)), //
060: new MapCache(new FIFOCache(KEY_COUNT)), //
061: new MapCache(new LRUCache(KEY_COUNT)), //
062: new SwarmCache(KEY_COUNT), //
063: new JcsCache(), //
064: new EHCacheCache("test"), //
065: new ZeroCacheFactory().newInstance("zero", TIMEOUT_MS,
066: KEY_COUNT), //
067: new SingleCacheFactory().newInstance("single",
068: TIMEOUT_MS, KEY_COUNT), //
069: new FifoCacheFactory().newInstance("fifo", TIMEOUT_MS,
070: KEY_COUNT), //
071: new LruCacheFactory().newInstance("lru", TIMEOUT_MS,
072: KEY_COUNT), //
073: new LfuCacheFactory().newInstance("lfu", TIMEOUT_MS,
074: KEY_COUNT), //
075: OroCache.createFIFO(KEY_COUNT), //
076: OroCache.createFIFO2(KEY_COUNT), //
077: OroCache.createLRU(KEY_COUNT), //
078: OroCache.createRandom(KEY_COUNT) //
079: };
080: }
081:
082: private final long now() {
083: return System.currentTimeMillis();
084: }
085:
086: public void run(PrintStream tsv) {
087:
088: for (int i = 0; i < benchmarks.length; i++) {
089: tsv.print("\t" + benchmarks[i]);
090: }
091:
092: tsv.println();
093:
094: for (int i = 0; i < caches.length; i++) {
095: run(caches[i], tsv);
096: }
097: }
098:
099: public void run(Cache cache, PrintStream tsv) {
100:
101: long start;
102:
103: tsv.print(cache);
104: tsv.print('\t');
105:
106: // -----------------------------------------
107: for (int i = 0; i < benchmarks.length; i++) {
108: start = now();
109:
110: benchmarks[i].run(cache);
111: tsv.print(now() - start);
112: tsv.print('\t');
113: }
114:
115: tsv.println();
116: cache.clear();
117: Runtime.getRuntime().gc();
118: Runtime.getRuntime().runFinalization();
119: }
120:
121: // new BenchmarkRunner(new MapCache(new LinkedHashMap())),
122: public static void main(String[] args) throws Exception {
123:
124: PrintStream tsv = System.out;
125: BenchmarkRunner runner = new BenchmarkRunner();
126:
127: runner.run(tsv);
128: }
129: }
|