KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > test > Thrasher


1 package org.shiftone.cache.test;
2
3
4
5 import org.shiftone.cache.Cache;
6 import org.shiftone.cache.CacheFactory;
7 import org.shiftone.cache.decorator.sync.SyncCache;
8 import org.shiftone.cache.policy.fifo.FifoCacheFactory;
9 import org.shiftone.cache.util.Log;
10
11 import java.util.Random JavaDoc;
12
13
14 /**
15  * Concurrent cache tester.
16  *
17  * @version $Revision: 1.7 $
18  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
19  */

20 public class Thrasher
21 {
22
23     private static final Log LOG = new Log(Thrasher.class);
24     private Thread JavaDoc[] threads; // running worker threads
25
private Cache cache;
26     private int cycles = 100000;
27
28     public Thrasher(Cache cache, int threadCount)
29     {
30
31         LOG.message("new Thrasher for " + cache.getClass().getName() + " with " + threadCount + " threads");
32
33         this.cache = cache;
34
35         // this.cache = new StatCache(this.cache);
36
this.cache = new SyncCache(this.cache);
37         this.threads = new Thread JavaDoc[threadCount];
38     }
39
40
41     public void thrash()
42     {
43
44         for (int i = 0; i < threads.length; i++)
45         {
46             threads[i] = new Thread JavaDoc(new ThrasherRunnable());
47
48             LOG.message("starting thread #" + i);
49             threads[i].start();
50         }
51
52         for (int i = 0; i < threads.length; i++)
53         {
54             try
55             {
56                 LOG.message("waiting for thread #" + i + " to complete");
57                 threads[i].join();
58             }
59             catch (Exception JavaDoc e)
60             {
61                 LOG.error("join failed for thread #" + i, e);
62             }
63         }
64     }
65
66
67     class ThrasherRunnable implements Runnable JavaDoc
68     {
69
70         private Random JavaDoc random = new Random JavaDoc();
71
72         public void run()
73         {
74
75             try
76             {
77                 for (int i = 0; i < cycles; i++)
78                 {
79                     cache.addObject(new Integer JavaDoc(random.nextInt() % 100), "obj");
80                     cache.getObject(new Integer JavaDoc(random.nextInt() % 100));
81
82                     if (i % 5000 == 0)
83                     {
84
85                         /// LOG.info(i + " cycles complete " + cache);
86
}
87                 }
88             }
89             catch (Exception JavaDoc e)
90             {
91
92                 /// LOG.error("run failed",e);
93
}
94         }
95     }
96
97     public static void main(String JavaDoc[] args) throws Exception JavaDoc
98     {
99
100         Cache cache;
101         CacheFactory cacheFactory;
102         Class JavaDoc factoryClass;
103         String JavaDoc factoryClassName = FifoCacheFactory.class.getName();
104         int size = 100;
105         int ttl = 2000;
106         int threads = 1; // running worker threads
107
int cycles = 100; // cycles per thread
108
int gpc = 5; // gets per cycle
109
int ppc = 5; // puts per cycle
110
Thrasher thrasher = null;
111
112         for (int i = 0; i < args.length; i++)
113         {
114             LOG.message("arg[ " + i + " ] = " + args[i]);
115         }
116
117         for (int i = 0; i < args.length; i++)
118         {
119             if ("-factory".equalsIgnoreCase(args[i]))
120             {
121                 factoryClassName = args[++i];
122             }
123
124             if ("-size".equalsIgnoreCase(args[i]))
125             {
126                 size = Integer.parseInt(args[++i]);
127             }
128
129             if ("-ttl".equalsIgnoreCase(args[i]))
130             {
131                 ttl = Integer.parseInt(args[++i]);
132             }
133
134             if ("-threads".equalsIgnoreCase(args[i]))
135             {
136                 threads = Integer.parseInt(args[++i]);
137             }
138
139             if ("-cycles".equalsIgnoreCase(args[i]))
140             {
141                 cycles = Integer.parseInt(args[++i]);
142             }
143
144             if ("-gpc".equalsIgnoreCase(args[i]))
145             {
146                 gpc = Integer.parseInt(args[++i]);
147             }
148
149             if ("-ppc".equalsIgnoreCase(args[i]))
150             {
151                 ppc = Integer.parseInt(args[++i]);
152             }
153         }
154
155         factoryClass = Class.forName(factoryClassName);
156         cacheFactory = (CacheFactory) factoryClass.newInstance();
157         cache = cacheFactory.newInstance("thrasher", ttl, size);
158         thrasher = new Thrasher(cache, threads);
159
160         thrasher.thrash();
161     }
162 }
163
Popular Tags