KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > recovery > test > TestForceTime


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.tm.recovery.test;
23
24 import org.jboss.tm.recovery.BatchRecoveryLogger;
25 import org.jboss.tm.recovery.BatchWriter;
26 import org.jboss.tm.recovery.RecoveryLogTerminator;
27 import org.jboss.tm.XidFactory;
28
29 import java.io.RandomAccessFile JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.File JavaDoc;
32 import java.nio.channels.FileChannel JavaDoc;
33 import java.nio.ByteBuffer JavaDoc;
34
35 import EDU.oswego.cs.dl.util.concurrent.Latch;
36
37 import javax.transaction.xa.Xid JavaDoc;
38
39 /**
40  * benches file per thread vs. batch queue
41  *
42  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
43  * @version $Revision: 37459 $
44  */

45 public class TestForceTime
46 {
47    private static final int RECORD_SIZE = Xid.MAXGTRIDSIZE * 2;
48    private static int NUM_DIRS = 2;
49    private static final int ITERATIONS = 100;
50    private static int numThreads = 500;
51    private static long average;
52    private static int count;
53    private static long startTime;
54    private static XidFactory factory = new XidFactory();
55    private static int tx_per_sec;
56
57
58    private static class BatchThread implements Runnable JavaDoc
59    {
60       private BatchRecoveryLogger logger;
61       private int id;
62
63       public BatchThread(int id, BatchRecoveryLogger logger)
64       {
65          this.logger = logger;
66          this.id = id;
67       }
68
69       public void run()
70       {
71          synchronized (startLock)
72          {
73             try
74             {
75                startLock.wait();
76             }
77             catch (InterruptedException JavaDoc e)
78             {
79                throw new RuntimeException JavaDoc(e);
80             }
81          }
82
83          try
84          {
85             //System.out.println("Starting " + id);
86

87             long start = System.currentTimeMillis();
88             for (int i = 0; i < ITERATIONS; i++)
89             {
90                Xid JavaDoc xid = factory.newXid();
91                RecoveryLogTerminator term = logger.committing(xid);
92                term.committed(xid);
93             }
94             long time = System.currentTimeMillis() - start;
95             //System.out.println(ITERATIONS + " batches took : " + time + " of id " + id);
96
stats("batch: ", time);
97          }
98          catch (Exception JavaDoc e)
99          {
100             e.printStackTrace();
101          }
102       }
103
104    }
105
106    private static void stats(String JavaDoc tag, long time)
107    {
108       synchronized (startLock)
109       {
110          count++;
111          if (count == numThreads)
112          {
113             long end = System.currentTimeMillis() - startTime;
114             System.out.println(tag + " TOTAL TIME TOOK: " + end);
115             tx_per_sec = (int) (((double) (numThreads * ITERATIONS) / (double) end) * 1000);
116             System.out.println("tx per sec = " + tx_per_sec);
117             double avg = (double) average / (double) numThreads;
118             //System.out.println(tag + "average time took: " + avg);
119
}
120          average += time;
121       }
122    }
123
124    public static void main(String JavaDoc[] args) throws Exception JavaDoc
125    {
126
127
128       for (int i = 1; i <= 1024; i = i * 2)
129       {
130          System.out.println("*** threads: " + i);
131          NUM_DIRS = 1;
132          numThreads = i;
133          average = 0;
134          count = 0;
135          runLogger();
136       }
137
138
139       /*
140       int i = 1024;
141       int last_tx = 0;
142       while (true)
143       {
144          System.out.println("*** threads: " + i);
145          NUM_DIRS = 1;
146          numThreads = i;
147          average = 0;
148          count = 0;
149          runLogger();
150          if (last_tx > tx_per_sec) break;
151          last_tx = tx_per_sec;
152          i+= 1;
153       }
154       */

155    }
156
157    private static void runLogger()
158            throws IOException JavaDoc, InterruptedException JavaDoc
159    {
160       average = 0;
161       File JavaDoc dir = new File JavaDoc("/tmp/batchRecovery");
162       dir.mkdirs();
163       String JavaDoc[] dirs = new String JavaDoc[NUM_DIRS];
164       for (int i = 0; i < dirs.length; i++)
165       {
166          dirs[i] = "/tmp/batchRecovery/dir" + i;
167       }
168       BatchRecoveryLogger logger = new BatchRecoveryLogger();
169       logger.setDirectoryList(dirs);
170       logger.setMaxLogSize(ITERATIONS * numThreads);
171       try
172       {
173          logger.start();
174       }
175       catch (Exception JavaDoc e)
176       {
177          throw new RuntimeException JavaDoc(e);
178       }
179
180       Thread JavaDoc[] workers = new Thread JavaDoc[numThreads];
181
182       for (int i = 0; i < numThreads; i++)
183       {
184          workers[i] = new Thread JavaDoc(new BatchThread(i, logger));
185          workers[i].start();
186       }
187
188       Thread.sleep(1000);
189
190       //System.out.println("waking up threads");
191
count = 0;
192       startTime = System.currentTimeMillis();
193
194       synchronized (startLock)
195       {
196          startLock.notifyAll();
197       }
198
199       for (int i = 0; i < numThreads; i++)
200       {
201          workers[i].join();
202       }
203
204       //System.out.println("logger.stop");
205
try
206       {
207          logger.stop();
208       }
209       catch (Exception JavaDoc e)
210       {
211          throw new RuntimeException JavaDoc(e);
212       }
213    }
214
215    private static void simple()
216            throws Exception JavaDoc
217    {
218       average = 0;
219       Thread JavaDoc[] workers = new Thread JavaDoc[numThreads];
220
221       for (int i = 0; i < numThreads; i++)
222       {
223          workers[i] = new Thread JavaDoc(new SimpleThread(i));
224          workers[i].start();
225       }
226
227       Thread.sleep(1000);
228
229       //System.out.println("waking up threads");
230

231       startTime = System.currentTimeMillis();
232       count = 0;
233
234       synchronized (startLock)
235       {
236          startLock.notifyAll();
237       }
238
239       for (int i = 0; i < numThreads; i++)
240       {
241          workers[i].join();
242       }
243    }
244
245    private static Object JavaDoc startLock = new Object JavaDoc();
246
247    private static class SimpleThread implements Runnable JavaDoc
248    {
249       private FileChannel JavaDoc channel;
250       private RandomAccessFile JavaDoc raf;
251       private int id;
252       private BatchWriter logger;
253
254       public SimpleThread(int id) throws IOException JavaDoc
255       {
256
257          File JavaDoc dir = new File JavaDoc("/tmp/SimpleRecovery/dir" + id);
258          dir.mkdirs();
259          logger = new BatchWriter("hello", 100, dir, RECORD_SIZE, ITERATIONS * numThreads);
260       }
261
262       public void run()
263       {
264          synchronized (startLock)
265          {
266             try
267             {
268                startLock.wait();
269             }
270             catch (InterruptedException JavaDoc e)
271             {
272                throw new RuntimeException JavaDoc(e);
273             }
274          }
275
276          //System.out.println("Starting...");
277

278          long start = System.currentTimeMillis();
279          boolean failed = false;
280          for (int i = 0; i < ITERATIONS; i++)
281          {
282             byte[] bytes = new byte[RECORD_SIZE];
283             ByteBuffer JavaDoc buf = ByteBuffer.wrap(bytes);
284             try
285             {
286                logger.committing(buf);
287             }
288             catch (Exception JavaDoc e)
289             {
290                failed = true;
291             }
292          }
293          long time = (System.currentTimeMillis() - start);
294          //System.out.println(ITERATIONS + " forces took: " + time);
295
stats("SIMPLE: " + failed + " ", time);
296          logger.cleanup();
297       }
298    }
299 }
300
Popular Tags