001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.turbo.lru;
015
016import static org.junit.Assert.assertEquals;
017
018import java.util.LinkedList;
019import java.util.List;
020
021import org.junit.Ignore;
022import org.junit.Test;
023
024@Ignore
025public class LRUCacheTest {
026
027    @Test
028    public void smoke() {
029
030        LRUCache<String, String> cache = new LRUCache<String, String>(2);
031        cache.put("a", "a");
032        cache.put("b", "b");
033        cache.put("c", "c");
034        List<String> witness = new LinkedList<String>();
035        witness.add("b");
036        witness.add("c");
037        assertEquals(witness, cache.keyList());
038    }
039
040    @Test
041    public void typicalScenarioTest() {
042        int simulationLen = 1000 * 10;
043        int cacheSize = 100;
044        int worldSize = 1000;
045        doScenario(simulationLen, cacheSize, worldSize);
046    }
047
048    @Test
049    public void scenarioCoverageTest() {
050        int simulationLen = 1000 * 10;
051
052        int[] cacheSizes = new int[] { 1, 10, 100 };
053        // tests with large worldSizes are slow because with a large
054        // world size the probability of a cache miss is high.
055        int[] worldSizes = new int[] { 1, 10, 100 };
056
057        for (int i = 0; i < cacheSizes.length; i++) {
058            for (int j = 0; j < worldSizes.length; j++) {
059                doScenario(simulationLen, cacheSizes[i], worldSizes[j]);
060            }
061        }
062    }
063
064    void doScenario(int simulationLen, int cacheSize, int worldSize) {
065        int get2PutRatio = 10;
066        Simulator simulator = new Simulator(worldSize, get2PutRatio, false);
067        List<Event<String>> scenario = simulator.generateScenario(simulationLen);
068        LRUCache<String, String> lruCache = new LRUCache<String, String>(cacheSize);
069        T_LRUCache<String> tlruCache = new T_LRUCache<String>(cacheSize);
070        long start = System.nanoTime();
071        simulator.simulate(scenario, lruCache, tlruCache);
072        // assertEquals(tlruCache.keyList(), lruCache.keyList());
073        long end = System.nanoTime();
074        System.out.println("cacheSize=" + cacheSize + ", worldSize=" + worldSize + ", elapsed time=" + ((end - start) / (1000 * 1000)) + " in millis");
075    }
076
077    @Test
078    @Ignore
079    // slow test that is known to pass
080    public void multiThreadedScenario() throws InterruptedException {
081        int cacheSize = 100;
082        int worldSize = cacheSize * 2;
083        LRUCache<String, String> lruCache = new LRUCache<String, String>(cacheSize);
084        T_LRUCache<String> tlruCache = new T_LRUCache<String>(cacheSize);
085        SimulatorRunnable[] simulatorArray = new SimulatorRunnable[5];
086        for (int i = 0; i < simulatorArray.length; i++) {
087            simulatorArray[i] = new SimulatorRunnable(lruCache, tlruCache, worldSize);
088        }
089        for (int i = 0; i < simulatorArray.length; i++) {
090            simulatorArray[i].start();
091        }
092        for (int i = 0; i < simulatorArray.length; i++) {
093            simulatorArray[i].join();
094        }
095        assertEquals(tlruCache.keyList(), lruCache.keyList());
096    }
097
098    private class SimulatorRunnable extends Thread {
099
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}