1 import java.util.*;
2 import java.io.*;
3
4 public class PerfRedux {
5
6 static TimeRecord currentRecord;
7
8 static int gran;
9 static FileWriter out;
10
11 public static void main(String[] args) {
12
13 String currentString;
14 String finalReportString;
15
16 String target;
17 String destination;
18
19
20 try {
21 target = (String) args[0];
22 destination = (String) args[1];
23 gran = Integer.parseInt(args[2]);
24 } catch (Exception e) {
25 System.out.println("ERROR! Malformed command line arguements");
26 return;
27 }
28
29 // Build the first time record
30 currentRecord = new TimeRecord(0, gran-1);
31
32 // Open files and start read loop
33 try {
34
35 out = new FileWriter(destination);
36
37 FileReader theFile = new FileReader(target);
38 BufferedReader in = new BufferedReader(theFile);
39
40 currentString = in.readLine();
41 while( currentString != null ) {
42
43 handleLine(currentString);
44
45 currentString = in.readLine();
46 }
47
48 if (currentRecord != null) {
49
50 // code to clear out last time record
51 finalReportString = currentRecord.report();
52 out.write(finalReportString, 0, finalReportString.length());
53
54 }
55
56 in.close();
57 out.close();
58
59 } catch (FileNotFoundException e) {
60 System.out.println("ERROR! Could not find FILE: " + target);
61 } catch (IOException e) {
62 System.out.println("ERROR! General IO Error");
63 }
64
65 } // end main()
66
67 private static void handleLine(String line) {
68
69 StringBuffer timeString = new StringBuffer();
70 int rover = 0;
71 char tempChar;
72
73 System.out.println("HANDLE : " + line);
74
75 // if we bust a bounds here anywhere, then the line is not a valid put.
76 // catch the exception and move on.
77 try {
78
79 // Discard if does not start with time
80 if ( !Character.isDigit(line.charAt(0)) ) return;
81
82 // Peal off important elements. Return if any indicate this is not a put record.
83 tempChar = line.charAt(rover);
84 while (Character.isDigit(tempChar)) {
85 timeString.append(tempChar);
86 rover++;
87 tempChar = line.charAt(rover);
88 }
89
90 // Jump 3 chars. If not a P, then ditch.
91 rover += 3;
92 if (line.charAt(rover) != 'P') {
93 System.out.println("DROP");
94 return;
95 };
96
97 // Ok. I think we have a put line. Go ahead and start building the entry
98 TimeEntry te = new TimeEntry();
99 te.time = Integer.parseInt(timeString.toString());
100
101 // What was the result code? Find a digit
102 while ( !Character.isDigit(line.charAt(rover)) ) { rover++; }
103
104 te.result = Character.digit(line.charAt(rover), 10);
105 rover++;
106
107 // Find the latency
108 while ( !Character.isDigit(line.charAt(rover)) ) { rover++; }
109 StringBuffer latencyString = new StringBuffer();
110 tempChar = line.charAt(rover);
111 while (Character.isDigit(tempChar)) {
112 latencyString.append(tempChar);
113 rover++;
114 tempChar = line.charAt(rover);
115 }
116 te.latency = Integer.parseInt(latencyString.toString());
117
118 // Find the size
119 while ( !Character.isDigit(line.charAt(rover)) ) { rover++; }
120
121 StringBuffer sizeString = new StringBuffer();
122 tempChar = line.charAt(rover);
123
124 while (Character.isDigit(tempChar)) {
125 sizeString.append(tempChar);
126 rover++;
127 try {
128 tempChar = line.charAt(rover);
129 } catch (StringIndexOutOfBoundsException es) {
130 // This just means there is no white space at the end of the line. technically,
131 // it is ok.
132 tempChar = 'x'; // something that ISNT a digit :-)
133 }
134 }
135
136 te.size = Integer.parseInt(sizeString.toString());
137
138 // We have a whole record. Try to put it in somewhere
139 if (currentRecord.isIn(te)) {
140 System.out.println("Push");
141 currentRecord.put(te);
142
143 } else {
144 // Not in that time zone. report the current and build a new one
145 System.out.println("NEW");
146
147 String outString = currentRecord.report();
148 out.write(outString, 0, outString.length());
149
150 int newBase = te.time - (te.time % gran);
151 currentRecord = new TimeRecord(newBase, (newBase + (gran-1)) );
152
153 if (!currentRecord.put(te)) {
154 System.out.println("Software detected fault: couldnt stuff a brand-new TimeRecord");
155 }
156 }
157
158 } catch (Exception e) { };
159
160 } // end handeLine()
161
162 }
163
|