ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/MapLoops.java
Revision: 1.5
Committed: Mon Nov 2 23:42:46 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.3 * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 dl 1.1 */
6    
7     import java.util.*;
8     import java.util.concurrent.*;
9    
10     public class MapLoops {
11 jsr166 1.4 static int nkeys = 1000;
12 dl 1.1 static int pinsert = 60;
13     static int premove = 2;
14     static int maxThreads = 100;
15 dl 1.2 static int nops = 1000000;
16 dl 1.1 static int removesPerMaxRandom;
17     static int insertsPerMaxRandom;
18    
19     static final ExecutorService pool = Executors.newCachedThreadPool();
20    
21     public static void main(String[] args) throws Exception {
22    
23     Class mapClass = null;
24     if (args.length > 0) {
25     try {
26     mapClass = Class.forName(args[0]);
27 jsr166 1.5 } catch (ClassNotFoundException e) {
28 dl 1.1 throw new RuntimeException("Class " + args[0] + " not found.");
29     }
30     }
31 jsr166 1.4 else
32 dl 1.1 mapClass = java.util.concurrent.ConcurrentHashMap.class;
33    
34 jsr166 1.4 if (args.length > 1)
35 dl 1.1 maxThreads = Integer.parseInt(args[1]);
36    
37 jsr166 1.4 if (args.length > 2)
38 dl 1.1 nkeys = Integer.parseInt(args[2]);
39    
40 jsr166 1.4 if (args.length > 3)
41 dl 1.1 pinsert = Integer.parseInt(args[3]);
42    
43 jsr166 1.4 if (args.length > 4)
44 dl 1.1 premove = Integer.parseInt(args[4]);
45    
46 jsr166 1.4 if (args.length > 5)
47 dl 1.1 nops = Integer.parseInt(args[5]);
48    
49     // normalize probabilities wrt random number generator
50     removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL));
51     insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL));
52 jsr166 1.4
53 dl 1.1 System.out.print("Class: " + mapClass.getName());
54     System.out.print(" threads: " + maxThreads);
55     System.out.print(" size: " + nkeys);
56     System.out.print(" ins: " + pinsert);
57     System.out.print(" rem: " + premove);
58     System.out.print(" ops: " + nops);
59     System.out.println();
60    
61     int k = 1;
62     int warmups = 2;
63     for (int i = 1; i <= maxThreads;) {
64     Thread.sleep(100);
65     test(i, nkeys, mapClass);
66     if (warmups > 0)
67     --warmups;
68     else if (i == k) {
69     k = i << 1;
70     i = i + (i >>> 1);
71 jsr166 1.4 }
72 dl 1.1 else if (i == 1 && k == 2) {
73     i = k;
74     warmups = 1;
75     }
76 jsr166 1.4 else
77 dl 1.1 i = k;
78     }
79     pool.shutdown();
80     }
81    
82     static Integer[] makeKeys(int n) {
83     LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
84     Integer[] key = new Integer[n];
85 jsr166 1.4 for (int i = 0; i < key.length; ++i)
86 dl 1.1 key[i] = new Integer(rng.next());
87     return key;
88     }
89    
90     static void shuffleKeys(Integer[] key) {
91     Random rng = new Random();
92     for (int i = key.length; i > 1; --i) {
93     int j = rng.nextInt(i);
94     Integer tmp = key[j];
95     key[j] = key[i-1];
96     key[i-1] = tmp;
97     }
98     }
99    
100     static void test(int i, int nkeys, Class mapClass) throws Exception {
101     System.out.print("Threads: " + i + "\t:");
102     Map<Integer, Integer> map = (Map<Integer,Integer>)mapClass.newInstance();
103     Integer[] key = makeKeys(nkeys);
104     // Uncomment to start with a non-empty table
105     // for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied
106     // map.put(key[j], key[j]);
107 dl 1.2 shuffleKeys(key);
108 dl 1.1 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
109     CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
110 jsr166 1.4 for (int t = 0; t < i; ++t)
111 dl 1.2 pool.execute(new Runner(t, map, key, barrier));
112 dl 1.1 barrier.await();
113     barrier.await();
114     long time = timer.getTime();
115     long tpo = time / (i * (long)nops);
116     System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
117     double secs = (double)(time) / 1000000000.0;
118     System.out.println("\t " + secs + "s run time");
119     map.clear();
120     }
121    
122     static class Runner implements Runnable {
123     final Map<Integer,Integer> map;
124     final Integer[] key;
125 dl 1.2 final LoopHelpers.SimpleRandom rng;
126 dl 1.1 final CyclicBarrier barrier;
127     int position;
128     int total;
129    
130 dl 1.2 Runner(int id, Map<Integer,Integer> map, Integer[] key, CyclicBarrier barrier) {
131 jsr166 1.4 this.map = map;
132     this.key = key;
133 dl 1.1 this.barrier = barrier;
134 jsr166 1.4 position = key.length / 2;
135 dl 1.2 rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
136     rng.next();
137 dl 1.1 }
138    
139     int step() {
140     // random-walk around key positions, bunching accesses
141     int r = rng.next();
142     position += (r & 7) - 3;
143 jsr166 1.4 while (position >= key.length) position -= key.length;
144 dl 1.1 while (position < 0) position += key.length;
145    
146     Integer k = key[position];
147     Integer x = map.get(k);
148    
149     if (x != null) {
150 jsr166 1.4 if (x.intValue() != k.intValue())
151 dl 1.1 throw new Error("bad mapping: " + x + " to " + k);
152    
153     if (r < removesPerMaxRandom) {
154     if (map.remove(k) != null) {
155     position = total % key.length; // move from position
156     return 2;
157     }
158     }
159     }
160     else if (r < insertsPerMaxRandom) {
161     ++position;
162     map.put(k, k);
163     return 2;
164 jsr166 1.4 }
165 dl 1.1
166     // Uncomment to add a little computation between accesses
167     // total += LoopHelpers.compute1(k.intValue());
168     total += r;
169     return 1;
170     }
171    
172     public void run() {
173     try {
174     barrier.await();
175     int ops = nops;
176 jsr166 1.4 while (ops > 0)
177 dl 1.1 ops -= step();
178     barrier.await();
179     }
180     catch (Exception ex) {
181     ex.printStackTrace();
182     }
183     }
184     }
185     }