View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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          // tests with large worldSizes are slow because with a large
54          // world size the probability of a cache miss is high.
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          // assertEquals(tlruCache.keyList(), lruCache.keyList());
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      // slow test that is known to pass
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 }