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 static org.junit.Assert.assertEquals;
17  
18  import java.util.LinkedList;
19  import java.util.List;
20  
21  import org.junit.Ignore;
22  import org.junit.Test;
23  
24  @Ignore
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=" + ((end - start) / (1000 * 1000)) + " in millis");
75      }
76  
77      @Test
78      @Ignore
79      // slow test that is known to pass
80      public void multiThreadedScenario() throws InterruptedException {
81          int cacheSize = 100;
82          int worldSize = cacheSize * 2;
83          LRUCache<String, String> lruCache = new LRUCache<String, String>(cacheSize);
84          T_LRUCache<String> tlruCache = new T_LRUCache<String>(cacheSize);
85          SimulatorRunnable[] simulatorArray = new SimulatorRunnable[5];
86          for (int i = 0; i < simulatorArray.length; i++) {
87              simulatorArray[i] = new SimulatorRunnable(lruCache, tlruCache, worldSize);
88          }
89          for (int i = 0; i < simulatorArray.length; i++) {
90              simulatorArray[i].start();
91          }
92          for (int i = 0; i < simulatorArray.length; i++) {
93              simulatorArray[i].join();
94          }
95          assertEquals(tlruCache.keyList(), lruCache.keyList());
96      }
97  
98      private class SimulatorRunnable extends Thread {
99  
100         LRUCache<String, String> lruCache;
101         T_LRUCache<String> tlruCache;
102         int worldSize;
103 
104         SimulatorRunnable(LRUCache<String, String> lruCache, T_LRUCache<String> tlruCache, int worldSize) {
105             this.lruCache = lruCache;
106             this.tlruCache = tlruCache;
107             this.worldSize = worldSize;
108         }
109 
110         public void run() {
111             int get2PutRatio = 10;
112             int simulationLen = 1000 * 50;
113             Simulator simulator = new Simulator(worldSize, get2PutRatio, true);
114             List<Event<String>> scenario = simulator.generateScenario(simulationLen);
115             simulator.simulate(scenario, lruCache, tlruCache);
116             System.out.println("done");
117         }
118     }
119 
120 }