public static void main(String[] args) throws IOException {
int cacheSize = 5 * 1024 * 1024;
int objectSize = 2 * 1024;
Stopwatch setupMon = SimonManager.getStopwatch("setupMon");
Stopwatch readerMon = SimonManager.getStopwatch("readerMon");
Stopwatch writerMon = SimonManager.getStopwatch("writerMon");
Stopwatch serializeMon = SimonManager.getStopwatch("serializeMon");
Stopwatch deserializeMon = SimonManager.getStopwatch("deserializeMon");
// long started = Calendar.getInstance().getTimeInMillis();
System.out.println("started one thread test for a " + (cacheSize / 1024 / 1024) + "mb cache" + " objectSize=" + objectSize + " serialization buffer=" + serBufferSize);
ByteBuffer buffer = ByteBuffer.allocateDirect(cacheSize);
Map<String, CacheEntry> map = new ConcurrentHashMap<String, CacheEntry>();
int memoryUsed = 0;
int count = 0;
while (memoryUsed <= cacheSize - objectSize) {
CacheEntry entry = new CacheEntry();
entry.key = "key"+(count++);
DummyPojo obj = new DummyPojo(entry.key, objectSize);
Split serSplit = serializeMon.start();
byte[] src = serialize(obj);
serSplit.stop();
Split split = setupMon.start();
entry.size = src.length;
entry.buffer = buffer.duplicate();
entry.position = memoryUsed;
entry.buffer.position(memoryUsed);
try {
entry.buffer.put(src);
} catch (BufferOverflowException e) {
// TODO: handle exception
e.printStackTrace();
}
map.put(entry.key, entry);
memoryUsed+=entry.size;
split.stop();
}
System.out.println("setupMon: " + setupMon);
System.out.println("average append duration: " + (double)(setupMon.getTotal() / setupMon.getCounter())/1000000 + "ms");
System.out.println("average serialize duration: " + (double)(serializeMon.getTotal() / serializeMon.getCounter())/1000000 + "ms");
int numOfThreads = 10;
System.out.println("multithread test - starting " + numOfThreads + " threads");
Thread last = null;
for (int i = 0; i < numOfThreads; i++) {
Reader reader = new Reader();
reader.map = map;
reader.start();
last = reader;
Thread.yield();
Writer writer = new Writer();
writer.map = map;
writer.start();
Thread.yield();
last = writer;
}
while (last.isAlive())
Thread.yield();
System.out.println(readerMon);
System.out.println(writerMon);
System.out.println(deserializeMon);
System.out.println(serializeMon);
System.out.println("average read duration: " + (double)(readerMon.getTotal() / readerMon.getCounter())/1000000 + "ms");
System.out.println("average write duration: " + (double)(writerMon.getTotal() / writerMon.getCounter())/1000000 + "ms");
System.out.println("average deserialize duration: " + (double)(deserializeMon.getTotal() / deserializeMon.getCounter())/1000000 + "ms");
System.out.println("average serialize duration: " + (double)(serializeMon.getTotal() / serializeMon.getCounter())/1000000 + "ms");
}