001: /*
002: * Created on Oct 10, 2004 by pjacob
003: *
004: */
005: package com.whirlycott.cache.benchmarks;
006:
007: import java.io.File;
008: import java.io.IOException;
009: import java.util.ArrayList;
010: import java.util.Date;
011: import java.util.HashMap;
012: import java.util.Iterator;
013: import java.util.List;
014: import java.util.Map;
015:
016: import org.apache.commons.logging.Log;
017: import org.apache.commons.logging.LogFactory;
018: import org.jfree.chart.ChartUtilities;
019: import org.jfree.chart.JFreeChart;
020: import org.jfree.chart.axis.CategoryAxis;
021: import org.jfree.chart.axis.CategoryAxis3D;
022: import org.jfree.chart.axis.NumberAxis3D;
023: import org.jfree.chart.axis.ValueAxis;
024: import org.jfree.chart.plot.CategoryPlot;
025: import org.jfree.chart.renderer.category.BarRenderer3D;
026: import org.jfree.data.category.DefaultCategoryDataset;
027:
028: /**
029: * @author pjacob
030: *
031: */
032: public class BenchmarkRunner {
033:
034: /**
035: * Number of concurrent threads.
036: */
037: public final static int CONCURRENT_THREADS = 20;
038:
039: /**
040: * Number of elements in the cache
041: */
042: private static final int ELEMENTS_CACHED = 50000;
043:
044: /**
045: * Number of iterations that the benchmark will be run.
046: */
047: public final static int ITERATIONS_PER_BENCHMARK = 10;
048:
049: /**
050: * Logger.
051: */
052: private static final Log log = LogFactory
053: .getLog(BenchmarkRunner.class);
054:
055: /**
056: * Percent of read-only activity: 8 = 80%, 5 = 50%, etc.
057: */
058: public final static int PERCENT_READONLY = 8;
059:
060: /**
061: * How many milliseconds should we do a Thread.sleep(n) between tests.
062: */
063: public final static int SLEEPTIME_BETWEEN_TESTS = 11000;
064:
065: /**
066: * Entry point into the benchmarking code.
067: * @param args
068: * @throws IOException
069: */
070: public static void main(final String[] args) throws Exception {
071:
072: //Just add in new cache bencharkables here and the rest is automatic.
073: final Map benchmarkableCaches = new HashMap();
074:
075: //Test params
076: final Params testParams = new Params(ELEMENTS_CACHED,
077: CONCURRENT_THREADS);
078:
079: benchmarkableCaches.put(new StandardHashmap(), testParams);
080:
081: benchmarkableCaches.put(new Ehcache(), testParams);
082:
083: benchmarkableCaches.put(new Whirlycache(), testParams);
084:
085: benchmarkableCaches.put(new Oscache(), testParams);
086:
087: benchmarkableCaches.put(new ApacheJCS(), testParams);
088:
089: benchmarkableCaches.put(new JBossCache(), testParams);
090:
091: //Store the results in a list.
092: final List results = new ArrayList();
093:
094: /*
095: * Run each benchmark.
096: */
097: for (final Iterator i = benchmarkableCaches.keySet().iterator(); i
098: .hasNext();) {
099: final Benchmarkable b = (Benchmarkable) i.next();
100: log.debug("Starting test: " + b.getName());
101:
102: final Params p = (Params) benchmarkableCaches.get(b);
103: final Result result = b.benchmark(p);
104: results.add(result);
105: log.debug("Benchmark for " + b.getName()
106: + " with settings " + p.getShortName() + ": "
107: + result.getTime());
108:
109: //Do a gc between each test.
110: System.gc();
111:
112: log.debug("Sleeping between tests...");
113: Thread.sleep(SLEEPTIME_BETWEEN_TESTS);
114:
115: }
116:
117: //Print out a graphic.
118: makeGraph(results);
119:
120: }
121:
122: /**
123: * Make the graphic.
124: * @param _results
125: * @throws IOException
126: */
127: public static void makeGraph(final List _results)
128: throws IOException {
129: final CategoryPlot plot = new CategoryPlot();
130:
131: //Create jfree objects based on the List of results passed in
132: int counter = 0;
133: final DefaultCategoryDataset d = new DefaultCategoryDataset();
134: for (final Iterator i = _results.iterator(); i.hasNext();) {
135:
136: final Result result = (Result) i.next();
137: d.addValue(result.getTime(), result.getName(), result
138: .getName());
139: log.debug("Adding: " + result.getName() + " with time "
140: + result.getTime());
141: plot.setDataset(counter, d);
142: counter++;
143: }
144:
145: plot.setRenderer(new BarRenderer3D());
146:
147: final CategoryAxis domainAxis = new CategoryAxis3D();
148: plot.setDomainAxis(domainAxis);
149:
150: final ValueAxis rangeAxis = new NumberAxis3D();
151: rangeAxis.setLabel("Milliseconds");
152: plot.setRangeAxis(rangeAxis);
153: plot.setForegroundAlpha(0.7f);
154:
155: final JFreeChart chart = new JFreeChart(
156: "Whirlycache Benchmarks at " + new Date(), plot);
157: chart.setAntiAlias(true);
158:
159: final String file = System.getProperty("java.io.tmpdir")
160: + "whirlycache-results.png";
161: log.debug("Writing results to " + file);
162: final File f = new File(file);
163:
164: ChartUtilities.saveChartAsPNG(f, chart, 500, 500);
165: }
166:
167: public BenchmarkRunner() {
168: super();
169: }
170:
171: }
|