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 15. July 2006 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.tools.benchmark;
013:
014: import java.util.List;
015: import java.util.ArrayList;
016: import java.util.Iterator;
017:
018: /**
019: * A simple harness for running benchmarks over object serialization products.
020: * <p/>
021: * There are three dimensions that can be added:
022: * <ul>
023: * <li>{@link Product} (e.g. DOM, SAX, XPP...)</li>
024: * <li>{@link Metric} (e.g. time taken, memory usage, output size...)</li>
025: * <li>{@link Target} (e.g. a small object, large object, list of objects...)</li>
026: * </ul>
027: * The Harness will then across every permutation of these
028: * (in order of product, metric, target), and write the results to a {@link Reporter}.
029: *
030: * <h3>Example usage</h3>
031: * <pre>
032: * Harness harness = new Harness();
033: *
034: * // Compare speed of serialization/deserialization metrics...
035: * harness.addMetric(new SerializationSpeedMetric());
036: * harness.addMetric(new DeserializationSpeedMetric());
037: *
038: * // Using a simple String and a JTree instance...
039: * harness.addTarget(new StringTarget());
040: * harness.addTarget(new JTreeTarget());
041: *
042: * // Across XStream with different XML drivers.
043: * harness.addProduct(new XStreamDom());
044: * harness.addProduct(new XStreamXpp());
045: * harness.addProduct(new XStreamSax());
046: *
047: * // Now do it, and report the results as text to the console.
048: * harness.run(new TextReporter());
049: * </pre>
050: *
051: * @author Joe Walnes
052: */
053: public class Harness {
054:
055: private List products = new ArrayList();
056: private List targets = new ArrayList();
057: private List metrics = new ArrayList();
058:
059: public synchronized void addProduct(Product product) {
060: products.add(product);
061: }
062:
063: public synchronized void addTarget(Target target) {
064: targets.add(target);
065: }
066:
067: public synchronized void addMetric(Metric metric) {
068: metrics.add(metric);
069: }
070:
071: public synchronized void run(Reporter reporter) {
072: // Nested loop galore.
073: reporter.startBenchmark();
074: for (Iterator metricsIt = metrics.iterator(); metricsIt
075: .hasNext();) {
076: Metric metric = (Metric) metricsIt.next();
077: reporter.startMetric(metric);
078: for (Iterator targetIt = targets.iterator(); targetIt
079: .hasNext();) {
080: Target target = (Target) targetIt.next();
081: reporter.startTarget(target);
082: for (Iterator productsIt = products.iterator(); productsIt
083: .hasNext();) {
084: Product product = (Product) productsIt.next();
085: run(reporter, metric, target, product);
086: }
087: reporter.endTarget(target);
088: }
089: reporter.endMetric(metric);
090: }
091: reporter.endBenchmark();
092: }
093:
094: private void run(Reporter reporter, Metric metric, Target target,
095: Product product) {
096: try {
097: double result = metric.run(product, target);
098: reporter.metricRecorded(product, result);
099: } catch (Exception e) {
100: reporter.metricFailed(product, e);
101: }
102: }
103:
104: }
|