23 import java.io.PrintStream; |
23 import java.io.PrintStream; |
24 import java.text.SimpleDateFormat; |
24 import java.text.SimpleDateFormat; |
25 import java.util.Date; |
25 import java.util.Date; |
26 import java.util.concurrent.TimeUnit; |
26 import java.util.concurrent.TimeUnit; |
27 import java.util.Hashtable; |
27 import java.util.Hashtable; |
28 import java.util.Collections; |
28 import java.util.ArrayList; |
29 import java.util.Enumeration; |
29 import java.util.Calendar; |
30 import java.util.Vector; |
|
31 |
30 |
32 import org.apache.tools.ant.BuildEvent; |
31 import org.apache.tools.ant.BuildEvent; |
33 import org.apache.tools.ant.Project; |
32 import org.apache.tools.ant.Project; |
34 import org.apache.commons.logging.Log; |
33 import org.apache.commons.logging.Log; |
35 import org.apache.commons.logging.LogFactory; |
34 import org.apache.commons.logging.LogFactory; |
86 // Record build start time |
85 // Record build start time |
87 endOfPreviousTarget = new Date(); |
86 endOfPreviousTarget = new Date(); |
88 buildStartTime = new Date(); |
87 buildStartTime = new Date(); |
89 endOfPreviousTarget = new Date(); |
88 endOfPreviousTarget = new Date(); |
90 |
89 |
91 targetTable = new Hashtable(); |
90 targetTable = new ArrayList<String>(); |
92 tempStartTime = new Hashtable(); |
91 tempStartTime = new Hashtable(); |
93 |
92 |
94 // For Stage start time |
93 // For Stage start time |
95 allStages = new StringBuffer("\t<stages>"); |
94 allStages = new StringBuffer("\t<stages>"); |
96 |
95 |
138 |
137 |
139 /** |
138 /** |
140 * Triggered when a target finishes. |
139 * Triggered when a target finishes. |
141 */ |
140 */ |
142 public void targetFinished(BuildEvent event) { |
141 public void targetFinished(BuildEvent event) { |
143 String targetName = event.getTarget().getName(); |
142 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
143 String time = sdf.format(Calendar.getInstance().getTime()); |
|
144 |
|
145 String targetName = time + "," + event.getTarget().getName(); |
144 |
146 |
145 logTargetEvent(targetName, "finish"); |
147 logTargetEvent(targetName, "finish"); |
146 |
|
147 logTargetTime(targetName); |
148 logTargetTime(targetName); |
148 |
|
149 } |
149 } |
150 |
150 |
151 private void logTargetTime(String targetName) { |
151 private void logTargetTime(String targetName) { |
152 Date targetFinishTime = new Date(); |
152 Date targetFinishTime = new Date(); |
153 long targetLengthMSecs = targetFinishTime.getTime() |
153 long targetLengthMSecs = targetFinishTime.getTime() |
154 - targetStartTime.getTime(); |
154 - targetStartTime.getTime(); |
155 Long outputSecs = TimeUnit.MILLISECONDS.toSeconds(targetLengthMSecs); |
155 Long outputSecs = TimeUnit.MILLISECONDS.toSeconds(targetLengthMSecs); |
156 targetTable.put(outputSecs, targetName); |
156 targetTable.add(targetName + "," + outputSecs.toString()); |
157 } |
157 } |
158 |
158 |
159 /** |
159 /** |
160 * Triggered when the build finishes. |
160 * Triggered when the build finishes. |
161 */ |
161 */ |
162 public void buildFinished(BuildEvent event) { |
162 public void buildFinished(BuildEvent event) { |
163 if (isInitialized) { |
163 if (isInitialized) { |
164 if (directory != null && new File(directory).exists()) { |
164 if (directory != null && new File(directory).exists()) { |
165 try { |
165 try { |
166 // Log target times to file |
166 // Log target times to file |
167 String timesLogFileName = directory + File.separator |
167 String timesLogFileName = directory + File.separator + project.getProperty("build.id") + "_targetTimesLog.csv"; |
168 + "targetTimesLog.csv"; |
|
169 File timesLogFile = new File(timesLogFileName); |
168 File timesLogFile = new File(timesLogFileName); |
170 |
|
171 // Sort hashtable. |
|
172 Vector v = new Vector(targetTable.keySet()); |
|
173 Collections.sort(v); |
|
174 |
169 |
175 FileOutputStream timesLogFileStream = new FileOutputStream( |
170 FileOutputStream timesLogFileStream = new FileOutputStream( |
176 timesLogFileName, true); |
171 timesLogFileName, true); |
177 DataOutputStream timesLogOut = new DataOutputStream( |
172 DataOutputStream timesLogOut = new DataOutputStream( |
178 timesLogFileStream); |
173 timesLogFileStream); |
179 // Display (sorted) hashtable. |
174 // Display (sorted) hashtable. |
180 for (Enumeration e = v.elements(); e.hasMoreElements();) { |
175 for (String s : targetTable) |
181 Long key = (Long) e.nextElement(); |
176 timesLogOut.writeBytes(s + "\n"); |
182 String val = (String) targetTable.get(key); |
|
183 timesLogOut.writeBytes(val + "," + key.toString() |
|
184 + "\n"); |
|
185 } |
|
186 timesLogOut.close(); |
177 timesLogOut.close(); |
187 } catch (Exception ex) { |
178 } catch (Exception ex) { |
|
179 // We are Ignoring the errors as no need to fail the build. |
188 log.fatal("Exception has occurred", ex); |
180 log.fatal("Exception has occurred", ex); |
189 ex.printStackTrace(); |
181 ex.printStackTrace(); |
190 } |
182 } |
191 } |
183 } |
192 cleanup(); |
184 cleanup(); |