KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > reports > XmlFilePerfReportWriter


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.tool.reports;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.activemq.tool.reports.plugins.ThroughputReportPlugin;
23 import org.apache.activemq.tool.reports.plugins.CpuReportPlugin;
24
25 import java.util.Properties JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.io.BufferedOutputStream JavaDoc;
35 import java.io.FileOutputStream JavaDoc;
36 import java.io.FileNotFoundException JavaDoc;
37 import java.io.BufferedReader JavaDoc;
38 import java.io.InputStreamReader JavaDoc;
39 import java.io.FileInputStream JavaDoc;
40 import java.io.IOException JavaDoc;
41
42 public class XmlFilePerfReportWriter extends AbstractPerfReportWriter {
43     private static final Log log = LogFactory.getLog(XmlFilePerfReportWriter.class);
44
45     private File JavaDoc tempLogFile;
46     private PrintWriter JavaDoc tempLogFileWriter;
47
48     private File JavaDoc xmlFile;
49     private PrintWriter JavaDoc xmlFileWriter;
50
51     private String JavaDoc reportDir;
52     private String JavaDoc reportName;
53
54     private Map JavaDoc testPropsMap;
55     private List JavaDoc testPropsList;
56
57     public XmlFilePerfReportWriter() {
58         this("", "PerformanceReport.xml");
59     }
60
61     public XmlFilePerfReportWriter(String JavaDoc reportDir, String JavaDoc reportName) {
62         this.testPropsMap = new HashMap JavaDoc();
63         this.testPropsList = new ArrayList JavaDoc();
64         this.reportDir = reportDir;
65         this.reportName = reportName;
66     }
67
68     public void openReportWriter() {
69         if (tempLogFile == null) {
70             tempLogFile = createTempLogFile();
71         }
72
73         try {
74             // Disable auto-flush and allocate 100kb of buffer
75
tempLogFileWriter = new PrintWriter JavaDoc(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(tempLogFile), 102400), false);
76         } catch (FileNotFoundException JavaDoc e) {
77             e.printStackTrace();
78         }
79     }
80
81     public void closeReportWriter() {
82         // Flush and close log file writer
83
tempLogFileWriter.flush();
84         tempLogFileWriter.close();
85
86         writeToXml();
87     }
88
89     public String JavaDoc getReportDir() {
90         return reportDir;
91     }
92
93     public void setReportDir(String JavaDoc reportDir) {
94         this.reportDir = reportDir;
95     }
96
97     public String JavaDoc getReportName() {
98         return reportName;
99     }
100
101     public void setReportName(String JavaDoc reportName) {
102         this.reportName = reportName;
103     }
104
105     public File JavaDoc getXmlFile() {
106         return xmlFile;
107     }
108
109     public void setXmlFile(File JavaDoc xmlFile) {
110         this.xmlFile = xmlFile;
111     }
112
113     public void writeInfo(String JavaDoc info) {
114         tempLogFileWriter.println("[INFO]" + info);
115     }
116
117     public void writeCsvData(int csvType, String JavaDoc csvData) {
118         if (csvType == REPORT_PLUGIN_THROUGHPUT) {
119             tempLogFileWriter.println("[TP-DATA]" + csvData);
120         } else if (csvType == REPORT_PLUGIN_CPU) {
121             tempLogFileWriter.println("[CPU-DATA]" + csvData);
122         }
123     }
124
125     public void writeProperties(String JavaDoc header, Properties JavaDoc props) {
126         testPropsMap.put(header, props);
127     }
128
129     public void writeProperties(Properties JavaDoc props) {
130         testPropsList.add(props);
131     }
132
133     protected File JavaDoc createTempLogFile() {
134         File JavaDoc f;
135         try {
136             f = File.createTempFile("tmpPL", null);
137         } catch (IOException JavaDoc e) {
138             f = new File JavaDoc("tmpPL" + System.currentTimeMillis() + ".tmp");
139         }
140         f.deleteOnExit();
141         return f;
142     }
143
144     protected File JavaDoc createXmlFile() {
145         String JavaDoc filename = (getReportName().endsWith(".xml") ? getReportName() : (getReportName() + ".xml"));
146         String JavaDoc path = (getReportDir() == null) ? "" : getReportDir();
147
148         return new File JavaDoc(path + filename);
149     }
150
151     protected void writeToXml() {
152         try {
153             xmlFile = createXmlFile();
154             xmlFileWriter = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(xmlFile));
155             writeXmlHeader();
156             writeXmlTestSettings();
157             writeXmlLogFile();
158             writeXmlPerfSummary();
159             writeXmlFooter();
160             xmlFileWriter.close();
161
162             log.info("Created performance report: " + xmlFile.getAbsolutePath());
163         } catch (Exception JavaDoc e) {
164             e.printStackTrace();
165         }
166     }
167
168     protected void writeXmlHeader() {
169         xmlFileWriter.println("<testResult>");
170     }
171
172     protected void writeXmlFooter() {
173         xmlFileWriter.println("</testResult>");
174     }
175
176     protected void writeXmlTestSettings() {
177         Properties JavaDoc props;
178
179         // Write test settings
180
for (Iterator JavaDoc i=testPropsMap.keySet().iterator(); i.hasNext();) {
181             String JavaDoc key = (String JavaDoc)i.next();
182             props = (Properties JavaDoc)testPropsMap.get(key);
183             writeMap(key, props);
184         }
185
186         int count = 1;
187         for (Iterator JavaDoc i=testPropsList.iterator(); i.hasNext();) {
188             props = (Properties JavaDoc)i.next();
189             writeMap("settings" + count++, props);
190         }
191     }
192
193     protected void writeXmlLogFile() throws IOException JavaDoc {
194         // Write throughput data
195
xmlFileWriter.println("<property name='performanceData'>");
196         xmlFileWriter.println("<list>");
197
198         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(tempLogFile)));
199         String JavaDoc line;
200         while ((line = reader.readLine()) != null) {
201             if (line.startsWith("[TP-DATA]")) {
202                 handleCsvData(REPORT_PLUGIN_THROUGHPUT, line.substring("[TP-DATA]".length()));
203                 parsePerfCsvData("tpdata", line.substring("[TP-DATA]".length()));
204             } else if (line.startsWith("[CPU-DATA]")) {
205                 handleCsvData(REPORT_PLUGIN_CPU, line.substring("[CPU-DATA]".length()));
206                 parsePerfCsvData("cpudata", line.substring("[CPU-DATA]".length()));
207             } else if (line.startsWith("[INFO]")) {
208                 xmlFileWriter.println("<info>" + line + "</info>");
209             } else {
210                 xmlFileWriter.println("<error>" + line + "</error>");
211             }
212         }
213
214         xmlFileWriter.println("</list>");
215         xmlFileWriter.println("</property>");
216     }
217
218     protected void writeXmlPerfSummary() {
219
220         Map JavaDoc summary;
221
222         summary = getSummary(REPORT_PLUGIN_THROUGHPUT);
223         if (summary != null && summary.size() > 0) {
224             writeThroughputSummary(summary);
225         }
226
227         summary = getSummary(REPORT_PLUGIN_CPU);
228         if (summary != null && summary.size() > 0) {
229             writeCpuSummary(summary);
230         }
231
232     }
233
234     protected void writeThroughputSummary(Map JavaDoc summary) {
235         // Write throughput summary
236
xmlFileWriter.println("<property name='perfTpSummary'>");
237         xmlFileWriter.println("<props>");
238
239         String JavaDoc val, clientName, clientVal;
240
241         System.out.println("#########################################");
242         System.out.println("#### SYSTEM THROUGHPUT SUMMARY ####");
243         System.out.println("#########################################");
244
245         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_SYS_TOTAL_TP);
246         System.out.println("System Total Throughput: " + val);
247         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_SYS_TOTAL_TP + "'>" + val + "</prop>");
248
249         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_SYS_TOTAL_CLIENTS);
250         System.out.println("System Total Clients: " + val);
251         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_SYS_TOTAL_CLIENTS + "'>" + val + "</prop>");
252
253         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_SYS_AVE_TP);
254         System.out.println("System Average Throughput: " + val);
255         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_SYS_AVE_TP + "'>" + val + "</prop>");
256
257         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_SYS_AVE_EMM_TP);
258         System.out.println("System Average Throughput Excluding Min/Max: " + val);
259         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_SYS_AVE_EMM_TP + "'>" + val + "</prop>");
260
261         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_SYS_AVE_CLIENT_TP);
262         System.out.println("System Average Client Throughput: " + val);
263         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_SYS_AVE_CLIENT_TP + "'>" + val + "</prop>");
264
265         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_SYS_AVE_CLIENT_EMM_TP);
266         System.out.println("System Average Client Throughput Excluding Min/Max: " + val);
267         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_SYS_AVE_CLIENT_EMM_TP + "'>" + val + "</prop>");
268
269         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_MIN_CLIENT_TP);
270         clientName = val.substring(0, val.indexOf("="));
271         clientVal = val.substring(val.indexOf("=") + 1);
272         System.out.println("Min Client Throughput Per Sample: clientName=" + clientName + ", value=" + clientVal);
273         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_MIN_CLIENT_TP + "'>clientName=" + clientName + ",value=" + clientVal + "</prop>");
274
275         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_MAX_CLIENT_TP);
276         clientName = val.substring(0, val.indexOf("="));
277         clientVal = val.substring(val.indexOf("=") + 1);
278         System.out.println("Max Client Throughput Per Sample: clientName=" + clientName + ", value=" + clientVal);
279         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_MAX_CLIENT_TP + "'>clientName=" + clientName + ",value=" + clientVal + "</prop>");
280
281         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_MIN_CLIENT_TOTAL_TP);
282         clientName = val.substring(0, val.indexOf("="));
283         clientVal = val.substring(val.indexOf("=") + 1);
284         System.out.println("Min Client Total Throughput: clientName=" + clientName + ", value=" + clientVal);
285         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_MIN_CLIENT_TOTAL_TP + "'>clientName=" + clientName + ",value=" + clientVal + "</prop>");
286
287         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_MAX_CLIENT_TOTAL_TP);
288         clientName = val.substring(0, val.indexOf("="));
289         clientVal = val.substring(val.indexOf("=") + 1);
290         System.out.println("Max Client Total Throughput: clientName=" + clientName + ", value=" + clientVal);
291         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_MAX_CLIENT_TOTAL_TP + "'>clientName=" + clientName + ",value=" + clientVal + "</prop>");
292
293         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_MIN_CLIENT_AVE_TP);
294         clientName = val.substring(0, val.indexOf("="));
295         clientVal = val.substring(val.indexOf("=") + 1);
296         System.out.println("Min Average Client Throughput: clientName=" + clientName + ", value=" + clientVal);
297         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_MIN_CLIENT_AVE_TP + "'>clientName=" + clientName + ",value=" + clientVal + "</prop>");
298
299         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_MAX_CLIENT_AVE_TP);
300         clientName = val.substring(0, val.indexOf("="));
301         clientVal = val.substring(val.indexOf("=") + 1);
302         System.out.println("Max Average Client Throughput: clientName=" + clientName + ", value=" + clientVal);
303         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_MAX_CLIENT_AVE_TP + "'>clientName=" + clientName + ",value=" + clientVal + "</prop>");
304
305         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_MIN_CLIENT_AVE_EMM_TP);
306         clientName = val.substring(0, val.indexOf("="));
307         clientVal = val.substring(val.indexOf("=") + 1);
308         System.out.println("Min Average Client Throughput Excluding Min/Max: clientName=" + clientName + ", value=" + clientVal);
309         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_MIN_CLIENT_AVE_EMM_TP + "'>clientName=" + clientName + ",value=" + clientVal + "</prop>");
310
311         val = (String JavaDoc)summary.get(ThroughputReportPlugin.KEY_MAX_CLIENT_AVE_EMM_TP);
312         clientName = val.substring(0, val.indexOf("="));
313         clientVal = val.substring(val.indexOf("=") + 1);
314         System.out.println("Max Average Client Throughput Excluding Min/Max: clientName=" + clientName + ", value=" + clientVal);
315         xmlFileWriter.println("<prop key='" + ThroughputReportPlugin.KEY_MAX_CLIENT_AVE_EMM_TP + "'>clientName=" + clientName + ",value=" + clientVal + "</prop>");
316
317         xmlFileWriter.println("</props>");
318         xmlFileWriter.println("</property>");
319     }
320
321     protected void writeCpuSummary(Map JavaDoc summary) {
322         xmlFileWriter.println("<property name='perfTpSummary'>");
323         xmlFileWriter.println("<props>");
324
325         System.out.println("########################################");
326         System.out.println("#### SYSTEM CPU USAGE SUMMARY ####");
327         System.out.println("########################################");
328
329         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_BLOCK_RECV + "'>" + summary.get(CpuReportPlugin.KEY_BLOCK_RECV) + "</prop>");
330         System.out.println("Total Blocks Received: " + summary.get(CpuReportPlugin.KEY_BLOCK_RECV));
331
332         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_AVE_BLOCK_RECV + "'>" + summary.get(CpuReportPlugin.KEY_AVE_BLOCK_RECV) + "</prop>");
333         System.out.println("Ave Blocks Received: " + summary.get(CpuReportPlugin.KEY_AVE_BLOCK_RECV));
334
335         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_BLOCK_SENT + "'>" + summary.get(CpuReportPlugin.KEY_BLOCK_SENT) + "</prop>");
336         System.out.println("Total Blocks Sent: " + summary.get(CpuReportPlugin.KEY_BLOCK_SENT));
337
338         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_AVE_BLOCK_SENT + "'>" + summary.get(CpuReportPlugin.KEY_AVE_BLOCK_SENT) + "</prop>");
339         System.out.println("Ave Blocks Sent: " + summary.get(CpuReportPlugin.KEY_AVE_BLOCK_SENT));
340
341         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_CTX_SWITCH + "'>" + summary.get(CpuReportPlugin.KEY_CTX_SWITCH) + "</prop>");
342         System.out.println("Total Context Switches: " + summary.get(CpuReportPlugin.KEY_CTX_SWITCH));
343
344         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_AVE_CTX_SWITCH + "'>" + summary.get(CpuReportPlugin.KEY_AVE_CTX_SWITCH) + "</prop>");
345         System.out.println("Ave Context Switches: " + summary.get(CpuReportPlugin.KEY_AVE_CTX_SWITCH));
346
347         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_USER_TIME + "'>" + summary.get(CpuReportPlugin.KEY_USER_TIME) + "</prop>");
348         System.out.println("Total User Time: " + summary.get(CpuReportPlugin.KEY_USER_TIME));
349
350         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_AVE_USER_TIME + "'>" + summary.get(CpuReportPlugin.KEY_AVE_USER_TIME) + "</prop>");
351         System.out.println("Ave User Time: " + summary.get(CpuReportPlugin.KEY_AVE_USER_TIME));
352
353         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_SYS_TIME + "'>" + summary.get(CpuReportPlugin.KEY_SYS_TIME) + "</prop>");
354         System.out.println("Total System Time: " + summary.get(CpuReportPlugin.KEY_SYS_TIME));
355
356         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_AVE_SYS_TIME + "'>" + summary.get(CpuReportPlugin.KEY_AVE_SYS_TIME) + "</prop>");
357         System.out.println("Ave System Time: " + summary.get(CpuReportPlugin.KEY_AVE_SYS_TIME));
358
359         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_IDLE_TIME + "'>" + summary.get(CpuReportPlugin.KEY_IDLE_TIME) + "</prop>");
360         System.out.println("Total Idle Time: " + summary.get(CpuReportPlugin.KEY_IDLE_TIME));
361
362         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_AVE_IDLE_TIME + "'>" + summary.get(CpuReportPlugin.KEY_AVE_IDLE_TIME) + "</prop>");
363         System.out.println("Ave Idle Time: " + summary.get(CpuReportPlugin.KEY_AVE_IDLE_TIME));
364
365         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_WAIT_TIME + "'>" + summary.get(CpuReportPlugin.KEY_WAIT_TIME) + "</prop>");
366         System.out.println("Total Wait Time: " + summary.get(CpuReportPlugin.KEY_WAIT_TIME));
367
368         xmlFileWriter.println("<prop key='" + CpuReportPlugin.KEY_AVE_WAIT_TIME + "'>" + summary.get(CpuReportPlugin.KEY_AVE_WAIT_TIME) + "</prop>");
369         System.out.println("Ave Wait Time: " + summary.get(CpuReportPlugin.KEY_AVE_WAIT_TIME));
370
371         xmlFileWriter.println("</props>");
372         xmlFileWriter.println("</property>");
373     }
374
375     protected void writeMap(String JavaDoc name, Map JavaDoc map) {
376         xmlFileWriter.println("<property name='" + name + "'>");
377         xmlFileWriter.println("<props>");
378         for (Iterator JavaDoc i=map.keySet().iterator(); i.hasNext();) {
379             String JavaDoc propKey = (String JavaDoc)i.next();
380             Object JavaDoc propVal = map.get(propKey);
381             xmlFileWriter.println("<prop key='" + propKey + "'>" + propVal.toString() + "</prop>");
382         }
383         xmlFileWriter.println("</props>");
384         xmlFileWriter.println("</property>");
385     }
386
387     protected void parsePerfCsvData(String JavaDoc elementName, String JavaDoc csvData) {
388         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(csvData, ",;");
389         String JavaDoc xmlElement;
390
391         xmlElement = "<" + elementName;
392         String JavaDoc data, key, val;
393         while (tokenizer.hasMoreTokens()) {
394             data = tokenizer.nextToken();
395             key = data.substring(0, data.indexOf("="));
396             val = data.substring(data.indexOf("=") + 1);
397             xmlElement += (" " + key + "='" + val + "'");
398         }
399         xmlElement += " />";
400         xmlFileWriter.println(xmlElement);
401     }
402 }
403
Popular Tags