forked from maxmind/MaxMind-DB-Reader-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreadedTest.java
More file actions
65 lines (56 loc) · 2.26 KB
/
MultiThreadedTest.java
File metadata and controls
65 lines (56 loc) · 2.26 KB
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
package com.maxmind.db;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.junit.jupiter.api.Test;
public class MultiThreadedTest {
@Test
public void multipleMmapOpens() throws InterruptedException,
ExecutionException {
Callable<Map<?, ?>> task = () -> {
try (Reader reader = new Reader(ReaderTest.getFile("MaxMind-DB-test-decoder.mmdb"))) {
return reader.get(InetAddress.getByName("::1.1.1.0"), Map.class);
}
};
MultiThreadedTest.runThreads(task);
}
@Test
public void streamThreadTest() throws IOException, InterruptedException,
ExecutionException {
try (Reader reader = new Reader(ReaderTest.getStream("MaxMind-DB-test-decoder.mmdb"))) {
MultiThreadedTest.threadTest(reader);
}
}
@Test
public void mmapThreadTest() throws IOException, InterruptedException,
ExecutionException {
try (Reader reader = new Reader(ReaderTest.getFile("MaxMind-DB-test-decoder.mmdb"))) {
MultiThreadedTest.threadTest(reader);
}
}
private static void threadTest(final Reader reader)
throws InterruptedException, ExecutionException {
Callable<Map<?, ?>> task = () -> reader.get(InetAddress.getByName("::1.1.1.0"), Map.class);
MultiThreadedTest.runThreads(task);
}
private static void runThreads(Callable<Map<?, ?>> task)
throws InterruptedException, ExecutionException {
int threadCount = 256;
var tasks = Collections.nCopies(threadCount, task);
ExecutorService executorService = Executors
.newFixedThreadPool(threadCount);
var futures = executorService.invokeAll(tasks);
for (Future<Map<?, ?>> future : futures) {
Map<?, ?> record = future.get();
assertEquals(268435456, (long) record.get("uint32"));
assertEquals("unicode! ☯ - ♫", record.get("utf8_string"));
}
}
}