01: /*
02: * Copyright (C) 2006 Joe Walnes.
03: * Copyright (C) 2006, 2007, 2008 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 15. July 2006 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.tools.benchmark.metrics;
13:
14: import com.thoughtworks.xstream.tools.benchmark.Metric;
15: import com.thoughtworks.xstream.tools.benchmark.Product;
16: import com.thoughtworks.xstream.tools.benchmark.Target;
17:
18: import java.io.ByteArrayOutputStream;
19:
20: /**
21: * Determines how long it takes to serialize an object (in ms).
22: *
23: * @author Joe Walnes
24: * @see com.thoughtworks.xstream.tools.benchmark.Harness
25: * @see Metric
26: */
27: public class SerializationSpeedMetric implements Metric {
28:
29: private int iterations;
30:
31: public SerializationSpeedMetric(int iterations) {
32: this .iterations = iterations;
33: }
34:
35: public double run(Product product, Target target) throws Exception {
36: return run(product, target.target());
37: }
38:
39: /**
40: *@deprecated since 1.3
41: */
42: public double run(Product product, Object object) throws Exception {
43: // Do it once to warm up.
44: product.serialize(object, new ByteArrayOutputStream());
45:
46: // Now lots of times
47: long start = System.currentTimeMillis();
48: for (int i = 0; i < iterations; i++) {
49: product.serialize(object, new ByteArrayOutputStream());
50: }
51: long end = System.currentTimeMillis();
52:
53: return (end - start);
54: }
55:
56: public String unit() {
57: return "ms";
58: }
59:
60: public boolean biggerIsBetter() {
61: return false;
62: }
63:
64: public String toString() {
65: return "Serialization speed (" + iterations + " iteration"
66: + (iterations == 1 ? "" : "s") + ")";
67: }
68: }
|