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