KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > cache > test > CacheTestPerf


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

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 JavaDoc {
26         private int readsPerThread;
27         Integer JavaDoc[] testIntegers = new Integer JavaDoc[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             // Initialize an array with random values between 0 and cacheSize-1
36
for (int j = 0; j < cacheSize; j++) {
37                 testIntegers[j] = new Integer JavaDoc((int) (Math.random() * (cacheSize - 1)));
38             }
39
40             // Get a reference to the test cache
41
sapeCache = (Cache) Lookup.getInstance().fetchComponent(
42                 "/cache/test/performanceTest");
43
44         }
45
46         public void run() {
47             String JavaDoc cacheItem = null;
48             Integer JavaDoc[] localTestIntegers = this.testIntegers;
49             Cache localCache = this.sapeCache;
50             int cacheSize = CacheServiceTest.CACHE_SIZE;
51
52             // Record the start time for this thread
53
long startTime = System.currentTimeMillis();
54
55             // Perform this.readsPerThread number of reads.
56
for (int i = 0; i < this.readsPerThread; i++) {
57                 cacheItem = (String JavaDoc) 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 JavaDoc 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 JavaDoc[] threads = new Thread JavaDoc[threadCount];
75
76         // Initialize an arrray of test threads
77
for (int i = 0; i < threadCount; i++) {
78             threads[i] = new Thread JavaDoc(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 // System.out.println("RefreshInterval: " + CacheServiceTest.REFRESH_INTERVAL);
85

86         // Capture the start time for all threads before starting them
87
long startTime = System.currentTimeMillis();
88         for (int i = 0; i < threadCount; i++) {
89             threads[i].start();
90         }
91
92         // Join on the array of threads to ensure that all threads have
93
// finished before continuing on
94
for (int i = 0; i < threadCount; i++) {
95             try {
96                 threads[i].join();
97             } catch (java.lang.InterruptedException JavaDoc e) {}
98         }
99
100         // Output the Total amount of time
101
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