ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/StringMapLoops.java
Revision: 1.15
Committed: Sun Oct 23 03:03:24 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.14: +2 -1 lines
Log Message:
fix deprecation warnings for Class#newInstance

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 dl 1.12 import java.io.*;
8     import java.math.*;
9 dl 1.1 import java.util.*;
10     import java.util.concurrent.*;
11    
12     public class StringMapLoops {
13 dl 1.12 static int nkeys = 147456;
14 dl 1.1 static int pinsert = 60;
15 dl 1.12 static int premove = 5;
16 dl 1.1 static int maxThreads = 100;
17 dl 1.3 static int nops = 8000000;
18 dl 1.1 static int removesPerMaxRandom;
19     static int insertsPerMaxRandom;
20    
21     static final ExecutorService pool = Executors.newCachedThreadPool();
22    
23     public static void main(String[] args) throws Exception {
24    
25 jsr166 1.11 Class<?> mapClass = null;
26 dl 1.1 if (args.length > 0) {
27     try {
28     mapClass = Class.forName(args[0]);
29 jsr166 1.5 } catch (ClassNotFoundException e) {
30 dl 1.1 throw new RuntimeException("Class " + args[0] + " not found.");
31     }
32     }
33 jsr166 1.4 else
34 dl 1.1 mapClass = java.util.concurrent.ConcurrentHashMap.class;
35    
36 jsr166 1.4 if (args.length > 1)
37 dl 1.1 maxThreads = Integer.parseInt(args[1]);
38    
39 jsr166 1.4 if (args.length > 2)
40 dl 1.1 nkeys = Integer.parseInt(args[2]);
41    
42 jsr166 1.4 if (args.length > 3)
43 dl 1.1 pinsert = Integer.parseInt(args[3]);
44    
45 jsr166 1.4 if (args.length > 4)
46 dl 1.1 premove = Integer.parseInt(args[4]);
47    
48 jsr166 1.4 if (args.length > 5)
49 dl 1.1 nops = Integer.parseInt(args[5]);
50    
51     // normalize probabilities wrt random number generator
52     removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL));
53     insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL));
54 jsr166 1.4
55 dl 1.1 System.out.print("Class: " + mapClass.getName());
56     System.out.print(" threads: " + maxThreads);
57     System.out.print(" size: " + nkeys);
58     System.out.print(" ins: " + pinsert);
59     System.out.print(" rem: " + premove);
60     System.out.print(" ops: " + nops);
61     System.out.println();
62    
63 dl 1.12 String[] key = new String[nkeys];
64     initStringKeys(key, nkeys);
65     // String[] key = makeKeys(nkeys);
66 dl 1.1
67     int warmups = 2;
68 jsr166 1.10 for (int k = 1, i = 1; i <= maxThreads;) {
69 dl 1.1 Thread.sleep(100);
70     test(i, nkeys, key, mapClass);
71     shuffleKeys(key);
72     if (warmups > 0)
73     --warmups;
74     else if (i == k) {
75     k = i << 1;
76     i = i + (i >>> 1);
77 jsr166 1.4 }
78 dl 1.1 else if (i == 1 && k == 2) {
79     i = k;
80     warmups = 1;
81     }
82 jsr166 1.4 else
83 dl 1.1 i = k;
84     }
85 dl 1.3 for (int j = 0; j < 10; ++j) {
86     Thread.sleep(100);
87     test(1, nkeys, key, mapClass);
88     // shuffleKeys(key);
89     }
90 dl 1.1 pool.shutdown();
91     }
92    
93     static void shuffleKeys(String[] key) {
94     Random rng = new Random();
95     for (int i = key.length; i > 1; --i) {
96     int j = rng.nextInt(i);
97     String tmp = key[j];
98     key[j] = key[i-1];
99     key[i-1] = tmp;
100     }
101     }
102    
103 jsr166 1.11 static void test(int i, int nkeys, String[] key, Class<?> mapClass) throws Exception {
104 dl 1.1 System.out.print("Threads: " + i + "\t:");
105 jsr166 1.15 Map<String, String> map =
106     (Map<String,String>) mapClass.getConstructor().newInstance();
107 dl 1.1 // Uncomment to start with a non-empty table
108     // for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied
109     // map.put(key[j], key[j]);
110 dl 1.3
111 dl 1.1 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
112     CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
113 jsr166 1.4 for (int t = 0; t < i; ++t)
114 dl 1.1 pool.execute(new Runner(t, map, key, barrier));
115     barrier.await();
116     barrier.await();
117     long time = timer.getTime();
118 jsr166 1.6 long tpo = time / (i * (long) nops);
119 dl 1.1 System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
120 jsr166 1.6 double secs = (double) time / 1000000000.0;
121 dl 1.1 System.out.println("\t " + secs + "s run time");
122     map.clear();
123     }
124    
125     static class Runner implements Runnable {
126     final Map<String,String> map;
127     final String[] key;
128     final LoopHelpers.SimpleRandom rng;
129     final CyclicBarrier barrier;
130     int position;
131     int total;
132    
133 dl 1.12 Runner(int id, Map<String,String> map, String[] key, CyclicBarrier barrier) {
134 jsr166 1.4 this.map = map;
135     this.key = key;
136 dl 1.1 this.barrier = barrier;
137 jsr166 1.4 position = key.length / 2;
138 dl 1.1 rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
139     rng.next();
140     }
141    
142     int step() {
143 jsr166 1.9 // random-walk around key positions, bunching accesses
144 dl 1.1 int r = rng.next();
145     position += (r & 7) - 3;
146 jsr166 1.4 while (position >= key.length) position -= key.length;
147 dl 1.1 while (position < 0) position += key.length;
148    
149     String k = key[position];
150     String x = map.get(k);
151    
152     if (x != null) {
153     if (r < removesPerMaxRandom) {
154     if (map.remove(k) != null) {
155     position = total % key.length; // move from position
156     return 2;
157     }
158     }
159     }
160     else if (r < insertsPerMaxRandom) {
161     ++position;
162 dl 1.12 // map.putIfAbsent(k, k);
163 dl 1.1 map.put(k, k);
164     return 2;
165 jsr166 1.4 }
166 dl 1.1
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 dl 1.12
185     static final String wordFile = "testwords.txt";
186    
187     // Read in String keys from file if possible
188     static void initStringKeys(String[] keys, int n) throws Exception {
189     FileInputStream fr = null;
190     try {
191     fr = new FileInputStream(wordFile);
192     } catch (IOException ex) {
193     System.out.println("No word file. Using String.valueOf(i)");
194     for (int i = 0; i < n; i++)
195     keys[i] = String.valueOf(i);
196     return;
197     }
198    
199     BufferedInputStream in = new BufferedInputStream(fr);
200     int k = 0;
201     outer:while (k < n) {
202     StringBuffer sb = new StringBuffer();
203     for (;;) {
204     int c = in.read();
205     if (c < 0)
206     break outer;
207     char ch = (char) c;
208     if (ch == '\n') {
209     keys[k++] = sb.toString();
210     break;
211     }
212     if (!Character.isWhitespace(ch))
213     sb.append(ch);
214     }
215     }
216     in.close();
217    
218     // fill up remaining keys with path-like compounds of previous pairs
219     int j = 0;
220     while (k < n)
221 jsr166 1.14 keys[k++] = keys[j++] + "/" + keys[j];
222 dl 1.12 }
223 dl 1.1 }