1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.turbo.lru;
15
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Random;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
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
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
67
68
69
70 if (r0 != null) {
71 assertEquals(r0, e.k);
72 }
73 assertEquals(r0, r1);
74 }
75 }
76 }
77 }
78
79
80
81
82
83
84
85
86
87
88
89 }