KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > prevayler > demos > scalability > TransactionTestRun


1 package org.prevayler.demos.scalability;
2
3
4 /** Tests insert, update and delete scalability.
5 */

6 public class TransactionTestRun extends ScalabilityTestRun {
7
8     private int halfTheObjects;
9
10
11     public TransactionTestRun(ScalabilityTestSubject subject, int numberOfObjects, int minThreads, int maxThreads) {
12         super(subject, numberOfObjects, minThreads, maxThreads);
13     }
14
15     protected String JavaDoc name() {
16         return "Transaction Test";
17     }
18
19     protected void prepare() {
20         super.prepare();
21         halfTheObjects = numberOfObjects / 2;
22     }
23
24     /**
25     * Deletes records from id zero to id halfTheObjects - 1.
26     * Updates records from id halfTheObjects to id numberOfObjects - 1.
27     * Inserts records from id numberOfObjects to id numberOfObjects + halfTheObjects - 1.
28     * Every time halfTheObjects operations have completed, all ranges are shifted up by halfTheObjects.
29     * Example for one million objects:
30     * Deletes records from id 0000000 to id 0499999.
31     * Updates records from id 0500000 to id 0999999.
32     * Inserts records from id 1000000 to id 1499999.
33     * Every time 500000 operations have completed, all ranges are shifted up by 500000.
34     */

35     protected void executeOperation(Object JavaDoc connection, long operationSequence) {
36         Record recordToInsert = new Record(numberOfObjects + operationSequence);
37         long idToDelete = spreadId(operationSequence);
38         Record recordToUpdate = new Record(halfTheObjects + idToDelete);
39
40         ((TransactionConnection)connection).performTransaction(recordToInsert, recordToUpdate, idToDelete);
41     }
42
43
44     /** Spreads out the id values so that deletes and updates are not done contiguously.
45     */

46     private long spreadId(long id) {
47         return (id / halfTheObjects) * halfTheObjects //Step function.
48
+ ((id * 16807) % halfTheObjects); //16807 == 7 * 7 * 7 * 7 * 7. 16807 is relatively prime to 50000, 500000 and 5000000. This guarantees that all ids in the range will be covered.
49
}
50 }
51
Popular Tags