1 17 18 package org.sape.carbon.services.cache.test; 19 20 import org.sape.carbon.core.component.Lookup; 21 import org.sape.carbon.services.cache.Cache; 22 23 public class CacheTestPerf { 24 25 private static class CachePerformanceThread implements Runnable { 26 private int readsPerThread; 27 Integer [] testIntegers = new Integer [CacheServiceTest.CACHE_SIZE]; 28 Cache sapeCache; 29 30 public CachePerformanceThread( int myReadsPerThread) { 31 this.readsPerThread = myReadsPerThread; 32 int cacheSize = CacheServiceTest.CACHE_SIZE; 33 34 35 for (int j = 0; j < cacheSize; j++) { 37 testIntegers[j] = new Integer ((int) (Math.random() * (cacheSize - 1))); 38 } 39 40 sapeCache = (Cache) Lookup.getInstance().fetchComponent( 42 "/cache/test/performanceTest"); 43 44 } 45 46 public void run() { 47 String cacheItem = null; 48 Integer [] localTestIntegers = this.testIntegers; 49 Cache localCache = this.sapeCache; 50 int cacheSize = CacheServiceTest.CACHE_SIZE; 51 52 long startTime = System.currentTimeMillis(); 54 55 for (int i = 0; i < this.readsPerThread; i++) { 57 cacheItem = (String ) localCache.get(testIntegers[i % cacheSize]); 58 } 59 long endTime = System.currentTimeMillis(); 60 System.out.println("Thread time: " + (endTime - startTime) + " milliseconds"); 61 } 62 } 63 64 public static void main(String args[]) { 65 66 if (args.length < 2) { 67 System.out.println("Usage is <program> threadCount readsPerThread"); 68 System.exit(0); 69 } 70 71 int threadCount = Integer.parseInt(args[0]); 72 int readsPerThread = Integer.parseInt(args[1]); 73 74 Thread [] threads = new Thread [threadCount]; 75 76 for (int i = 0; i < threadCount; i++) { 78 threads[i] = new Thread (new CachePerformanceThread(readsPerThread)); 79 } 80 81 System.out.println("Starting ReadOnlyCache Test Case"); 82 System.out.println("Totals Thread Count: " + threadCount); 83 System.out.println("Number of reads/thread: " + readsPerThread); 84 86 long startTime = System.currentTimeMillis(); 88 for (int i = 0; i < threadCount; i++) { 89 threads[i].start(); 90 } 91 92 for (int i = 0; i < threadCount; i++) { 95 try { 96 threads[i].join(); 97 } catch (java.lang.InterruptedException e) {} 98 } 99 100 long elapsedTime = System.currentTimeMillis() - startTime; 102 long totalReads = threadCount*readsPerThread; 103 System.out.println("----------------------------------------------"); 104 System.out.println("Total time: " + elapsedTime + " milliseconds"); 105 System.out.println("Total reads: " + totalReads +" reads"); 106 System.out.println("Reads/second: " +(((long)(totalReads*1000))/elapsedTime) +" rps"); 107 } 108 } 109 | Popular Tags |