001: /*
002: * Copyright (C) 2006 Joe Walnes.
003: * Copyright (C) 2006, 2007, 2008 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.metrics;
013:
014: import com.thoughtworks.xstream.tools.benchmark.Metric;
015: import com.thoughtworks.xstream.tools.benchmark.Product;
016: import com.thoughtworks.xstream.tools.benchmark.Target;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.ByteArrayInputStream;
020:
021: /**
022: * Determines how long it takes to deserialize an object (in ms).
023: *
024: * @author Joe Walnes
025: * @author Jörg Schaible
026: * @see com.thoughtworks.xstream.tools.benchmark.Harness
027: * @see Metric
028: */
029: public class DeserializationSpeedMetric implements Metric {
030:
031: private final int iterations;
032: private final boolean validate;
033:
034: /**
035: * Measure deserialization speed.
036: *
037: * @param iterations
038: * @deprecated since 1.3, use {@link #DeserializationSpeedMetric(int, boolean)}
039: */
040: public DeserializationSpeedMetric(int iterations) {
041: this (iterations, false);
042: }
043:
044: /**
045: * Measure deserialization speed.
046: * @param iterations
047: * @param validate flag to compare result of last iteration with original data
048: * @since 1.3
049: */
050: public DeserializationSpeedMetric(int iterations, boolean validate) {
051: this .iterations = iterations;
052: this .validate = validate;
053: }
054:
055: public double run(Product product, Target target) throws Exception {
056:
057: // Serialize once (because we need something to deserialize).
058: ByteArrayOutputStream output = new ByteArrayOutputStream();
059: product.serialize(target.target(), output);
060: byte[] data = output.toByteArray();
061:
062: // Deserialize once, to warm up.
063: product.deserialize(new ByteArrayInputStream(data));
064:
065: // Now lots of times
066: Object lastResult = null;
067: long start = System.currentTimeMillis();
068: for (int i = 0; i < iterations; i++) {
069: lastResult = product.deserialize(new ByteArrayInputStream(
070: data));
071: }
072: long end = System.currentTimeMillis();
073: if (validate && iterations > 0) {
074: if (!target.isEqual(lastResult)) {
075: throw new RuntimeException(
076: "Deserialized object is not equal");
077: }
078: }
079:
080: return (end - start);
081: }
082:
083: /**
084: *@deprecated since 1.3
085: */
086: public double run(Product product, Object object) throws Exception {
087:
088: // Serialize once (because we need something to deserialize).
089: ByteArrayOutputStream output = new ByteArrayOutputStream();
090: product.serialize(object, output);
091: byte[] data = output.toByteArray();
092:
093: // Deserialize once, to warm up.
094: product.deserialize(new ByteArrayInputStream(data));
095:
096: // Now lots of times
097: long start = System.currentTimeMillis();
098: for (int i = 0; i < iterations; i++) {
099: product.deserialize(new ByteArrayInputStream(data));
100: }
101: long end = System.currentTimeMillis();
102:
103: return (end - start);
104: }
105:
106: public String unit() {
107: return "ms";
108: }
109:
110: public boolean biggerIsBetter() {
111: return false;
112: }
113:
114: public String toString() {
115: return "Deserialization speed (" + iterations + " iteration"
116: + (iterations == 1 ? "" : "s") + ")";
117: }
118: }
|