ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/MapLoops.java
Revision: 1.11
Committed: Thu Dec 18 18:43:22 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.10: +2 -2 lines
Log Message:
fix some [rawtypes] warnings

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 import java.util.*;
8 import java.util.concurrent.*;
9
10 public class MapLoops {
11 static int nkeys = 1000;
12 static int pinsert = 60;
13 static int premove = 2;
14 static int maxThreads = 100;
15 static int nops = 1000000;
16 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 } catch (ClassNotFoundException e) {
28 throw new RuntimeException("Class " + args[0] + " not found.");
29 }
30 }
31 else
32 mapClass = java.util.concurrent.ConcurrentHashMap.class;
33
34 if (args.length > 1)
35 maxThreads = Integer.parseInt(args[1]);
36
37 if (args.length > 2)
38 nkeys = Integer.parseInt(args[2]);
39
40 if (args.length > 3)
41 pinsert = Integer.parseInt(args[3]);
42
43 if (args.length > 4)
44 premove = Integer.parseInt(args[4]);
45
46 if (args.length > 5)
47 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
53 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 for (int k = 1, i = 1; i <= maxThreads;) {
63 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 }
71 else if (i == 1 && k == 2) {
72 i = k;
73 warmups = 1;
74 }
75 else
76 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 for (int i = 0; i < key.length; ++i)
85 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 shuffleKeys(key);
107 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
108 CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
109 for (int t = 0; t < i; ++t)
110 pool.execute(new Runner(t, map, key, barrier));
111 barrier.await();
112 barrier.await();
113 long time = timer.getTime();
114 long tpo = time / (i * (long) nops);
115 System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
116 double secs = (double) time / 1000000000.0;
117 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 final LoopHelpers.SimpleRandom rng;
125 final CyclicBarrier barrier;
126 int position;
127 int total;
128
129 Runner(int id, Map<Integer,Integer> map, Integer[] key, CyclicBarrier barrier) {
130 this.map = map;
131 this.key = key;
132 this.barrier = barrier;
133 position = key.length / 2;
134 rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
135 rng.next();
136 }
137
138 int step() {
139 // random-walk around key positions, bunching accesses
140 int r = rng.next();
141 position += (r & 7) - 3;
142 while (position >= key.length) position -= key.length;
143 while (position < 0) position += key.length;
144
145 Integer k = key[position];
146 Integer x = map.get(k);
147
148 if (x != null) {
149 if (x.intValue() != k.intValue())
150 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 }
164
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 while (ops > 0)
176 ops -= step();
177 barrier.await();
178 }
179 catch (Exception ex) {
180 ex.printStackTrace();
181 }
182 }
183 }
184 }