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.ArrayList;
19  import java.util.List;
20  import java.util.Random;
21  
22  public class Simulator {
23  
24      Random random;
25  
26      int worldSize;
27      int get2PutRatio;
28      boolean multiThreaded;
29  
30      public Simulator(int worldSize, int get2PutRatio, boolean multiThreaded) {
31          this.worldSize = worldSize;
32          this.get2PutRatio = get2PutRatio;
33          long seed = System.nanoTime();
34          // System.out.println("seed is "+seed);
35          random = new Random(seed);
36          this.multiThreaded = multiThreaded;
37      }
38  
39      public List<Event<String>> generateScenario(int len) {
40          List<Event<String>> scenario = new ArrayList<>();
41  
42          for (int i = 0; i < len; i++) {
43  
44              int r = random.nextInt(get2PutRatio);
45              boolean put = false;
46              if (r == 0) {
47                  put = true;
48              }
49              r = random.nextInt(worldSize);
50              Event<String> e = new Event<>(put, String.valueOf(r));
51              scenario.add(e);
52          }
53          return scenario;
54      }
55  
56      public void simulate(List<Event<String>> scenario, LRUCache<String, String> lruCache, T_LRUCache<String> tlruCache) {
57          for (Event<String> e : scenario) {
58              if (e.put) {
59                  lruCache.put(e.k, e.k);
60                  tlruCache.put(e.k);
61              } else {
62                  String r0 = lruCache.get(e.k);
63                  String r1 = tlruCache.get(e.k);
64                  if (!multiThreaded) {
65                      // if the simulation is used in a multi-threaded
66                      // context, then the state of lruCache may be different than
67                      // that of tlruCache. In single threaded mode, they should
68                      // return the same values all the time
69                      if (r0 != null) {
70                          assertEquals(r0, e.k);
71                      }
72                      assertEquals(r0, r1);
73                  }
74              }
75          }
76      }
77  
78      // void compareAndDumpIfDifferent(LRUCache<String, String> lruCache,
79      // T_LRUCache<String> tlruCache) {
80      // lruCache.dump();
81      // tlruCache.dump();
82      // if(!lruCache.keyList().equals(tlruCache.ketList())) {
83      // lruCache.dump();
84      // tlruCache.dump();
85      // throw new AssertionFailedError("s");
86      // }
87      // }
88  }