-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBeingZeroLogSearch.java
More file actions
262 lines (238 loc) · 8.32 KB
/
Copy pathBeingZeroLogSearch.java
File metadata and controls
262 lines (238 loc) · 8.32 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import java.util.*;
import java.io.*;
import java.util.concurrent.atomic.*;
class Configuration{
public static final String LOG_FOLDER_PATH = "./spring-boot-server-logs";
public static final LogLevel LOG_LEVEL = LogLevel.INFO;
}
class FileUtility {
public static List<File> getAllLogFiles(String folderPath){
File folder = new File(folderPath);
File[] files = folder.listFiles();
return Arrays.asList(files);
}
}
enum LogLevel{
DEBUG(0), INFO(1), WARN(2), FATAL(3);
int level;
LogLevel(int level){
this.level = level;
}
public int getLevel(){
return level;
}
}
class Logger{
LogLevel logLevel;
Logger(){
logLevel = Configuration.LOG_LEVEL;
}
public void info(String msg){
if(logLevel.getLevel() <= LogLevel.INFO.getLevel())
System.out.println(msg);
}
public void warn(String msg){
if(logLevel.getLevel() <= LogLevel.WARN.getLevel())
System.out.println(msg);
}
public void debug(String msg){
if(logLevel.getLevel() <= LogLevel.DEBUG.getLevel())
System.out.println(msg);
}
public void fatal(String msg){
if(logLevel.getLevel() <= LogLevel.FATAL.getLevel())
System.out.println(msg);
}
}
class FileWordCount {
static Logger logger = new Logger();
public static int wordCount(File file, String pattern){
int currentCount = 0;
String filePath = file.getAbsolutePath();
try{
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line;
while((line = br.readLine()) != null){
if(line.contains(pattern))
currentCount++;
}
}
catch(Exception ex){
ex.printStackTrace();
}
logger.debug(String.format("'%s' : '%s' => %d\n", filePath, pattern, currentCount));
return currentCount;
}
}
interface ILogSearch {
public int logSearch(String pattern) throws Exception;
}
class SingleThreadedLogSearch implements ILogSearch{
public SingleThreadedLogSearch(){
}
public int logSearch(String pattern) throws Exception {
int count = 0;
List<File> files = FileUtility.getAllLogFiles(Configuration.LOG_FOLDER_PATH);
for(File file : files){
count += FileWordCount.wordCount(file, pattern);
}
return count;
}
}
class MultithreadedLogSearch implements ILogSearch{
int count;
public MultithreadedLogSearch(){
count = 0;
}
public int logSearch(String pattern) throws Exception {
List<File> files = FileUtility.getAllLogFiles(Configuration.LOG_FOLDER_PATH);
List<Thread> threadsList = new ArrayList<>();
for(File file : files){
threadsList.add(new Thread(){
public void run(){
count += FileWordCount.wordCount(file, pattern);
}
});
}
for(Thread t : threadsList)
t.start();
for(Thread t : threadsList)
t.join();
return count;
}
}
class MultithreadedLogSearchVolatile implements ILogSearch{
volatile int count;
public MultithreadedLogSearchVolatile(){
count = 0;
}
public int logSearch(String pattern) throws Exception {
List<File> files = FileUtility.getAllLogFiles(Configuration.LOG_FOLDER_PATH);
List<Thread> threadsList = new ArrayList<>();
for(File file : files){
threadsList.add(new Thread(){
public void run(){
count += FileWordCount.wordCount(file, pattern);
}
});
}
for(Thread t : threadsList)
t.start();
for(Thread t : threadsList)
t.join();
return count;
}
}
class MultithreadedLogSearchAtomicInteger implements ILogSearch{
AtomicInteger count;
public MultithreadedLogSearchAtomicInteger(){
// count = 0;
count = new AtomicInteger(0);
}
public int logSearch(String pattern) throws Exception {
List<File> files = FileUtility.getAllLogFiles(Configuration.LOG_FOLDER_PATH);
List<Thread> threadsList = new ArrayList<>();
for(File file : files){
threadsList.add(new Thread(){
public void run(){
while(!count.compareAndSet(count.get(), count.get()+FileWordCount.wordCount(file, pattern))){
}
}
});
}
for(Thread t : threadsList)
t.start();
for(Thread t : threadsList)
t.join();
return count.get();
}
}
class MultithreadedLogSearchSynchronizedFunction implements ILogSearch{
volatile int count;
public MultithreadedLogSearchSynchronizedFunction(){
count = 0;
}
synchronized void calculateAndUpdate(File file, String pattern){
count += FileWordCount.wordCount(file, pattern);
}
public int logSearch(String pattern) throws Exception {
List<File> files = FileUtility.getAllLogFiles(Configuration.LOG_FOLDER_PATH);
List<Thread> threadsList = new ArrayList<>();
for(File file : files){
threadsList.add(new Thread(){
public void run(){
calculateAndUpdate(file, pattern);
}
});
}
for(Thread t : threadsList)
t.start();
for(Thread t : threadsList)
t.join();
return count;
}
}
class MultithreadedLogSearchSynchronizedBlock implements ILogSearch{
volatile int count;
Object lockObject;
public MultithreadedLogSearchSynchronizedBlock(){
count = 0;
lockObject = new Object();
}
public int logSearch(String pattern) throws Exception {
List<File> files = FileUtility.getAllLogFiles(Configuration.LOG_FOLDER_PATH);
List<Thread> threadsList = new ArrayList<>();
for(File file : files){
threadsList.add(new Thread(){
public void run(){
int curCount = FileWordCount.wordCount(file, pattern);
synchronized(lockObject){
count += curCount;
}
}
});
}
for(Thread t : threadsList)
t.start();
for(Thread t : threadsList)
t.join();
return count;
}
}
class TimeDecorator implements ILogSearch{
ILogSearch decoratee;
long timeTaken;
static Logger logger = new Logger();
public TimeDecorator(ILogSearch decoratee){
this.decoratee = decoratee;
}
public int logSearch(String pattern) throws Exception {
long startTime = System.currentTimeMillis();
int ans = decoratee.logSearch(pattern);
long endTime = System.currentTimeMillis();
timeTaken = endTime - startTime;
return ans;
}
public long getTimeTaken(){
return timeTaken;
}
}
public class BeingZeroLogSearch{
static void countAndPrint(TimeDecorator td, String searchTerm, String type) throws Exception {
Logger logger = new Logger();
int occurenceCount = td.logSearch(searchTerm);
long timeTakenInMs = td.getTimeTaken();
logger.info(String.format("%-3d ms : %-45s : '%s' occurs %d times.", timeTakenInMs, type, searchTerm, occurenceCount));
}
public static void main(String args[]) throws Exception {
String searchTerm = "MongoSocketOpenException";
searchTerm = "mongodb";
TimeDecorator td = new TimeDecorator(new SingleThreadedLogSearch());
countAndPrint(new TimeDecorator(new SingleThreadedLogSearch()), searchTerm, "SingleThreadedLogSearch");
countAndPrint(new TimeDecorator(new MultithreadedLogSearch()), searchTerm, "MultithreadedLogSearch");
countAndPrint(new TimeDecorator(new MultithreadedLogSearchAtomicInteger()), searchTerm, "MultithreadedLogSearchAtomicInteger");
countAndPrint(new TimeDecorator(new MultithreadedLogSearchVolatile()), searchTerm, "MultithreadedLogSearchVolatile");
countAndPrint(new TimeDecorator(new MultithreadedLogSearchSynchronizedFunction()), searchTerm, "MultithreadedLogSearchSynchronizedFunction");
countAndPrint(new TimeDecorator(new MultithreadedLogSearchSynchronizedBlock()), searchTerm, "MultithreadedLogSearchSynchronizedBlock");
}
}