001: /*
002: * Copyright (C) 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 13. September 2007 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream.benchmark.xmlfriendly;
012:
013: import com.thoughtworks.xstream.InitializationException;
014: import com.thoughtworks.xstream.XStream;
015: import com.thoughtworks.xstream.benchmark.reflection.targets.FieldReflection;
016: import com.thoughtworks.xstream.benchmark.xmlfriendly.metric.CharacterCountMetric;
017: import com.thoughtworks.xstream.benchmark.xmlfriendly.product.CombinedLookupAppender;
018: import com.thoughtworks.xstream.benchmark.xmlfriendly.product.CombinedLookupReplacer;
019: import com.thoughtworks.xstream.benchmark.xmlfriendly.product.IterativeAppender;
020: import com.thoughtworks.xstream.benchmark.xmlfriendly.product.IterativeReplacer;
021: import com.thoughtworks.xstream.benchmark.xmlfriendly.product.NoReplacer;
022: import com.thoughtworks.xstream.benchmark.xmlfriendly.product.SeparateLookupReplacer;
023: import com.thoughtworks.xstream.benchmark.xmlfriendly.product.XStream122Replacer;
024: import com.thoughtworks.xstream.benchmark.xmlfriendly.target.Field_Reflection;
025: import com.thoughtworks.xstream.benchmark.xmlfriendly.target.Field$Reflection;
026: import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
027: import com.thoughtworks.xstream.io.xml.XppDriver;
028: import com.thoughtworks.xstream.tools.benchmark.Harness;
029: import com.thoughtworks.xstream.tools.benchmark.Reporter;
030: import com.thoughtworks.xstream.tools.benchmark.metrics.DeserializationSpeedMetric;
031: import com.thoughtworks.xstream.tools.benchmark.metrics.SerializationSpeedMetric;
032: import com.thoughtworks.xstream.tools.benchmark.metrics.SizeMetric;
033: import com.thoughtworks.xstream.tools.benchmark.reporters.HtmlReporter;
034: import com.thoughtworks.xstream.tools.benchmark.reporters.MultiReporter;
035: import com.thoughtworks.xstream.tools.benchmark.reporters.TextReporter;
036:
037: import junit.framework.Test;
038: import junit.framework.TestCase;
039: import junit.framework.TestSuite;
040:
041: import java.io.File;
042: import java.io.FileWriter;
043: import java.io.IOException;
044: import java.lang.reflect.Constructor;
045: import java.lang.reflect.InvocationTargetException;
046:
047: /**
048: * Main application to run harness for Reflection benchmark.
049: *
050: * @author Jörg Schaible
051: */
052: public class XmlFriendlyBenchmark extends TestSuite {
053:
054: public static class __ {
055: public static class UnfriendlyClass {
056: String __a__$$a__;
057: String b__b__;
058: String __c__c;
059:
060: public boolean equals(Object obj) {
061: UnfriendlyClass other = (UnfriendlyClass) obj;
062: return __a__$$a__.equals(other.__a__$$a__)
063: && b__b__.equals(other.b__b__)
064: && __c__c.equals(other.__c__c);
065: }
066:
067: }
068: }
069:
070: private static Class currentType;
071:
072: public static class ReplacerTest extends TestCase {
073:
074: private final Class type;
075:
076: public ReplacerTest(String name) {
077: super (name);
078: type = currentType;
079: }
080:
081: public String getName() {
082: return type.getName() + ": " + super .getName();
083: }
084:
085: public void testReplacerWithDefaultReplacements() {
086: String xml = ""
087: + "<com.thoughtworks.xstream.benchmark.xmlfriendly.XmlFriendlyBenchmark_-_____-UnfriendlyClass>\n"
088: + " <____a_____-_-a____>a</____a_____-_-a____>\n"
089: + " <b____b____>b</b____b____>\n"
090: + " <____c____c>c</____c____c>\n"
091: + "</com.thoughtworks.xstream.benchmark.xmlfriendly.XmlFriendlyBenchmark_-_____-UnfriendlyClass>";
092: performTest("_-", "__", getReference(), xml);
093: }
094:
095: public void testReplacerWithDollarReplacementOnly() {
096: String xml = ""
097: + "<com.thoughtworks.xstream.benchmark.xmlfriendly.XmlFriendlyBenchmark_-___-UnfriendlyClass>\n"
098: + " <__a___-_-a__>a</__a___-_-a__>\n"
099: + " <b__b__>b</b__b__>\n"
100: + " <__c__c>c</__c__c>\n"
101: + "</com.thoughtworks.xstream.benchmark.xmlfriendly.XmlFriendlyBenchmark_-___-UnfriendlyClass>";
102: performTest("_-", "_", getReference(), xml);
103: }
104:
105: private void performTest(String dollar, String underscore,
106: __.UnfriendlyClass object, String xml) {
107: XStream xstream = createXStreamWithReplacer(dollar,
108: underscore);
109: assertEquals(xml, xstream.toXML(object));
110: assertEquals(object, xstream.fromXML(xml));
111: }
112:
113: private __.UnfriendlyClass getReference() {
114: __.UnfriendlyClass ref = new __.UnfriendlyClass();
115: ref.__a__$$a__ = "a";
116: ref.b__b__ = "b";
117: ref.__c__c = "c";
118: return ref;
119: }
120:
121: private XStream createXStreamWithReplacer(String dollar,
122: String underscore) {
123: Exception ex;
124: try {
125: Constructor constructor = type
126: .getConstructor(new Class[] { String.class,
127: String.class, int.class });
128: XmlFriendlyReplacer replacer = (XmlFriendlyReplacer) constructor
129: .newInstance(new Object[] { dollar, underscore,
130: new Integer(0) });
131: return new XStream(new XppDriver(replacer));
132: } catch (NoSuchMethodException e) {
133: ex = e;
134: } catch (InstantiationException e) {
135: ex = e;
136: } catch (IllegalAccessException e) {
137: ex = e;
138: } catch (InvocationTargetException e) {
139: ex = e;
140: }
141: throw new InitializationException(
142: "Cannot initialize XmlFriendlyReplacer", ex);
143: }
144: }
145:
146: XmlFriendlyBenchmark() {
147: addTestSuite(XStream122Replacer.XmlFriendlyReplacer.class);
148: addTestSuite(CombinedLookupAppender.XmlFriendlyReplacer.class);
149: addTestSuite(CombinedLookupReplacer.XmlFriendlyReplacer.class);
150: addTestSuite(IterativeAppender.XmlFriendlyReplacer.class);
151: addTestSuite(IterativeReplacer.XmlFriendlyReplacer.class);
152: addTestSuite(SeparateLookupReplacer.XmlFriendlyReplacer.class);
153: }
154:
155: public void addTestSuite(Class replacerClass) {
156: currentType = replacerClass;
157: super .addTestSuite(ReplacerTest.class);
158: }
159:
160: public static Test suite() {
161: // Ensure the different implementations work
162: return new XmlFriendlyBenchmark();
163: }
164:
165: public static void main(String[] args) {
166: new File("target/benchmarks").mkdirs();
167:
168: Reporter[] reporters;
169: try {
170: String basename = "target/benchmarks/xmlfriendly-"
171: + System.getProperty("user.name");
172: reporters = new Reporter[] {
173: new TextReporter(),
174: new TextReporter(new FileWriter(basename + ".txt")),
175: new HtmlReporter(new File(basename + ".html"),
176: "XmlFriendlyReplacer Benchmark") };
177: } catch (IOException e) {
178: throw new RuntimeException(e);
179: }
180:
181: Harness stats = new Harness();
182: stats.addMetric(new SizeMetric());
183: stats.addMetric(new CharacterCountMetric('$'));
184: stats.addMetric(new CharacterCountMetric('_'));
185: stats.addProduct(new NoReplacer());
186: stats.addTarget(new FieldReflection());
187: stats.addTarget(new Field_Reflection());
188: stats.addTarget(new Field$Reflection());
189: stats.run(new MultiReporter(reporters) {
190:
191: public void endBenchmark() {
192: // do nothing
193: }
194:
195: });
196:
197: Harness harness = new Harness();
198: harness.addMetric(new SerializationSpeedMetric(50));
199: harness.addMetric(new DeserializationSpeedMetric(50, false));
200: harness.addProduct(new XStream122Replacer());
201: harness.addProduct(new CombinedLookupAppender(0));
202: harness.addProduct(new CombinedLookupAppender(16));
203: harness.addProduct(new CombinedLookupReplacer(0));
204: harness.addProduct(new CombinedLookupReplacer(16));
205: harness.addProduct(new IterativeAppender(0));
206: harness.addProduct(new IterativeAppender(16));
207: harness.addProduct(new IterativeReplacer(0));
208: harness.addProduct(new IterativeReplacer(16));
209: harness.addProduct(new SeparateLookupReplacer(0));
210: harness.addProduct(new SeparateLookupReplacer(16));
211: harness.addTarget(new FieldReflection());
212: harness.addTarget(new Field_Reflection());
213: harness.addTarget(new Field$Reflection());
214: harness.run(new MultiReporter(reporters) {
215:
216: public void startBenchmark() {
217: // do nothing
218: }
219:
220: });
221: }
222: }
|