String value = "value for (" + k + ")";
return new ProviderResult(value, k.getGroup(), cacheable, "extensions");
}
});
final InternalCache cache = (InternalCache) builder.buildCache();
// Create 7 groups, of size 4,8,12,16,20,24,28 with two subgroups
// one of which is 50% and one is 75% of the size of the parent group.
GroupBuilder groupBuilder = factory.createGroupBuilder();
Group rootGroup = cache.getRootGroup();
for (int i = 0, g = 0; i < 7; i += 1) {
int maxCount = i * GROUP_SIZE_MULTIPLE + 4;
groupBuilder.setMaxCount(maxCount);
Group group = rootGroup.addGroup("group" + i, groupBuilder);
groups[g++] = group;
for (int s = 0; s < 2; s += 1) {
groupBuilder.setMaxCount((int) ((0.5 + s / 4.0) * maxCount));
groups[g++] = group.addGroup("group" + i + "/" + s,
groupBuilder);
}
}
TestRemovalListener listener = new TestRemovalListener();
rootGroup.addRemovalListener(listener);
// Create 10 threads, each of which will perform one of the following
// actions at random.
// 1) Retrieve an item from the store
Thread[] threads = new Thread[20];
MyRunnable[] runnables = new MyRunnable[threads.length];
for (int i = 0; i < threads.length; i++) {
MyRunnable runnable = new MyRunnable(i, groups, cache);
Thread thread = new Thread(runnable);
threads[i] = thread;
runnables[i] = runnable;
}
StatisticsSnapshot snapshotBefore = rootGroup.getStatisticsSnapshot();
for (int i = 0; i < threads.length; i++) {
Thread thread = threads[i];
thread.start();
}
boolean failed = false;
for (int i = 0; i < threads.length; i++) {
Thread thread = threads[i];
MyRunnable runnable = runnables[i];
thread.join();
if (runnable.failed()) {
String errors = runnable.getErrors();
if (errors != null) {
if (logger.isDebugEnabled()) {
logger.debug(errors);
}
}
failed = true;
}
}
if (failed) {
fail("Stress test failed due to exceptions thrown by threads");
}
// Validate the internal structure of the cache.
ensureCacheIntegrity(cache);
StatisticsSnapshot snapshotAfter = rootGroup.getStatisticsSnapshot();
StatisticsDelta delta = snapshotAfter.difference(snapshotBefore);
System.out.println("Period: " + delta.getPeriod());
System.out.println("Hit count: " + delta.getHitCount());
System.out.println("Missed / Added count: " + delta.getMissedAddedCount());
System.out.println("Removed count: " + delta.getRemovedCount());
System.out.println("Change count: " +
(delta.getMissedAddedCount() - delta.getRemovedCount()));
System.out.println("Hit rate: " + delta.getHitRate() + "%");
System.out.println("Key count: " + keys.size());
cache.debugStructure(new IndentingWriter(System.out));
// Make sure that the number of entries removed according to the
// snapshot is equal to the number removed according to the listener.
int notifiedRemovedCount = listener.getRemovedCount();
System.out.println("Notified removed count: " + notifiedRemovedCount);
assertEquals(delta.getRemovedCount(), notifiedRemovedCount);
if (logger.isDebugEnabled()) {
StringWriter writer = new StringWriter();
IndentingWriter printer = new IndentingWriter(writer);
cache.debugStructure(printer);
logger.debug(writer.toString());
}
}