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,
57              T_LRUCache<String> tlruCache) {
58          for (Event<String> e : scenario) {
59              if (e.put) {
60                  lruCache.put(e.k, e.k);
61                  tlruCache.put(e.k);
62              } else {
63                  String r0 = lruCache.get(e.k);
64                  String r1 = tlruCache.get(e.k);
65                  if (!multiThreaded) {
66                      // if the simulation is used in a multi-threaded
67                      // context, then the state of lruCache may be different than
68                      // that of tlruCache. In single threaded mode, they should
69                      // return the same values all the time
70                      if (r0 != null) {
71                          assertEquals(r0, e.k);
72                      }
73                      assertEquals(r0, r1);
74                  }
75              }
76          }
77      }
78  
79      // void compareAndDumpIfDifferent(LRUCache<String, String> lruCache,
80      // T_LRUCache<String> tlruCache) {
81      // lruCache.dump();
82      // tlruCache.dump();
83      // if(!lruCache.keyList().equals(tlruCache.ketList())) {
84      // lruCache.dump();
85      // tlruCache.dump();
86      // throw new AssertionFailedError("s");
87      // }
88      // }
89  }