OnJava8-Examples/concurrency/MapComparisons.java

97 lines
3.0 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// concurrency/MapComparisons.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
// {Args: 1 10 10} (Fast verification check during build)
// Rough comparison of thread-safe Map performance.
import java.util.concurrent.*;
import java.util.*;
import onjava.*;
2015-06-15 17:47:35 -07:00
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++)
2015-12-15 11:47:04 -08:00
for(int index = 0; index < collectionSize; index++)
result += testCollection.get(index);
2015-06-15 17:47:35 -07:00
}
void putResults() {
readResult += result;
readTime += duration;
}
}
class Writer extends TestTask {
void test() {
for(long i = 0; i < testCycles; i++)
2015-12-15 11:47:04 -08:00
for(int index = 0; index < collectionSize; index++)
testCollection.put(index, writeData[index]);
2015-06-15 17:47:35 -07:00
}
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 {
2015-12-15 11:47:04 -08:00
Map<Integer,Integer> collectionInitializer() {
2015-06-15 17:47:35 -07:00
return Collections.synchronizedMap(
new HashMap<>(
MapData.map(
2015-11-03 12:00:44 -08:00
new CountingSupplier.Integer(),
new CountingSupplier.Integer(),
2015-12-15 11:47:04 -08:00
collectionSize)));
2015-06-15 17:47:35 -07:00
}
SynchronizedHashMapTest(int nReaders, int nWriters) {
super("Synched HashMap", nReaders, nWriters);
}
}
class ConcurrentHashMapTest extends MapTest {
2015-12-15 11:47:04 -08:00
Map<Integer,Integer> collectionInitializer() {
2015-06-15 17:47:35 -07:00
return new ConcurrentHashMap<>(
MapData.map(
2015-11-03 12:00:44 -08:00
new CountingSupplier.Integer(),
2015-12-15 11:47:04 -08:00
new CountingSupplier.Integer(), collectionSize));
2015-06-15 17:47:35 -07:00
}
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();
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
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
2015-09-07 11:44:36 -06:00
*/