-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheTester.java
124 lines (106 loc) · 2.8 KB
/
CacheTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/*
* Implementation by Eric Xing
* Driver style class for testing the 6 cache replacement policies under
* Various poisson distributions
* Similar to Driver.java
*/
public class CacheTester {
// Cache, Reader, and Writer objects for testing
static Cache cache;
static Reader reader;
static Writer writer;
// Cache capacity
static int CAPACITY = 500;
// Type of cache for displaying results
static String cache_type;
// Display hit rate
public static void dispAll() {
System.out.println(cache_type + " cache");
reader.display();
}
/*
* Return a Writer object based on an id parameter Where 0 <= id <= 1
*/
public static Writer getWriter(int id) {
switch (id) {
case 0:
return new WriteBack(cache);
case 1:
return new WriteThrough(cache);
}
return null;
}
/*
* Return a Cache object based on id parameter Where 0 <= id <= 5
*/
public static Cache getCache(int id) {
switch (id) {
case 0:
cache_type = "MRU";
return new MRUCache(CAPACITY);
case 1:
cache_type = "LRU";
return new LRUCache(CAPACITY);
case 2:
cache_type = "FIFO";
return new FIFOCache(CAPACITY);
case 3:
cache_type = "LIFO";
return new LIFOCache(CAPACITY);
case 4:
cache_type = "RR";
return new RandomReplaceCache(CAPACITY);
case 5:
cache_type = "LFU";
return new LFUCache(CAPACITY);
}
return null;
}
/*
* Return a Reader object based on an id parameter Where 0 <= id <= 1
*/
public static Reader getReader(int id) {
switch (id) {
case 0:
return new ReadThrough(cache);
case 1:
return new CacheAside(cache);
}
return null;
}
public static void main(String[] args) throws IOException {
// test all 6 replacement policies
for (int i = 0; i < 6; i++) {
// Read data from the testing data file
BufferedReader br = new BufferedReader(new FileReader(new File("test_data_0.75.in")));
// get the right cache, reader and writer don't matter
cache = getCache(i);
reader = getReader(0);
writer = getWriter(0);
// record start time
long start_time = System.nanoTime();
// go through all queries
for (int idx = 0; idx < 10000; idx++) {
String[] line = br.readLine().split(" ");
if (line[0].equals("q")) {
// query
reader.query(Integer.parseInt(line[1]));
} else {
// add
writer.write(Integer.parseInt(line[1]));
}
}
// record end time
long end_time = System.nanoTime();
// display all info for this replacement policy
dispAll();
System.out.println("Time elapsed: " + ((end_time - start_time) / 1000000.0));
System.out.println();
br.close();
}
}
}