ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/StringMapLoops.java
Revision: 1.6
Committed: Tue Nov 3 01:04:02 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +6 -6 lines
Log Message:
coding style

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