1 package ch.qos.logback.classic.pattern;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.util.LinkedList;
6 import java.util.List;
7
8 import org.junit.Ignore;
9 import org.junit.Test;
10
11 import ch.qos.logback.classic.pattern.lru.Event;
12 import ch.qos.logback.classic.pattern.lru.T_LRUCache;
13
14 @Ignore
15 public class LRUCacheTest {
16
17 @Test
18 public void smoke() {
19
20 LRUCache<String, String> cache = new LRUCache<String, String>(2);
21 cache.put("a", "a");
22 cache.put("b", "b");
23 cache.put("c", "c");
24 List<String> witness = new LinkedList<String>();
25 witness.add("b");
26 witness.add("c");
27 assertEquals(witness, cache.keyList());
28 }
29
30 @Test
31 public void typicalScenarioTest() {
32 int simulationLen = 1000 * 10;
33 int cacheSize = 100;
34 int worldSize = 1000;
35 doScenario(simulationLen, cacheSize, worldSize);
36 }
37
38 @Test
39 public void scenarioCoverageTest() {
40 int simulationLen = 1000 * 10;
41
42 int[] cacheSizes = new int[] { 1, 10, 100};
43
44
45 int[] worldSizes = new int[] { 1, 10, 100 };
46
47 for (int i = 0; i < cacheSizes.length; i++) {
48 for (int j = 0; j < worldSizes.length; j++) {
49 doScenario(simulationLen, cacheSizes[i], worldSizes[j]);
50 }
51 }
52 }
53
54 void doScenario(int simulationLen, int cacheSize, int worldSize) {
55 int get2PutRatio = 10;
56 Simulator simulator = new Simulator(worldSize, get2PutRatio, false);
57 List<Event> scenario = simulator.generateScenario(simulationLen);
58 LRUCache<String, String> lruCache = new LRUCache<String, String>(cacheSize);
59 T_LRUCache<String> tlruCache = new T_LRUCache<String>(cacheSize);
60 long start = System.nanoTime();
61 simulator.simulate(scenario, lruCache, tlruCache);
62
63 long end = System.nanoTime();
64 System.out.println("cacheSize=" + cacheSize + ", worldSize=" + worldSize
65 + ", elapsed time=" + ((end - start) / (1000 * 1000)) + " in millis");
66 }
67
68
69
70 @Test
71 @Ignore
72 public void multiThreadedScenario() throws InterruptedException {
73 int cacheSize = 100;
74 int worldSize = cacheSize*2;
75 LRUCache<String, String> lruCache = new LRUCache<String, String>(cacheSize);
76 T_LRUCache<String> tlruCache = new T_LRUCache<String>(cacheSize);
77 SimulatorRunnable[] simulatorArray = new SimulatorRunnable[5];
78 for(int i = 0; i < simulatorArray.length; i++) {
79 simulatorArray[i] = new SimulatorRunnable(lruCache, tlruCache, worldSize);
80 }
81 for(int i = 0; i < simulatorArray.length; i++) {
82 simulatorArray[i].start();
83 }
84 for(int i = 0; i < simulatorArray.length; i++) {
85 simulatorArray[i].join();
86 }
87 assertEquals(tlruCache.keyList(), lruCache.keyList());
88 }
89
90 private class SimulatorRunnable extends Thread {
91
92 LRUCache<String, String> lruCache;
93 T_LRUCache<String> tlruCache;
94 int worldSize;
95
96 SimulatorRunnable(LRUCache<String, String> lruCache, T_LRUCache<String> tlruCache, int worldSize) {
97 this.lruCache = lruCache;
98 this.tlruCache = tlruCache;
99 this.worldSize = worldSize;
100 }
101
102 public void run() {
103 int get2PutRatio = 10;
104 int simulationLen = 1000*50;
105 Simulator simulator = new Simulator(worldSize, get2PutRatio, true);
106 List<Event> scenario = simulator.generateScenario(simulationLen);
107 simulator.simulate(scenario, lruCache, tlruCache);
108 System.out.println("done");
109 }
110 }
111
112 }