-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestDiskCache.java
89 lines (75 loc) · 2.93 KB
/
TestDiskCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import org.apache.commons.jcs3.JCS;
import org.apache.commons.jcs3.access.CacheAccess;
import org.apache.commons.jcs3.access.exception.CacheException;
import org.junit.jupiter.api.Test;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestDiskCache {
private CacheAccess<String, Set<String>> jcsCache = null;
private final int cachedCriteriaAmount = 11;
private final int idsPerCriterion = 1;
private final boolean fillCache = true;
private HashMap<String, Set<String>> testData;
@Test
public void mainTest() throws InterruptedException {
testData = generateData();
try{
jcsCache = JCS.getInstance("default");
}catch(CacheException e){
System.out.printf("Problem initializing cache: %s%n", e.getMessage() );
}
if(fillCache){
putDataToJcs();
TimeUnit.SECONDS.sleep(1); //without this sleep, the cache would for some reason find all values, although they aren't (/shouldn't be) in the cache
}
String keyTemplate = "someKey-";
float totalTime = 0;
for(int i = 0; i < cachedCriteriaAmount; i++){
String key = keyTemplate + i;
Set<String> valueSet = testData.get(key);
long startTime = System.nanoTime();
Set<String> foundValues = jcsGet(key);
long endTime = System.nanoTime();
//assertEquals(valueSet, foundValues);
System.out.println("found for key [" + key + "]: " + foundValues);
totalTime += (float)(endTime - startTime) / 1000000.0;
}
System.out.printf("It took on average %fms to get an entry%n", totalTime / cachedCriteriaAmount);
}
private void putDataToJcs(){
String keyTemplate = "someKey-";
for(int i = 0; i < cachedCriteriaAmount; i++){
String key = keyTemplate + i;
Set<String> valueSet = testData.get(key);
jcsPut(key, valueSet);
}
}
private HashMap<String, Set<String>> generateData(){
HashMap<String, Set<String>> data = new HashMap<>();
String keyTemplate = "someKey-";
for(int i = 0; i < cachedCriteriaAmount; i++){
String key = keyTemplate + i;
String valueTemplate = "val-";
Set<String> valueSet = new HashSet<>();
for(int j = 0; j < idsPerCriterion; j++){
valueSet.add(valueTemplate + (i + j) );
}
data.put(key, valueSet);
}
return data;
}
public void jcsPut(String key, Set<String> values){
try
{
jcsCache.put(key, values);
}
catch (CacheException e)
{
System.out.printf("Problem putting entry in the cache, for key %s%n%s%n", key, e.getMessage());
}
}
public Set<String> jcsGet(String key){
return jcsCache.get(key);
}
}