001: /*
002: * Copyright (C) 2006 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 22. July 2006 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.tools.benchmark.reporters;
013:
014: import com.thoughtworks.xstream.tools.benchmark.Reporter;
015: import com.thoughtworks.xstream.tools.benchmark.Metric;
016: import com.thoughtworks.xstream.tools.benchmark.Target;
017: import com.thoughtworks.xstream.tools.benchmark.Product;
018: import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
019:
020: import java.io.File;
021: import java.io.FileWriter;
022: import java.io.IOException;
023: import java.util.List;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.Date;
027:
028: public class HtmlReporter implements Reporter {
029:
030: private final PrettyPrintWriter out;
031: private final String title;
032:
033: private Metric currentMetric;
034: private double largestMetricForTarget;
035: private List resultsForTarget;
036:
037: public HtmlReporter(File htmlFile, String title) throws IOException {
038: this .title = title;
039: out = new PrettyPrintWriter(new FileWriter(htmlFile));
040: }
041:
042: public void startBenchmark() {
043: out.startNode("html");
044:
045: out.startNode("head");
046: writeTag("title", title);
047: writeTag("style", css(), "type", "text/css");
048: out.endNode();
049:
050: out.startNode("body");
051:
052: writeTag("h1", title);
053: }
054:
055: private String css() {
056: StringBuffer css = new StringBuffer("\n");
057: css.append("body, h1, h2, h3, td { font-family: arial; }\n");
058: css.append("h1 { text-align: center; }\n");
059: css.append("table, h3 { margin-left: 40px; }\n");
060: css
061: .append("table, td, th { border: 1px solid #999; border-collapse: collapse; font-size: smaller; }\n");
062: css.append(".success { color: #090; }\n");
063: css.append(".fail { color: #900; }\n");
064: return css.toString();
065: }
066:
067: public void startMetric(Metric metric) {
068: writeTag("h2", metric.toString());
069: currentMetric = metric;
070: }
071:
072: public void startTarget(Target target) {
073: writeTag("h3", target.toString());
074: out.flush(); // Flush now, so progress can be seen with slow benchmarks.
075: largestMetricForTarget = 0;
076: resultsForTarget = new ArrayList();
077: }
078:
079: public void metricRecorded(Product product, double result) {
080: // Keep a look out for the largest result.
081: if (result > largestMetricForTarget) {
082: largestMetricForTarget = result;
083: }
084: resultsForTarget.add(new MetricResult(product, result));
085: }
086:
087: public void metricFailed(Product product, Exception e) {
088: resultsForTarget.add(new MetricResult(product, e));
089: }
090:
091: public void endTarget(Target target) {
092: out.startNode("table");
093: out.startNode("tr");
094:
095: writeTag("th", "Product");
096: writeTag("th", currentMetric.unit());
097:
098: out.endNode();
099: for (Iterator iterator = resultsForTarget.iterator(); iterator
100: .hasNext();) {
101: MetricResult metricResult = (MetricResult) iterator.next();
102: out.startNode("tr");
103:
104: writeTag("td", metricResult.product.toString());
105:
106: if (metricResult.exception == null) {
107: writeTag("td", String.valueOf(metricResult.result),
108: "class", "success");
109:
110: long percentage = Math.round(Math
111: .abs(metricResult.result
112: / largestMetricForTarget) * 100.0);
113: out.startNode("td");
114: out.addAttribute("style", "width: 400px;");
115: writeTag("div", "", "style", "height: 100%; width: "
116: + percentage + "%; background-color: blue;");
117: out.endNode();
118: } else {
119: writeTag("td", "FAIL", "class", "fail");
120: writeTag("td", metricResult.exception.toString());
121: }
122:
123: out.endNode();
124: }
125: out.endNode();
126: out.flush(); // Flush now, so progress can be seen with slow benchmarks.
127: }
128:
129: private void writeTag(String tag, String value) {
130: out.startNode(tag);
131: out.setValue(value);
132: out.endNode();
133: }
134:
135: private void writeTag(String tag, String value,
136: String attributeName, String attributeValue) {
137: out.startNode(tag);
138: out.addAttribute(attributeName, attributeValue);
139: out.setValue(value);
140: out.endNode();
141: }
142:
143: public void endMetric(Metric metric) {
144: }
145:
146: public void endBenchmark() {
147: writeTag("p", new Date().toString());
148: out.endNode();
149: out.endNode();
150: out.close();
151: }
152:
153: private static class MetricResult {
154: private final Product product;
155: private final double result;
156: private final Exception exception;
157:
158: public MetricResult(Product product, double result) {
159: this .result = result;
160: this .product = product;
161: this .exception = null;
162: }
163:
164: public MetricResult(Product product, Exception exception) {
165: this .product = product;
166: this .result = 0;
167: this.exception = exception;
168: }
169: }
170: }
|