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

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.2 * 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 StringMapLoops {
11 jsr166 1.4 static int nkeys = 75000;
12 dl 1.1 static int pinsert = 60;
13 dl 1.3 static int premove = 2;
14 dl 1.1 static int maxThreads = 100;
15 dl 1.3 static int nops = 8000000;
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 jsr166 1.11 Class<?> mapClass = null;
24 dl 1.1 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     String[] key = makeKeys(nkeys);
62    
63     int warmups = 2;
64 jsr166 1.10 for (int k = 1, i = 1; i <= maxThreads;) {
65 dl 1.1 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 jsr166 1.4 }
74 dl 1.1 else if (i == 1 && k == 2) {
75     i = k;
76     warmups = 1;
77     }
78 jsr166 1.4 else
79 dl 1.1 i = k;
80     }
81 dl 1.3 for (int j = 0; j < 10; ++j) {
82     Thread.sleep(100);
83     test(1, nkeys, key, mapClass);
84     // shuffleKeys(key);
85     }
86 dl 1.1 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 jsr166 1.6 c[k++] = (char) (' ' + (r & 0x7f));
99 dl 1.1 r >>>= 8;
100 jsr166 1.6 c[k++] = (char) (' ' + (r & 0x7f));
101 dl 1.1 r >>>= 8;
102 jsr166 1.6 c[k++] = (char) (' ' + (r & 0x7f));
103 dl 1.1 r >>>= 8;
104 jsr166 1.6 c[k++] = (char) (' ' + (r & 0x7f));
105 dl 1.1 }
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 jsr166 1.11 static void test(int i, int nkeys, String[] key, Class<?> mapClass) throws Exception {
122 dl 1.1 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 dl 1.3
128 dl 1.1 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
129     CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
130 jsr166 1.4 for (int t = 0; t < i; ++t)
131 dl 1.1 pool.execute(new Runner(t, map, key, barrier));
132     barrier.await();
133     barrier.await();
134     long time = timer.getTime();
135 jsr166 1.6 long tpo = time / (i * (long) nops);
136 dl 1.1 System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
137 jsr166 1.6 double secs = (double) time / 1000000000.0;
138 dl 1.1 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 jsr166 1.8 Runner(int id, Map<String,String> map, String[] key, CyclicBarrier barrier) {
151 jsr166 1.4 this.map = map;
152     this.key = key;
153 dl 1.1 this.barrier = barrier;
154 jsr166 1.4 position = key.length / 2;
155 dl 1.1 rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
156     rng.next();
157     }
158    
159     int step() {
160 jsr166 1.9 // random-walk around key positions, bunching accesses
161 dl 1.1 int r = rng.next();
162     position += (r & 7) - 3;
163 jsr166 1.4 while (position >= key.length) position -= key.length;
164 dl 1.1 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 jsr166 1.4 }
182 dl 1.1
183     total += r;
184     return 1;
185     }
186    
187     public void run() {
188     try {
189     barrier.await();
190     int ops = nops;
191 jsr166 1.4 while (ops > 0)
192 dl 1.1 ops -= step();
193     barrier.await();
194     }
195     catch (Exception ex) {
196     ex.printStackTrace();
197     }
198     }
199     }
200     }