9
10
import org.slf4j.LoggerFactory;
11
12
import java.util.Random;
13
import java.util.concurrent.Callable;
14
import java.util.concurrent.TimeUnit;
13
import static org.junit.Assert.assertEquals;
14
import static org.junit.Assert.assertTrue;
16
import static org.hamcrest.core.IsEqual.equalTo;
17
import static org.hamcrest.number.OrderingComparison.greaterThan;
18
import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo;
19
import static org.hamcrest.number.OrderingComparison.lessThan;
20
import static org.junit.Assert.assertThat;
15
21
import static org.junit.Assert.fail;
67
73
public void testOverflowToDiskWithLargeNumberofCacheEntriesAndGets() throws Exception {
69
//Set size so the second element overflows to disk.
70
//Cache cache = new Cache("test", 1000, MemoryStoreEvictionPolicy.LRU, true, null, true, 500, 500, false, 60, null);
71
Cache cache = new Cache(new CacheConfiguration("test", 1000)
74
final int ELEMENTS = 100000;
75
final Cache cache = new Cache(new CacheConfiguration("test", 1000)
72
76
.memoryStoreEvictionPolicy("LRU")
74
78
.overflowToDisk(true)
76
79
.diskAccessStripes(5)
77
80
.diskExpiryThreadIntervalSeconds(60));
78
81
manager.addCache(cache);
79
82
Random random = new Random();
80
83
StopWatch stopWatch = new StopWatch();
81
for (int i = 0; i < 100000; i++) {
82
cache.put(new Element("" + i,
84
for (int i = 0; i < ELEMENTS; i++) {
85
cache.put(new Element(Integer.toString(i),
83
86
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
84
87
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
85
88
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
86
89
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
87
90
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
89
cache.get("" + random.nextInt(100000));
92
cache.get(Integer.toString(random.nextInt(i + 1)));
95
RetryAssert.assertBy(10, TimeUnit.SECONDS, new Callable<Integer>() {
97
public Integer call() throws Exception {
98
return cache.getDiskStoreSize();
100
}, equalTo(ELEMENTS));
101
assertThat(cache.getSize(), equalTo(ELEMENTS));
93
103
long elapsed = stopWatch.getElapsedTime();
94
LOG.info("Elapsed time: " + elapsed / 1000);
96
assertEquals(100000, cache.getSize());
97
assertTrue(23 < elapsed);
98
//Some entries may be in the Memory Store and Disk Store. cache.getSize removes dupes. a look at the
99
//disk store size directly does not.
100
assertTrue(99000 <= cache.getDiskStoreSize());
104
LOG.info("Elapsed time: " + elapsed);
126
130
Cache cache = new Cache("test", 1000, MemoryStoreEvictionPolicy.LRU, true, null, true, 500, 500, false, 1, null);
127
131
manager.addCache(cache);
128
132
StopWatch stopWatch = new StopWatch();
131
Integer index = null;
133
for (; i < 100; i++) {
134
for (j = 0; j < 100000; j++) {
135
index = Integer.valueOf(((1000000 * i) + j));
136
cache.put(new Element(index,
135
for (int i = 0; i < 100; i++) {
136
for (int j = 0; j < 100000; j++) {
137
index = (1000000 * i) + j;
138
cache.put(new Element(Integer.valueOf(index),
137
139
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
138
140
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
139
141
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
150
152
} catch (OutOfMemoryError e) {
151
153
LOG.info("All heap consumed after " + index + " entries created.");
152
int expectedMax = 3090000;
153
assertTrue("Achieved " + index.intValue() + " which was less than the expected value of " + expectedMax,
154
index.intValue() >= expectedMax);
154
assertThat(index, greaterThanOrEqualTo(3090000));
174
175
public void testLargePutGetPerformanceWithOverflowToDisk() throws Exception {
176
177
Cache cache = new Cache("test", 1000, MemoryStoreEvictionPolicy.LRU, true, null, true, 500, 500, false, 10000, null);
193
194
long elapsed = stopWatch.getElapsedTime();
194
195
long putTime = ((elapsed / 1000) - 10);
195
196
LOG.info("Put Elapsed time: " + putTime);
196
assertTrue(putTime < 20);
197
assertThat(putTime, lessThan(20L));
198
199
//wait for Disk Store to finish spooling
199
200
while (cache.getStore().bufferFull()) {