ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/StringMapLoops.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 StringMapLoops {
11 static int nkeys = 75000;
12 static int pinsert = 60;
13 static int premove = 2;
14 static int maxThreads = 100;
15 static int nops = 8000000;
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 String[] key = makeKeys(nkeys);
62
63 int warmups = 2;
64 for (int k = 1, i = 1; i <= maxThreads;) {
65 Thread.sleep(100);
66 test(i, nkeys, key, mapClass);
67 shuffleKeys(key);
68 if (warmups > 0)
69 --warmups;
70 else if (i == k) {
71 k = i << 1;
72 i = i + (i >>> 1);
73 }
74 else if (i == 1 && k == 2) {
75 i = k;
76 warmups = 1;
77 }
78 else
79 i = k;
80 }
81 for (int j = 0; j < 10; ++j) {
82 Thread.sleep(100);
83 test(1, nkeys, key, mapClass);
84 // shuffleKeys(key);
85 }
86 pool.shutdown();
87 }
88
89 static String[] makeKeys(int n) {
90 LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
91 String[] key = new String[n];
92 for (int i = 0; i < key.length; ++i) {
93 int k = 0;
94 int len = 1 + (rng.next() & 0xf);
95 char[] c = new char[len * 4];
96 for (int j = 0; j < len; ++j) {
97 int r = rng.next();
98 c[k++] = (char) (' ' + (r & 0x7f));
99 r >>>= 8;
100 c[k++] = (char) (' ' + (r & 0x7f));
101 r >>>= 8;
102 c[k++] = (char) (' ' + (r & 0x7f));
103 r >>>= 8;
104 c[k++] = (char) (' ' + (r & 0x7f));
105 }
106 key[i] = new String(c);
107 }
108 return key;
109 }
110
111 static void shuffleKeys(String[] key) {
112 Random rng = new Random();
113 for (int i = key.length; i > 1; --i) {
114 int j = rng.nextInt(i);
115 String tmp = key[j];
116 key[j] = key[i-1];
117 key[i-1] = tmp;
118 }
119 }
120
121 static void test(int i, int nkeys, String[] key, Class<?> mapClass) throws Exception {
122 System.out.print("Threads: " + i + "\t:");
123 Map<String, String> map = (Map<String,String>)mapClass.newInstance();
124 // Uncomment to start with a non-empty table
125 // for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied
126 // map.put(key[j], key[j]);
127
128 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
129 CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
130 for (int t = 0; t < i; ++t)
131 pool.execute(new Runner(t, map, key, barrier));
132 barrier.await();
133 barrier.await();
134 long time = timer.getTime();
135 long tpo = time / (i * (long) nops);
136 System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
137 double secs = (double) time / 1000000000.0;
138 System.out.println("\t " + secs + "s run time");
139 map.clear();
140 }
141
142 static class Runner implements Runnable {
143 final Map<String,String> map;
144 final String[] key;
145 final LoopHelpers.SimpleRandom rng;
146 final CyclicBarrier barrier;
147 int position;
148 int total;
149
150 Runner(int id, Map<String,String> map, String[] key, CyclicBarrier barrier) {
151 this.map = map;
152 this.key = key;
153 this.barrier = barrier;
154 position = key.length / 2;
155 rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
156 rng.next();
157 }
158
159 int step() {
160 // random-walk around key positions, bunching accesses
161 int r = rng.next();
162 position += (r & 7) - 3;
163 while (position >= key.length) position -= key.length;
164 while (position < 0) position += key.length;
165
166 String k = key[position];
167 String x = map.get(k);
168
169 if (x != null) {
170 if (r < removesPerMaxRandom) {
171 if (map.remove(k) != null) {
172 position = total % key.length; // move from position
173 return 2;
174 }
175 }
176 }
177 else if (r < insertsPerMaxRandom) {
178 ++position;
179 map.put(k, k);
180 return 2;
181 }
182
183 total += r;
184 return 1;
185 }
186
187 public void run() {
188 try {
189 barrier.await();
190 int ops = nops;
191 while (ops > 0)
192 ops -= step();
193 barrier.await();
194 }
195 catch (Exception ex) {
196 ex.printStackTrace();
197 }
198 }
199 }
200 }