ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CollectionLoops.java
Revision: 1.9
Committed: Thu Dec 18 18:43:22 2014 UTC (9 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +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.6 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7    
8     import java.util.*;
9     import java.util.concurrent.*;
10    
11     public class CollectionLoops {
12     static int pinsert = 100;
13     static int premove = 1;
14     static int maxThreads = 48;
15     static int removesPerMaxRandom;
16     static int insertsPerMaxRandom;
17     static volatile int checkSum = 0;
18     static boolean print = false;
19    
20     static final ExecutorService pool = Executors.newCachedThreadPool();
21    
22     public static void main(String[] args) throws Exception {
23     int nkeys = 10000;
24     int nops = 100000;
25    
26 jsr166 1.9 Class<?> collectionClass = null;
27 dl 1.1 if (args.length > 0) {
28     try {
29     collectionClass = Class.forName(args[0]);
30 jsr166 1.4 } catch (ClassNotFoundException e) {
31 dl 1.1 throw new RuntimeException("Class " + args[0] + " not found.");
32     }
33     }
34    
35 jsr166 1.3 if (args.length > 1)
36 dl 1.1 maxThreads = Integer.parseInt(args[1]);
37    
38 jsr166 1.3 if (args.length > 2)
39 dl 1.1 nkeys = Integer.parseInt(args[2]);
40    
41 jsr166 1.3 if (args.length > 3)
42 dl 1.1 pinsert = Integer.parseInt(args[3]);
43    
44 jsr166 1.3 if (args.length > 4)
45 dl 1.1 premove = Integer.parseInt(args[4]);
46    
47 jsr166 1.3 if (args.length > 5)
48 dl 1.1 nops = Integer.parseInt(args[5]);
49    
50     // normalize probabilities wrt random number generator
51     removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL));
52     insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL));
53 jsr166 1.3
54 dl 1.1 System.out.print("Class: " + collectionClass.getName());
55     System.out.print(" threads: " + maxThreads);
56     System.out.print(" size: " + nkeys);
57     System.out.print(" ins: " + pinsert);
58     System.out.print(" rem: " + premove);
59     System.out.print(" ops: " + nops);
60     System.out.println();
61    
62     // warmup
63     test(1, 100, 100, collectionClass);
64     test(2, 100, 100, collectionClass);
65     test(4, 100, 100, collectionClass);
66     print = true;
67    
68     int warmups = 2;
69 jsr166 1.8 for (int k = 1, i = 1; i <= maxThreads;) {
70 dl 1.1 Thread.sleep(100);
71     test(i, nkeys, nops, collectionClass);
72     if (warmups > 0)
73     --warmups;
74     else if (i == k) {
75     k = i << 1;
76     i = i + (i >>> 1);
77 jsr166 1.3 }
78 dl 1.1 else if (i == 1 && k == 2) {
79     i = k;
80     warmups = 1;
81     }
82 jsr166 1.3 else
83 dl 1.1 i = k;
84     }
85     pool.shutdown();
86     }
87    
88     static Integer[] makeKeys(int n) {
89     LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
90     Integer[] key = new Integer[n];
91 jsr166 1.3 for (int i = 0; i < key.length; ++i)
92 dl 1.1 key[i] = new Integer(rng.next());
93     return key;
94     }
95    
96     static void shuffleKeys(Integer[] key) {
97     Random rng = new Random();
98     for (int i = key.length; i > 1; --i) {
99     int j = rng.nextInt(i);
100     Integer tmp = key[j];
101     key[j] = key[i-1];
102     key[i-1] = tmp;
103     }
104     }
105    
106 jsr166 1.9 static void test(int i, int nk, int nops, Class<?> collectionClass) throws Exception {
107 dl 1.1 if (print)
108     System.out.print("Threads: " + i + "\t:");
109     Collection<Integer> collection = (Collection<Integer>)collectionClass.newInstance();
110     Integer[] key = makeKeys(nk);
111     // Uncomment to start with a non-empty table
112 jsr166 1.3 for (int j = 0; j < nk; j += 2)
113 dl 1.1 collection.add(key[j]);
114     shuffleKeys(key);
115     LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
116     CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
117 jsr166 1.3 for (int t = 0; t < i; ++t)
118 dl 1.1 pool.execute(new Runner(t, collection, key, barrier, nops));
119     barrier.await();
120     barrier.await();
121     long time = timer.getTime();
122 jsr166 1.5 long tpo = time / (i * (long) nops);
123 dl 1.1 if (print)
124     System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
125 jsr166 1.5 double secs = (double) time / 1000000000.0;
126 dl 1.1 if (print)
127     System.out.print("\t " + secs + "s run time");
128     if (checkSum == 0) System.out.print(" ");
129     if (print)
130     System.out.println();
131     collection.clear();
132     }
133    
134     static class Runner implements Runnable {
135     final Collection<Integer> collection;
136     final Integer[] key;
137     final LoopHelpers.SimpleRandom rng;
138     final CyclicBarrier barrier;
139     int position;
140     int total;
141     int nops;
142    
143 jsr166 1.7 Runner(int id, Collection<Integer> collection, Integer[] key, CyclicBarrier barrier, int nops) {
144 jsr166 1.3 this.collection = collection;
145     this.key = key;
146 dl 1.1 this.barrier = barrier;
147     this.nops = nops;
148 jsr166 1.3 position = key.length / (id + 1);
149 dl 1.1 rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
150     rng.next();
151     }
152    
153     public void run() {
154     try {
155     barrier.await();
156     int p = position;
157     int ops = nops;
158     Collection<Integer> c = collection;
159     while (ops > 0) {
160     int r = rng.next();
161     p += (r & 7) - 3;
162 jsr166 1.3 while (p >= key.length) p -= key.length;
163 dl 1.1 while (p < 0) p += key.length;
164    
165     Integer k = key[p];
166     if (c.contains(k)) {
167     if (r < removesPerMaxRandom) {
168     if (c.remove(k)) {
169 jsr166 1.3 p = Math.abs(total % key.length);
170 dl 1.1 ops -= 2;
171     continue;
172     }
173     }
174     }
175     else if (r < insertsPerMaxRandom) {
176     ++p;
177     ops -= 2;
178     c.add(k);
179     continue;
180 jsr166 1.3 }
181    
182 dl 1.1 total += LoopHelpers.compute6(k.intValue());
183     --ops;
184     }
185     checkSum += total;
186     barrier.await();
187     }
188     catch (Exception ex) {
189     throw new RuntimeException(ex);
190     }
191     }
192     }
193     }