-
Notifications
You must be signed in to change notification settings - Fork 554
/
NBHML_Tester2.java
656 lines (590 loc) · 23 KB
/
NBHML_Tester2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jctools.maps.nbhm_test;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.jctools.maps.NonBlockingHashMapLong;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
// Test NonBlockingHashMapLong via JUnit
public class NBHML_Tester2
{
static private NonBlockingHashMapLong<String> _nbhml;
@BeforeClass
public static void setUp()
{
_nbhml = new NonBlockingHashMapLong<>();
}
@AfterClass
public static void tearDown()
{
_nbhml = null;
}
// Test some basic stuff; add a few keys, remove a few keys
@Test
public void testBasic()
{
assertTrue(_nbhml.isEmpty());
assertThat(_nbhml.put(1, "v1"), nullValue());
checkSizes(1);
assertThat(_nbhml.putIfAbsent(2, "v2"), nullValue());
checkSizes(2);
assertTrue(_nbhml.containsKey(2));
assertThat(_nbhml.put(1, "v1a"), is("v1"));
assertThat(_nbhml.put(2, "v2a"), is("v2"));
checkSizes(2);
assertThat(_nbhml.putIfAbsent(2, "v2b"), is("v2a"));
assertThat(_nbhml.remove(1), is("v1a"));
assertFalse(_nbhml.containsKey(1));
checkSizes(1);
assertThat(_nbhml.remove(1), nullValue());
assertThat(_nbhml.remove(2), is("v2a"));
checkSizes(0);
assertThat(_nbhml.remove(2), nullValue());
assertThat(_nbhml.remove("k3"), nullValue());
assertTrue(_nbhml.isEmpty());
assertThat(_nbhml.put(0, "v0"), nullValue());
assertTrue(_nbhml.containsKey(0));
checkSizes(1);
assertThat(_nbhml.remove(0), is("v0"));
assertFalse(_nbhml.containsKey(0));
checkSizes(0);
assertThat(_nbhml.replace(0, "v0"), nullValue());
assertFalse(_nbhml.containsKey(0));
assertThat(_nbhml.put(0, "v0"), nullValue());
assertEquals(_nbhml.replace(0, "v0a"), "v0");
assertEquals(_nbhml.get(0), "v0a");
assertThat(_nbhml.remove(0), is("v0a"));
assertFalse(_nbhml.containsKey(0));
checkSizes(0);
assertThat(_nbhml.replace(1, "v1"), nullValue());
assertFalse(_nbhml.containsKey(1));
assertThat(_nbhml.put(1, "v1"), nullValue());
assertEquals(_nbhml.replace(1, "v1a"), "v1");
assertEquals(_nbhml.get(1), "v1a");
assertThat(_nbhml.remove(1), is("v1a"));
assertFalse(_nbhml.containsKey(1));
checkSizes(0);
// Simple insert of simple keys, with no reprobing on insert until the
// table gets full exactly. Then do a 'get' on the totally full table.
NonBlockingHashMapLong<Object> map = new NonBlockingHashMapLong<Object>(32);
for (int i = 1; i < 32; i++)
{
map.put(i, new Object());
}
map.get(33); // this causes a NPE
}
// Check all iterators for correct size counts
private void checkSizes(int expectedSize)
{
assertEquals("size()", _nbhml.size(), expectedSize);
Collection<String> vals = _nbhml.values();
checkSizes("values()", vals.size(), vals.iterator(), expectedSize);
Set<Long> keys = _nbhml.keySet();
checkSizes("keySet()", keys.size(), keys.iterator(), expectedSize);
Set<Map.Entry<Long, String>> ents = _nbhml.entrySet();
checkSizes("entrySet()", ents.size(), ents.iterator(), expectedSize);
}
// Check that the iterator iterates the correct number of times
private void checkSizes(String msg, int sz, Iterator it, int expectedSize)
{
assertEquals(msg, expectedSize, sz);
int result = 0;
while (it.hasNext())
{
result++;
it.next();
}
assertEquals(msg, expectedSize, result);
}
@Test
public void testIterationBig2()
{
final int CNT = 10000;
assertThat(_nbhml.size(), is(0));
final String v = "v";
for (int i = 0; i < CNT; i++)
{
_nbhml.put(i, v);
String s = _nbhml.get(i);
assertThat(s, is(v));
}
assertThat(_nbhml.size(), is(CNT));
_nbhml.clear();
}
@Test
public void testIteration()
{
assertTrue(_nbhml.isEmpty());
assertThat(_nbhml.put(1, "v1"), nullValue());
assertThat(_nbhml.put(2, "v2"), nullValue());
String str1 = "";
for (Map.Entry<Long, String> e : _nbhml.entrySet())
{
str1 += e.getKey();
}
assertThat("found all entries", str1, anyOf(is("12"), is("21")));
String str2 = "";
for (Long key : _nbhml.keySet())
{
str2 += key;
}
assertThat("found all keys", str2, anyOf(is("12"), is("21")));
String str3 = "";
for (String val : _nbhml.values())
{
str3 += val;
}
assertThat("found all vals", str3, anyOf(is("v1v2"), is("v2v1")));
assertThat("toString works", _nbhml.toString(), anyOf(is("{1=v1, 2=v2}"), is("{2=v2, 1=v1}")));
_nbhml.clear();
}
@Test
public void testSerial()
{
assertTrue(_nbhml.isEmpty());
assertThat(_nbhml.put(0x12345678L, "v1"), nullValue());
assertThat(_nbhml.put(0x87654321L, "v2"), nullValue());
// Serialize it out
try
{
FileOutputStream fos = new FileOutputStream("NBHML_test.txt");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(_nbhml);
out.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
// Read it back
try
{
File f = new File("NBHML_test.txt");
FileInputStream fis = new FileInputStream(f);
ObjectInputStream in = new ObjectInputStream(fis);
NonBlockingHashMapLong nbhml = (NonBlockingHashMapLong) in.readObject();
in.close();
assertEquals(_nbhml.toString(), nbhml.toString());
if (!f.delete())
{
throw new IOException("delete failed");
}
}
catch (IOException | ClassNotFoundException ex)
{
ex.printStackTrace();
}
}
@Test
public void testIterationBig()
{
final int CNT = 10000;
assertThat(_nbhml.size(), is(0));
for (int i = 0; i < CNT; i++)
{
_nbhml.put(i, "v" + i);
}
assertThat(_nbhml.size(), is(CNT));
int sz = 0;
int sum = 0;
for (long x : _nbhml.keySet())
{
sz++;
sum += x;
assertTrue(x >= 0 && x <= (CNT - 1));
}
assertThat("Found 10000 ints", sz, is(CNT));
assertThat("Found all integers in list", sum, is(CNT * (CNT - 1) / 2));
assertThat("can remove 3", _nbhml.remove(3), is("v3"));
assertThat("can remove 4", _nbhml.remove(4), is("v4"));
sz = 0;
sum = 0;
for (long x : _nbhml.keySet())
{
sz++;
sum += x;
assertTrue(x >= 0 && x <= (CNT - 1));
String v = _nbhml.get(x);
assertThat("", v.charAt(0), is('v'));
assertThat("", x, is(Long.parseLong(v.substring(1))));
}
assertThat("Found " + (CNT - 2) + " ints", sz, is(CNT - 2));
assertThat("Found all integers in list", sum, is(CNT * (CNT - 1) / 2 - (3 + 4)));
_nbhml.clear();
}
// Do some simple concurrent testing
@Test
public void testConcurrentSimple() throws InterruptedException
{
final NonBlockingHashMapLong<String> nbhml = new NonBlockingHashMapLong<>();
// In 2 threads, add & remove even & odd elements concurrently
final int num_thrds = 2;
Thread ts[] = new Thread[num_thrds];
for (int i = 1; i < num_thrds; i++)
{
final int x = i;
ts[i] = new Thread()
{
public void run()
{
work_helper(nbhml, x, num_thrds);
}
};
}
for (int i = 1; i < num_thrds; i++)
{
ts[i].start();
}
work_helper(nbhml, 0, num_thrds);
for (int i = 1; i < num_thrds; i++)
{
ts[i].join();
}
// In the end, all members should be removed
StringBuilder buf = new StringBuilder();
buf.append("Should be emptyset but has these elements: {");
boolean found = false;
for (long x : nbhml.keySet())
{
buf.append(" ").append(x);
found = true;
}
if (found)
{
System.out.println(buf + " }");
}
assertThat("concurrent size=0", nbhml.size(), is(0));
assertThat("keySet size==0", nbhml.keySet().size(), is(0));
}
void work_helper(NonBlockingHashMapLong<String> nbhml, int d, int num_thrds)
{
String thrd = "T" + d;
final int ITERS = 20000;
for (int j = 0; j < 10; j++)
{
//long start = System.nanoTime();
for (int i = d; i < ITERS; i += num_thrds)
{
assertThat("key " + i + " not in there, so putIfAbsent must work",
nbhml.putIfAbsent((long) i, thrd), is((String) null));
}
for (int i = d; i < ITERS; i += num_thrds)
{
assertTrue(nbhml.remove((long) i, thrd));
}
//double delta_nanos = System.nanoTime()-start;
//double delta_secs = delta_nanos/1000000000.0;
//double ops = ITERS*2;
//System.out.println("Thrd"+thrd+" "+(ops/delta_secs)+" ops/sec size="+nbhml.size());
}
}
// --- Customer Test Case 1 ------------------------------------------------
@Test
public final void testNonBlockingHashMapSize()
{
NonBlockingHashMapLong<String> items = new NonBlockingHashMapLong<>();
items.put(100L, "100");
items.put(101L, "101");
assertEquals("keySet().size()", 2, items.keySet().size());
assertTrue("keySet().contains(100)", items.keySet().contains(100L));
assertTrue("keySet().contains(101)", items.keySet().contains(101L));
assertEquals("values().size()", 2, items.values().size());
assertTrue("values().contains(\"100\")", items.values().contains("100"));
assertTrue("values().contains(\"101\")", items.values().contains("101"));
assertEquals("entrySet().size()", 2, items.entrySet().size());
boolean found100 = false;
boolean found101 = false;
for (Map.Entry<Long, String> entry : items.entrySet())
{
if (entry.getKey().equals(100L))
{
assertEquals("entry[100].getValue()==\"100\"", "100", entry.getValue());
found100 = true;
}
else if (entry.getKey().equals(101L))
{
assertEquals("entry[101].getValue()==\"101\"", "101", entry.getValue());
found101 = true;
}
}
assertTrue("entrySet().contains([100])", found100);
assertTrue("entrySet().contains([101])", found101);
}
// --- Customer Test Case 2 ------------------------------------------------
// Concurrent insertion & then iterator test.
@Test
public void testNonBlockingHashMapIterator() throws InterruptedException
{
final int ITEM_COUNT1 = 1000;
final int THREAD_COUNT = 5;
final int PER_CNT = ITEM_COUNT1 / THREAD_COUNT;
final int ITEM_COUNT = PER_CNT * THREAD_COUNT; // fix roundoff for odd thread counts
NonBlockingHashMapLong<TestKey> nbhml = new NonBlockingHashMapLong<>();
// use a barrier to open the gate for all threads at once to avoid rolling
// start and no actual concurrency
final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT);
final ExecutorService ex = Executors.newFixedThreadPool(THREAD_COUNT);
final CompletionService<Object> co = new ExecutorCompletionService<>(ex);
for (int i = 0; i < THREAD_COUNT; i++)
{
co.submit(new NBHMLFeeder(nbhml, PER_CNT, barrier, i * PER_CNT));
}
for (int retCount = 0; retCount < THREAD_COUNT; retCount++)
{
co.take();
}
ex.shutdown();
assertEquals("values().size()", ITEM_COUNT, nbhml.values().size());
assertEquals("entrySet().size()", ITEM_COUNT, nbhml.entrySet().size());
int itemCount = 0;
for (TestKey K : nbhml.values())
{
itemCount++;
}
assertEquals("values().iterator() count", ITEM_COUNT, itemCount);
}
// --- Customer Test Case 3 ------------------------------------------------
private TestKeyFeeder getTestKeyFeeder()
{
final TestKeyFeeder feeder = new TestKeyFeeder();
feeder.checkedPut(10401000001844L, 657829272, 680293140); // section 12
feeder.checkedPut(10401000000614L, 657829272, 401326994); // section 12
feeder.checkedPut(10400345749304L, 2095121916, -9852212); // section 12
feeder.checkedPut(10401000002204L, 657829272, 14438460); // section 12
feeder.checkedPut(10400345749234L, 1186831289, -894006017); // section 12
feeder.checkedPut(10401000500234L, 969314784, -2112018706); // section 12
feeder.checkedPut(10401000000284L, 657829272, 521425852); // section 12
feeder.checkedPut(10401000002134L, 657829272, 208406306); // section 12
feeder.checkedPut(10400345749254L, 2095121916, -341939818); // section 12
feeder.checkedPut(10401000500384L, 969314784, -2136811544); // section 12
feeder.checkedPut(10401000001944L, 657829272, 935194952); // section 12
feeder.checkedPut(10400345749224L, 1186831289, -828214183); // section 12
feeder.checkedPut(10400345749244L, 2095121916, -351234120); // section 12
feeder.checkedPut(10400333128994L, 2095121916, -496909430); // section 12
feeder.checkedPut(10400333197934L, 2095121916, 2147144926); // section 12
feeder.checkedPut(10400333197944L, 2095121916, -2082366964); // section 12
feeder.checkedPut(10400336947684L, 2095121916, -1404212288); // section 12
feeder.checkedPut(10401000000594L, 657829272, 124369790); // section 12
feeder.checkedPut(10400331896264L, 2095121916, -1028383492); // section 12
feeder.checkedPut(10400332415044L, 2095121916, 1629436704); // section 12
feeder.checkedPut(10400345749614L, 1186831289, 1027996827); // section 12
feeder.checkedPut(10401000500424L, 969314784, -1871616544); // section 12
feeder.checkedPut(10400336947694L, 2095121916, -1468802722); // section 12
feeder.checkedPut(10410002672481L, 2154973, 1515288586); // section 12
feeder.checkedPut(10410345749171L, 2154973, 2084791828); // section 12
feeder.checkedPut(10400004960671L, 2154973, 1554754674); // section 12
feeder.checkedPut(10410009983601L, 2154973, -2049707334); // section 12
feeder.checkedPut(10410335811601L, 2154973, 1547385114); // section 12
feeder.checkedPut(10410000005951L, 2154973, -1136117016); // section 12
feeder.checkedPut(10400004938331L, 2154973, -1361373018); // section 12
feeder.checkedPut(10410001490421L, 2154973, -818792874); // section 12
feeder.checkedPut(10400001187131L, 2154973, 649763142); // section 12
feeder.checkedPut(10410000409071L, 2154973, -614460616); // section 12
feeder.checkedPut(10410333717391L, 2154973, 1343531416); // section 12
feeder.checkedPut(10410336680071L, 2154973, -914544144); // section 12
feeder.checkedPut(10410002068511L, 2154973, -746995576); // section 12
feeder.checkedPut(10410336207851L, 2154973, 863146156); // section 12
feeder.checkedPut(10410002365251L, 2154973, 542724164); // section 12
feeder.checkedPut(10400335812581L, 2154973, 2146284796); // section 12
feeder.checkedPut(10410337345361L, 2154973, -384625318); // section 12
feeder.checkedPut(10410000409091L, 2154973, -528258556); // section 12
return feeder;
}
// ---
@Test
public void testNonBlockingHashMapIteratorMultithreaded() throws InterruptedException, ExecutionException
{
TestKeyFeeder feeder = getTestKeyFeeder();
final int itemCount = feeder.size();
// validate results
final NonBlockingHashMapLong<TestKey> items = feeder.getMapMultithreaded();
assertEquals("size()", itemCount, items.size());
assertEquals("values().size()", itemCount, items.values().size());
assertEquals("entrySet().size()", itemCount, items.entrySet().size());
int iteratorCount = 0;
for (TestKey m : items.values())
{
iteratorCount++;
}
// sometimes a different result comes back the second time
int iteratorCount2 = 0;
for (TestKey m2 : items.values())
{
iteratorCount2++;
}
assertEquals("iterator counts differ", iteratorCount, iteratorCount2);
assertEquals("values().iterator() count", itemCount, iteratorCount);
}
// --- NBHMLFeeder ---
// Class to be called from another thread, to get concurrent installs into
// the table.
static private class NBHMLFeeder implements Callable<Object>
{
static private final Random _rand = new Random(System.currentTimeMillis());
private final NonBlockingHashMapLong<TestKey> _map;
private final int _count;
private final CyclicBarrier _barrier;
private final long _offset;
public NBHMLFeeder(
final NonBlockingHashMapLong<TestKey> map,
final int count,
final CyclicBarrier barrier,
final long offset)
{
_map = map;
_count = count;
_barrier = barrier;
_offset = offset;
}
public Object call() throws Exception
{
_barrier.await(); // barrier, to force racing start
for (long j = 0; j < _count; j++)
{
_map.put(
j + _offset,
new TestKey(_rand.nextLong(), _rand.nextInt(), (short) _rand.nextInt(Short.MAX_VALUE)));
}
return null;
}
}
// --- TestKey ---
// Funny key tests all sorts of things, has a pre-wired hashCode & equals.
static private final class TestKey
{
public final int _type;
public final long _id;
public final int _hash;
public TestKey(final long id, final int type, int hash)
{
_id = id;
_type = type;
_hash = hash;
}
public int hashCode()
{
return _hash;
}
public boolean equals(Object object)
{
if (null == object)
{
return false;
}
if (object == this)
{
return true;
}
if (object.getClass() != this.getClass())
{
return false;
}
final TestKey other = (TestKey) object;
return (this._type == other._type && this._id == other._id);
}
public String toString()
{
return String.format("%s:%d,%d,%d", getClass().getSimpleName(), _id, _type, _hash);
}
}
// ---
static private class TestKeyFeeder
{
private final Hashtable<Integer, List<TestKey>> _items = new Hashtable<>();
private int _size = 0;
public int size()
{
return _size;
}
// Put items into the hashtable, sorted by 'type' into LinkedLists.
public void checkedPut(final long id, final int type, final int hash)
{
_size++;
final TestKey item = new TestKey(id, type, hash);
if (!_items.containsKey(type))
{
_items.put(type, new LinkedList<>());
}
_items.get(type).add(item);
}
public NonBlockingHashMapLong<TestKey> getMapMultithreaded() throws InterruptedException, ExecutionException
{
final int threadCount = _items.keySet().size();
final NonBlockingHashMapLong<TestKey> map = new NonBlockingHashMapLong<>();
// use a barrier to open the gate for all threads at once to avoid rolling start and no actual concurrency
final CyclicBarrier barrier = new CyclicBarrier(threadCount);
final ExecutorService ex = Executors.newFixedThreadPool(threadCount);
final CompletionService<Integer> co = new ExecutorCompletionService<>(ex);
for (Integer type : _items.keySet())
{
// A linked-list of things to insert
List<TestKey> items = _items.get(type);
TestKeyFeederThread feeder = new TestKeyFeederThread(items, map, barrier);
co.submit(feeder);
}
// wait for all threads to return
int itemCount = 0;
for (int retCount = 0; retCount < threadCount; retCount++)
{
final Future<Integer> result = co.take();
itemCount += result.get();
}
ex.shutdown();
return map;
}
}
// --- TestKeyFeederThread
static private class TestKeyFeederThread implements Callable<Integer>
{
private final NonBlockingHashMapLong<TestKey> _map;
private final List<TestKey> _items;
private final CyclicBarrier _barrier;
public TestKeyFeederThread(
final List<TestKey> items,
final NonBlockingHashMapLong<TestKey> map,
final CyclicBarrier barrier)
{
_map = map;
_items = items;
_barrier = barrier;
}
public Integer call() throws Exception
{
_barrier.await();
int count = 0;
for (TestKey item : _items)
{
if (_map.contains(item._id))
{
System.err.printf("COLLISION DETECTED: %s exists\n", item.toString());
}
final TestKey exists = _map.putIfAbsent(item._id, item);
if (exists == null)
{
count++;
}
else
{
System.err.printf("COLLISION DETECTED: %s exists as %s\n", item.toString(), exists.toString());
}
}
return count;
}
}
}