001: /*
002: Copyright 2004 Philip Jacob <phil@whirlycott.com>
003: Seth Fitzsimmons <seth@note.amherst.edu>
004:
005: Licensed under the Apache License, Version 2.0 (the "License");
006: you may not use this file except in compliance with the License.
007: You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: */
017:
018: package com.whirlycott.cache.test;
019:
020: import junit.framework.TestCase;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import com.whirlycott.cache.Cache;
026: import com.whirlycott.cache.CacheManager;
027:
028: /**
029: * @author phil
030: */
031: public class BenchmarkWhirlycache extends TestCase {
032:
033: private static Log log = LogFactory
034: .getLog(BenchmarkWhirlycache.class);
035:
036: public static void main(String[] args) throws Exception {
037: log.debug("Single threaded tests...");
038: BenchmarkWhirlycache whirlytest = new BenchmarkWhirlycache();
039: whirlytest.testStore();
040: whirlytest.testRetrieve();
041: whirlytest.testMultiThreaded();
042: }
043:
044: /**
045: * Some single threaded gets.
046: * @throws Exception
047: */
048: public void testRetrieve() throws Exception {
049: final Cache c = CacheManager.getInstance().getCache();
050: assertNotNull(c);
051:
052: //BEGIN RETRIEVES
053: final long startGet = System.currentTimeMillis();
054: for (int loop = 0; loop < Constants.RETRIEVE_COUNT; loop++) {
055: for (int i = 0; i < Constants.STORE_COUNT; i++) {
056:
057: final Object o = c.retrieve(new Integer(i).toString());
058: //assertEquals((String)o, "value" + new Integer(i).toString());
059:
060: }
061: }
062: //CacheManager.getInstance().destroy();
063: final long endGet = System.currentTimeMillis();
064: log.debug("Total RETRIEVE time was: " + (endGet - startGet));
065: //END RETRIEVES
066: }
067:
068: /**
069: * A bunch of single-threaded stores.
070: * @throws Exception
071: */
072: public void testStore() throws Exception {
073:
074: final Cache c = CacheManager.getInstance().getCache();
075:
076: assertNotNull(c);
077:
078: //BEGIN STORES
079: final long start = System.currentTimeMillis();
080: for (int i = 0; i < Constants.STORE_COUNT; i++) {
081: c.store(new Integer(i).toString(), "value" + i);
082: }
083: final long end = System.currentTimeMillis();
084: log.debug("Total STORE time was: " + (end - start));
085: //END STORES
086:
087: log.debug("Cache size is: " + c.size());
088: }
089:
090: /**
091: * Some multi-threaded tests.
092: *
093: */
094: public void testMultiThreaded() {
095: new ThreadedGets().testMultiThreadedReads();
096: }
097:
098: /**
099: * Inner class for running the multi-threaded tests.
100: * @author pjacob
101: *
102: */
103: public class ThreadedGets implements Runnable {
104:
105: private void testMultiThreadedReads() {
106: final Thread[] t = new Thread[Constants.THREAD_COUNT];
107: final long beginGets = System.currentTimeMillis();
108:
109: for (int i = 0; i < Constants.THREAD_COUNT; i++) {
110: log.debug("Starting thread " + i);
111: t[i] = new Thread(new ThreadedGets());
112: t[i].start();
113: }
114:
115: for (int i = 0; i < Constants.THREAD_COUNT; i++) {
116: try {
117: t[i].join();
118: } catch (InterruptedException e) {
119: e.printStackTrace();
120: }
121: }
122:
123: final long endGets = System.currentTimeMillis();
124:
125: log.debug("Total mx-thread time w/ concurrency level "
126: + Constants.THREAD_COUNT + " was "
127: + (endGets - beginGets));
128:
129: /* final CacheManager cm = CacheManager.getInstance();
130:
131: try {
132: cm.shutdown();
133: } catch (CacheException e) {
134: e.printStackTrace();
135: }*/
136: }
137:
138: /* (non-Javadoc)
139: * @see java.lang.Runnable#run()
140: */
141: public void run() {
142: try {
143: if (new Double(Math.random() * 100D).intValue() % 2 == 0) {
144: log.debug("Reader thread...");
145: new BenchmarkWhirlycache().testRetrieve();
146: } else {
147: log.debug("Writer thread...");
148: new BenchmarkWhirlycache().testStore();
149: }
150: } catch (Exception e) {
151: e.printStackTrace();
152: }
153: }
154: }
155:
156: }
|