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