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.ArrayList;
019import java.util.List;
020import java.util.Random;
021
022public class Simulator {
023
024    Random random;
025
026    int worldSize;
027    int get2PutRatio;
028    boolean multiThreaded;
029
030    public Simulator(int worldSize, int get2PutRatio, boolean multiThreaded) {
031        this.worldSize = worldSize;
032        this.get2PutRatio = get2PutRatio;
033        long seed = System.nanoTime();
034        // System.out.println("seed is "+seed);
035        random = new Random(seed);
036        this.multiThreaded = multiThreaded;
037    }
038
039    public List<Event<String>> generateScenario(int len) {
040        List<Event<String>> scenario = new ArrayList<>();
041
042        for (int i = 0; i < len; i++) {
043
044            int r = random.nextInt(get2PutRatio);
045            boolean put = false;
046            if (r == 0) {
047                put = true;
048            }
049            r = random.nextInt(worldSize);
050            Event<String> e = new Event<>(put, String.valueOf(r));
051            scenario.add(e);
052        }
053        return scenario;
054    }
055
056    public void simulate(List<Event<String>> scenario, LRUCache<String, String> lruCache, T_LRUCache<String> tlruCache) {
057        for (Event<String> e : scenario) {
058            if (e.put) {
059                lruCache.put(e.k, e.k);
060                tlruCache.put(e.k);
061            } else {
062                String r0 = lruCache.get(e.k);
063                String r1 = tlruCache.get(e.k);
064                if (!multiThreaded) {
065                    // if the simulation is used in a multi-threaded
066                    // context, then the state of lruCache may be different than
067                    // that of tlruCache. In single threaded mode, they should
068                    // return the same values all the time
069                    if (r0 != null) {
070                        assertEquals(r0, e.k);
071                    }
072                    assertEquals(r0, r1);
073                }
074            }
075        }
076    }
077
078    // void compareAndDumpIfDifferent(LRUCache<String, String> lruCache,
079    // T_LRUCache<String> tlruCache) {
080    // lruCache.dump();
081    // tlruCache.dump();
082    // if(!lruCache.keyList().equals(tlruCache.ketList())) {
083    // lruCache.dump();
084    // tlruCache.dump();
085    // throw new AssertionFailedError("s");
086    // }
087    // }
088}