94 lines
2.9 KiB
Java
94 lines
2.9 KiB
Java
//: concurrency/MapComparisons.java
|
|
// ©2015 MindView LLC: see Copyright.txt
|
|
// {Args: 1 10 10} (Fast verification check during build)
|
|
// Rough comparison of thread-safe Map performance.
|
|
import java.util.concurrent.*;
|
|
import java.util.*;
|
|
import com.mindviewinc.util.*;
|
|
|
|
abstract class MapTest
|
|
extends Tester<Map<Integer,Integer>> {
|
|
MapTest(String testId, int nReaders, int nWriters) {
|
|
super(testId, nReaders, nWriters);
|
|
}
|
|
class Reader extends TestTask {
|
|
long result = 0;
|
|
void test() {
|
|
for(long i = 0; i < testCycles; i++)
|
|
for(int index = 0; index < containerSize; index++)
|
|
result += testContainer.get(index);
|
|
}
|
|
void putResults() {
|
|
readResult += result;
|
|
readTime += duration;
|
|
}
|
|
}
|
|
class Writer extends TestTask {
|
|
void test() {
|
|
for(long i = 0; i < testCycles; i++)
|
|
for(int index = 0; index < containerSize; index++)
|
|
testContainer.put(index, writeData[index]);
|
|
}
|
|
void putResults() {
|
|
writeTime += duration;
|
|
}
|
|
}
|
|
void startReadersAndWriters() {
|
|
for(int i = 0; i < nReaders; i++)
|
|
exec.execute(new Reader());
|
|
for(int i = 0; i < nWriters; i++)
|
|
exec.execute(new Writer());
|
|
}
|
|
}
|
|
|
|
class SynchronizedHashMapTest extends MapTest {
|
|
Map<Integer,Integer> containerInitializer() {
|
|
return Collections.synchronizedMap(
|
|
new HashMap<>(
|
|
MapData.map(
|
|
new CountingGenerator.Integer(),
|
|
new CountingGenerator.Integer(),
|
|
containerSize)));
|
|
}
|
|
SynchronizedHashMapTest(int nReaders, int nWriters) {
|
|
super("Synched HashMap", nReaders, nWriters);
|
|
}
|
|
}
|
|
|
|
class ConcurrentHashMapTest extends MapTest {
|
|
Map<Integer,Integer> containerInitializer() {
|
|
return new ConcurrentHashMap<>(
|
|
MapData.map(
|
|
new CountingGenerator.Integer(),
|
|
new CountingGenerator.Integer(), containerSize));
|
|
}
|
|
ConcurrentHashMapTest(int nReaders, int nWriters) {
|
|
super("ConcurrentHashMap", nReaders, nWriters);
|
|
}
|
|
}
|
|
|
|
public class MapComparisons {
|
|
public static void main(String[] args) {
|
|
Tester.initMain(args);
|
|
new SynchronizedHashMapTest(10, 0);
|
|
new SynchronizedHashMapTest(9, 1);
|
|
new SynchronizedHashMapTest(5, 5);
|
|
new ConcurrentHashMapTest(10, 0);
|
|
new ConcurrentHashMapTest(9, 1);
|
|
new ConcurrentHashMapTest(5, 5);
|
|
Tester.exec.shutdown();
|
|
}
|
|
} /* Output:
|
|
Type Read time Write time
|
|
Synched HashMap 10r 0w 2825885 0
|
|
Synched HashMap 9r 1w 3463976 74283
|
|
readTime + writeTime = 3538259
|
|
Synched HashMap 5r 5w 744537 633254
|
|
readTime + writeTime = 1377791
|
|
ConcurrentHashMap 10r 0w 437445 0
|
|
ConcurrentHashMap 9r 1w 545597 78268
|
|
readTime + writeTime = 623865
|
|
ConcurrentHashMap 5r 5w 58343 232524
|
|
readTime + writeTime = 290867
|
|
*///:~
|