001: /*
002: * Created on Oct 11, 2004 by pjacob
003: *
004: */
005: package com.whirlycott.cache.benchmarks;
006:
007: import java.util.ArrayList;
008: import java.util.Iterator;
009: import java.util.List;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013:
014: import com.whirlycott.cache.Cache;
015: import com.whirlycott.cache.CacheException;
016: import com.whirlycott.cache.CacheManager;
017:
018: /**
019: * Holder for the tests specific to whirlycache.
020: * @author pjacob
021: *
022: */
023: public class Whirlycache implements Benchmarkable {
024:
025: private static final Log log = LogFactory.getLog(Whirlycache.class);
026:
027: private Params params;
028:
029: /**
030: *
031: */
032: public Whirlycache() {
033: super ();
034: }
035:
036: /**
037: *
038: */
039: public Result benchmark(final Params _params) {
040: params = _params;
041:
042: //Do some initialization.
043: final CacheManager c = CacheManager.getInstance();
044:
045: final Result r = new Result(this );
046: final long begin = System.currentTimeMillis();
047:
048: //Run some iterations.
049: for (int iterations = 0; iterations <= BenchmarkRunner.ITERATIONS_PER_BENCHMARK; iterations++) {
050:
051: //Run the benchmark() method
052: final List threads = new ArrayList();
053: for (int i = 0; i < params.getThreads(); i++) {
054: final Thread t = new Thread(this );
055: threads.add(t);
056: t.start();
057: try {
058: Thread.sleep(100);
059: } catch (final InterruptedException e2) {
060: log.error(e2.getMessage(), e2);
061: }
062: }
063:
064: //Wait for the threads to finish.
065: for (final Iterator i = threads.iterator(); i.hasNext();) {
066: try {
067: ((Thread) i.next()).join();
068: } catch (InterruptedException e) {
069: log.error(e.getMessage(), e);
070: }
071: }
072:
073: }
074:
075: //Return the time.
076: final long end = System.currentTimeMillis();
077: r.setTime(end - begin);
078:
079: //Shutdown.
080: try {
081: CacheManager.getInstance().shutdown();
082: } catch (final CacheException e) {
083: log.error(e.getMessage(), e);
084: }
085:
086: return r;
087:
088: }
089:
090: /* (non-Javadoc)
091: * @see com.whirlycott.cache.benchmarks.Benchmarkable#getName()
092: */
093: public String getName() {
094: return "Whirlycache";
095: }
096:
097: /* (non-Javadoc)
098: * @see com.whirlycott.cache.benchmarks.Benchmarkable#getParams()
099: */
100: public Params getParams() {
101: return params;
102: }
103:
104: /* (non-Javadoc)
105: * @see java.lang.Runnable#run()
106: */
107: public void run() {
108: try {
109: //Get a Cache.
110: final Cache c = CacheManager.getInstance().getCache();
111:
112: //Insert n elements.
113: for (int i = 0; i <= params.getCacheSize(); i++) {
114: c.store(new Integer(i), "v-" + i);
115: }
116:
117: //Get n elements.
118: for (int i = 0; i <= params.getCacheSize(); i++) {
119: //~80% of the time, do reads, else do a remove and a store
120: if (i % BenchmarkRunner.PERCENT_READONLY == 0) {
121: c.remove(new Integer(i));
122: c.store(new Integer(i), "v-" + i);
123: i += (10 - BenchmarkRunner.PERCENT_READONLY);
124: } else {
125: final Object o = c.retrieve(new Integer(i));
126: }
127: }
128:
129: } catch (final CacheException e) {
130: log.error(e.getMessage(), e);
131: }
132:
133: }
134:
135: }
|