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.Collections;
009: import java.util.HashMap;
010: import java.util.Iterator;
011: import java.util.List;
012: import java.util.Map;
013:
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016:
017: /**
018: * @author pjacob
019: *
020: */
021: public class StandardHashmap implements Benchmarkable {
022:
023: private static final Log log = LogFactory
024: .getLog(StandardHashmap.class);
025:
026: public static final Map cache = Collections
027: .synchronizedMap(new HashMap());
028: //public static final Map cache = new AnotherHashmap();
029:
030: private Params params;
031:
032: /**
033: *
034: */
035: public StandardHashmap() {
036: super ();
037: }
038:
039: /* (non-Javadoc)
040: */
041: public Result benchmark(final Params _params) {
042: params = _params;
043:
044: //Do some initialization
045: // n/a
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.clear();
083:
084: return r;
085: }
086:
087: /* (non-Javadoc)
088: * @see com.whirlycott.cache.benchmarks.Benchmarkable#getName()
089: */
090: public String getName() {
091: return "java.util.HashMap";
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: }
|