ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/MapLoops.java
Revision: 1.10
Committed: Thu Dec 18 18:13:06 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +1 -2 lines
Log Message:
move k = 1 into loop initializer

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 jsr166 1.7 * http://creativecommons.org/publicdomain/zero/1.0/
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 warmups = 2;
62 jsr166 1.10 for (int k = 1, i = 1; i <= maxThreads;) {
63 dl 1.1 Thread.sleep(100);
64     test(i, nkeys, mapClass);
65     if (warmups > 0)
66     --warmups;
67     else if (i == k) {
68     k = i << 1;
69     i = i + (i >>> 1);
70 jsr166 1.4 }
71 dl 1.1 else if (i == 1 && k == 2) {
72     i = k;
73     warmups = 1;
74     }
75 jsr166 1.4 else
76 dl 1.1 i = k;
77     }
78     pool.shutdown();
79     }
80    
81     static Integer[] makeKeys(int n) {
82     LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
83     Integer[] key = new Integer[n];
84 jsr166 1.4 for (int i = 0; i < key.length; ++i)
85 dl 1.1 key[i] = new Integer(rng.next());
86     return key;
87     }
88    
89     static void shuffleKeys(Integer[] key) {
90     Random rng = new Random();
91     for (int i = key.length; i > 1; --i) {
92     int j = rng.nextInt(i);
93     Integer tmp = key[j];
94     key[j] = key[i-1];
95     key[i-1] = tmp;
96     }
97     }
98    
99     static void test(int i, int nkeys, Class mapClass) throws Exception {
100     System.out.print("Threads: " + i + "\t:");
101     Map<Integer, Integer> map = (Map<Integer,Integer>)mapClass.newInstance();
102     Integer[] key = makeKeys(nkeys);
103     // Uncomment to start with a non-empty table
104     // for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied
105     // map.put(key[j], key[j]);
106 dl 1.2 shuffleKeys(key);
107 dl 1.1 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
108     CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
109 jsr166 1.4 for (int t = 0; t < i; ++t)
110 dl 1.2 pool.execute(new Runner(t, map, key, barrier));
111 dl 1.1 barrier.await();
112     barrier.await();
113     long time = timer.getTime();
114 jsr166 1.6 long tpo = time / (i * (long) nops);
115 dl 1.1 System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
116 jsr166 1.6 double secs = (double) time / 1000000000.0;
117 dl 1.1 System.out.println("\t " + secs + "s run time");
118     map.clear();
119     }
120    
121     static class Runner implements Runnable {
122     final Map<Integer,Integer> map;
123     final Integer[] key;
124 dl 1.2 final LoopHelpers.SimpleRandom rng;
125 dl 1.1 final CyclicBarrier barrier;
126     int position;
127     int total;
128    
129 jsr166 1.8 Runner(int id, Map<Integer,Integer> map, Integer[] key, CyclicBarrier barrier) {
130 jsr166 1.4 this.map = map;
131     this.key = key;
132 dl 1.1 this.barrier = barrier;
133 jsr166 1.4 position = key.length / 2;
134 dl 1.2 rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
135     rng.next();
136 dl 1.1 }
137    
138     int step() {
139 jsr166 1.9 // random-walk around key positions, bunching accesses
140 dl 1.1 int r = rng.next();
141     position += (r & 7) - 3;
142 jsr166 1.4 while (position >= key.length) position -= key.length;
143 dl 1.1 while (position < 0) position += key.length;
144    
145     Integer k = key[position];
146     Integer x = map.get(k);
147    
148     if (x != null) {
149 jsr166 1.4 if (x.intValue() != k.intValue())
150 dl 1.1 throw new Error("bad mapping: " + x + " to " + k);
151    
152     if (r < removesPerMaxRandom) {
153     if (map.remove(k) != null) {
154     position = total % key.length; // move from position
155     return 2;
156     }
157     }
158     }
159     else if (r < insertsPerMaxRandom) {
160     ++position;
161     map.put(k, k);
162     return 2;
163 jsr166 1.4 }
164 dl 1.1
165     // Uncomment to add a little computation between accesses
166     // total += LoopHelpers.compute1(k.intValue());
167     total += r;
168     return 1;
169     }
170    
171     public void run() {
172     try {
173     barrier.await();
174     int ops = nops;
175 jsr166 1.4 while (ops > 0)
176 dl 1.1 ops -= step();
177     barrier.await();
178     }
179     catch (Exception ex) {
180     ex.printStackTrace();
181     }
182     }
183     }
184     }