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.opensymphony.oscache.base.Cache;
015: import com.opensymphony.oscache.general.GeneralCacheAdministrator;
016:
017: /**
018: * @author pjacob
019: *
020: */
021: public class Oscache implements Benchmarkable {
022:
023: /**
024: * Logger
025: */
026: private static final Log log = LogFactory.getLog(Oscache.class);
027:
028: /**
029: * Test params
030: */
031: private Params params;
032:
033: /**
034: *
035: */
036: public Oscache() {
037: super ();
038: }
039:
040: /* (non-Javadoc)
041: */
042: public Result benchmark(final Params _params) {
043: params = _params;
044:
045: //Do some initialization
046: final Cache c = new GeneralCacheAdministrator().getCache();
047:
048: final Result r = new Result(this );
049: final long begin = System.currentTimeMillis();
050:
051: //Run some iterations.
052: for (int iterations = 0; iterations <= BenchmarkRunner.ITERATIONS_PER_BENCHMARK; iterations++) {
053:
054: //Run the benchmark() method
055: final List threads = new ArrayList();
056: for (int i = 0; i < params.getThreads(); i++) {
057: final Thread t = new Thread(this );
058: threads.add(t);
059: t.start();
060: try {
061: Thread.sleep(100);
062: } catch (final InterruptedException e2) {
063: log.error(e2.getMessage(), e2);
064: }
065: }
066:
067: //Wait for the threads to finish.
068: for (final Iterator i = threads.iterator(); i.hasNext();) {
069: try {
070: ((Thread) i.next()).join();
071: } catch (InterruptedException e) {
072: log.error(e.getMessage(), e);
073: }
074: }
075:
076: }
077:
078: //Return the time.
079: final long end = System.currentTimeMillis();
080: r.setTime(end - begin);
081: return r;
082: }
083:
084: /* (non-Javadoc)
085: * @see com.whirlycott.cache.benchmarks.Benchmarkable#getName()
086: */
087: public String getName() {
088: return "Oscache";
089: }
090:
091: /* (non-Javadoc)
092: * @see com.whirlycott.cache.benchmarks.Benchmarkable#getParams()
093: */
094: public Params getParams() {
095: return params;
096: }
097:
098: /* (non-Javadoc)
099: * @see java.lang.Runnable#run()
100: */
101: public void run() {
102: try {
103: final Cache c = new GeneralCacheAdministrator().getCache();
104:
105: //Insert n elements.
106: for (int i = 0; i <= params.getCacheSize(); i++) {
107: c.putInCache(new Integer(i).toString(), "v-" + i);
108: }
109:
110: //Get n elements.
111: for (int i = 0; i <= params.getCacheSize(); i++) {
112: //~80% of the time, do reads, else do a remove and a store
113: if (i % BenchmarkRunner.PERCENT_READONLY == 0) {
114: c.flushEntry(new Integer(i).toString());
115: c.putInCache(new Integer(i).toString(), "v-" + i);
116: i += (10 - BenchmarkRunner.PERCENT_READONLY);
117: } else {
118: final Object o = c.getFromCache(new Integer(i)
119: .toString());
120: }
121: }
122:
123: } catch (final Exception e) {
124: log.error(e.getMessage(), e);
125: }
126: }
127:
128: }
|