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