1 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 ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.Iterator ; 31 import java.util.StringTokenizer ; 32 import java.io.File ; 33 import java.io.PrintWriter ; 34 import java.io.BufferedOutputStream ; 35 import java.io.FileOutputStream ; 36 import java.io.FileNotFoundException ; 37 import java.io.BufferedReader ; 38 import java.io.InputStreamReader ; 39 import java.io.FileInputStream ; 40 import java.io.IOException ; 41 42 public class XmlFilePerfReportWriter extends AbstractPerfReportWriter { 43 private static final Log log = LogFactory.getLog(XmlFilePerfReportWriter.class); 44 45 private File tempLogFile; 46 private PrintWriter tempLogFileWriter; 47 48 private File xmlFile; 49 private PrintWriter xmlFileWriter; 50 51 private String reportDir; 52 private String reportName; 53 54 private Map testPropsMap; 55 private List testPropsList; 56 57 public XmlFilePerfReportWriter() { 58 this("", "PerformanceReport.xml"); 59 } 60 61 public XmlFilePerfReportWriter(String reportDir, String reportName) { 62 this.testPropsMap = new HashMap (); 63 this.testPropsList = new ArrayList (); 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 tempLogFileWriter = new PrintWriter (new BufferedOutputStream (new FileOutputStream (tempLogFile), 102400), false); 76 } catch (FileNotFoundException e) { 77 e.printStackTrace(); 78 } 79 } 80 81 public void closeReportWriter() { 82 tempLogFileWriter.flush(); 84 tempLogFileWriter.close(); 85 86 writeToXml(); 87 } 88 89 public String getReportDir() { 90 return reportDir; 91 } 92 93 public void setReportDir(String reportDir) { 94 this.reportDir = reportDir; 95 } 96 97 public String getReportName() { 98 return reportName; 99 } 100 101 public void setReportName(String reportName) { 102 this.reportName = reportName; 103 } 104 105 public File getXmlFile() { 106 return xmlFile; 107 } 108 109 public void setXmlFile(File xmlFile) { 110 this.xmlFile = xmlFile; 111 } 112 113 public void writeInfo(String info) { 114 tempLogFileWriter.println("[INFO]" + info); 115 } 116 117 public void writeCsvData(int csvType, String 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 header, Properties props) { 126 testPropsMap.put(header, props); 127 } 128 129 public void writeProperties(Properties props) { 130 testPropsList.add(props); 131 } 132 133 protected File createTempLogFile() { 134 File f; 135 try { 136 f = File.createTempFile("tmpPL", null); 137 } catch (IOException e) { 138 f = new File ("tmpPL" + System.currentTimeMillis() + ".tmp"); 139 } 140 f.deleteOnExit(); 141 return f; 142 } 143 144 protected File createXmlFile() { 145 String filename = (getReportName().endsWith(".xml") ? getReportName() : (getReportName() + ".xml")); 146 String path = (getReportDir() == null) ? "" : getReportDir(); 147 148 return new File (path + filename); 149 } 150 151 protected void writeToXml() { 152 try { 153 xmlFile = createXmlFile(); 154 xmlFileWriter = new PrintWriter (new FileOutputStream (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 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 props; 178 179 for (Iterator i=testPropsMap.keySet().iterator(); i.hasNext();) { 181 String key = (String )i.next(); 182 props = (Properties )testPropsMap.get(key); 183 writeMap(key, props); 184 } 185 186 int count = 1; 187 for (Iterator i=testPropsList.iterator(); i.hasNext();) { 188 props = (Properties )i.next(); 189 writeMap("settings" + count++, props); 190 } 191 } 192 193 protected void writeXmlLogFile() throws IOException { 194 xmlFileWriter.println("<property name='performanceData'>"); 196 xmlFileWriter.println("<list>"); 197 198 BufferedReader reader = new BufferedReader (new InputStreamReader (new FileInputStream (tempLogFile))); 199 String 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 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 summary) { 235 xmlFileWriter.println("<property name='perfTpSummary'>"); 237 xmlFileWriter.println("<props>"); 238 239 String val, clientName, clientVal; 240 241 System.out.println("#########################################"); 242 System.out.println("#### SYSTEM THROUGHPUT SUMMARY ####"); 243 System.out.println("#########################################"); 244 245 val = (String )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 )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 )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 )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 )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 )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 )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 )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 )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 )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 )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 )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 )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 )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 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 name, Map map) { 376 xmlFileWriter.println("<property name='" + name + "'>"); 377 xmlFileWriter.println("<props>"); 378 for (Iterator i=map.keySet().iterator(); i.hasNext();) { 379 String propKey = (String )i.next(); 380 Object 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 elementName, String csvData) { 388 StringTokenizer tokenizer = new StringTokenizer (csvData, ",;"); 389 String xmlElement; 390 391 xmlElement = "<" + elementName; 392 String 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 |