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