Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

BindingSpeedTest.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: BindingSpeedTest.java,v 12.1 2005/01/31 19:27:35 mark Exp $
00008  */
00009 
00010 package com.sleepycat.bind.test;
00011 
00012 import java.io.Externalizable;
00013 import java.io.IOException;
00014 import java.io.ObjectInput;
00015 import java.io.ObjectInputStream;
00016 import java.io.ObjectOutput;
00017 import java.io.ObjectOutputStream;
00018 import java.io.OutputStreamWriter;
00019 import java.io.Serializable;
00020 import java.io.Writer;
00021 
00022 import javax.xml.parsers.SAXParserFactory;
00023 
00024 import junit.framework.Test;
00025 import junit.framework.TestCase;
00026 import junit.framework.TestSuite;
00027 
00028 import org.xml.sax.InputSource;
00029 import org.xml.sax.XMLReader;
00030 
00031 import com.sleepycat.bind.serial.SerialInput;
00032 import com.sleepycat.bind.serial.SerialOutput;
00033 import com.sleepycat.bind.serial.test.TestClassCatalog;
00034 import com.sleepycat.bind.tuple.TupleInput;
00035 import com.sleepycat.bind.tuple.TupleOutput;
00036 import com.sleepycat.collections.test.DbTestUtil;
00037 import com.sleepycat.util.FastInputStream;
00038 import com.sleepycat.util.FastOutputStream;
00039 
00043 public class BindingSpeedTest extends TestCase {
00044 
00045     static final String JAVA_UNSHARED = "java-unshared".intern();
00046     static final String JAVA_SHARED = "java-shared".intern();
00047     static final String JAVA_EXTERNALIZABLE = "java-externalizable".intern();
00048     static final String XML_SAX = "xml-sax".intern();
00049     static final String TUPLE = "tuple".intern();
00050 
00051     static final int RUN_COUNT = 1000;
00052     static final boolean VERBOSE = false;
00053 
00054     public static void main(String[] args)
00055         throws Exception {
00056 
00057         junit.framework.TestResult tr =
00058             junit.textui.TestRunner.run(suite());
00059         if (tr.errorCount() > 0 ||
00060             tr.failureCount() > 0) {
00061             System.exit(1);
00062         } else {
00063             System.exit(0);
00064         }
00065     }
00066 
00067     public static Test suite() {
00068 
00069         TestSuite suite = new TestSuite();
00070         suite.addTest(new BindingSpeedTest(JAVA_UNSHARED));
00071         suite.addTest(new BindingSpeedTest(JAVA_SHARED));
00072         suite.addTest(new BindingSpeedTest(JAVA_EXTERNALIZABLE));
00073         suite.addTest(new BindingSpeedTest(XML_SAX));
00074         suite.addTest(new BindingSpeedTest(TUPLE));
00075         return suite;
00076     }
00077 
00078     private String command;
00079     private FastOutputStream fo;
00080     private TupleOutput to;
00081     private TestClassCatalog jtc;
00082     private byte[] buf;
00083     private XMLReader parser;
00084 
00085     public BindingSpeedTest(String name) {
00086 
00087         super("BindingSpeedTest." + name);
00088         command = name;
00089     }
00090 
00091     public void runTest()
00092         throws Exception {
00093 
00094         DbTestUtil.printTestName(getName());
00095 
00096         boolean isTuple = false;
00097         boolean isXmlSax = false;
00098         boolean isSerial = false;
00099         boolean isShared = false;
00100         boolean isExternalizable = false;
00101 
00102         if (command == TUPLE) {
00103             isTuple = true;
00104         } else if (command == XML_SAX) {
00105             isXmlSax = true;
00106         } else if (command == JAVA_UNSHARED) {
00107             isSerial = true;
00108         } else if (command == JAVA_SHARED) {
00109             isSerial = true;
00110             isShared = true;
00111         } else if (command == JAVA_EXTERNALIZABLE) {
00112             isSerial = true;
00113             isShared = true;
00114             isExternalizable = true;
00115         } else {
00116             throw new Exception("invalid command: " + command);
00117         }
00118 
00119         // Do initialization
00120 
00121         if (isTuple) {
00122             initTuple();
00123         } else if (isXmlSax) {
00124             initXmlSax();
00125         } else if (isSerial) {
00126             if (isShared) {
00127                 initSerialShared();
00128             } else {
00129                 initSerialUnshared();
00130             }
00131         }
00132 
00133         // Prime the Java compiler
00134 
00135         int size = 0;
00136         for (int i = 0; i < RUN_COUNT; i += 1) {
00137 
00138             if (isTuple) {
00139                 size = runTuple();
00140             } else if (isXmlSax) {
00141                 size = runXmlSax();
00142             } else if (isSerial) {
00143                 if (isShared) {
00144                     if (isExternalizable) {
00145                         size = runSerialExternalizable();
00146                     } else {
00147                         size = runSerialShared();
00148                     }
00149                 } else {
00150                     size = runSerialUnshared();
00151                 }
00152             }
00153         }
00154 
00155         // Then run the timing tests
00156 
00157         long startTime = System.currentTimeMillis();
00158 
00159         for (int i = 0; i < RUN_COUNT; i += 1) {
00160             if (isTuple) {
00161                 size = runTuple();
00162             } else if (isXmlSax) {
00163                 size = runXmlSax();
00164             } else if (isSerial) {
00165                 if (isShared) {
00166                     if (isExternalizable) {
00167                         size = runSerialExternalizable();
00168                     } else {
00169                         size = runSerialShared();
00170                     }
00171                 } else {
00172                     size = runSerialUnshared();
00173                 }
00174             }
00175         }
00176 
00177         long stopTime = System.currentTimeMillis();
00178 
00179         assertTrue("data size too big", size < 250);
00180 
00181         if (VERBOSE) {
00182             System.out.println(command);
00183             System.out.println("data size: " + size);
00184             System.out.println("run time:  " +
00185                 ((stopTime - startTime) / (double) RUN_COUNT));
00186         }
00187     }
00188 
00189     public void tearDown() {
00190 
00191         /* Ensure that GC can cleanup. */
00192         command = null;
00193         fo = null;
00194         to = null;
00195         jtc = null;
00196         buf = null;
00197         parser = null;
00198     }
00199 
00200     void initSerialUnshared()
00201         throws Exception {
00202 
00203         fo = new FastOutputStream();
00204     }
00205 
00206     int runSerialUnshared()
00207         throws Exception {
00208 
00209         fo.reset();
00210         ObjectOutputStream oos = new ObjectOutputStream(fo);
00211         oos.writeObject(new Data());
00212         byte[] bytes = fo.toByteArray();
00213         FastInputStream fi = new FastInputStream(bytes);
00214         ObjectInputStream ois = new ObjectInputStream(fi);
00215         ois.readObject();
00216         return bytes.length;
00217     }
00218 
00219     void initSerialShared()
00220         throws Exception {
00221 
00222         jtc = new TestClassCatalog();
00223         fo = new FastOutputStream();
00224     }
00225 
00226     int runSerialShared()
00227         throws Exception {
00228 
00229         fo.reset();
00230         SerialOutput oos = new SerialOutput(fo, jtc);
00231         oos.writeObject(new Data());
00232         byte[] bytes = fo.toByteArray();
00233         FastInputStream fi = new FastInputStream(bytes);
00234         SerialInput ois = new SerialInput(fi, jtc);
00235         ois.readObject();
00236         return (bytes.length - SerialOutput.getStreamHeader().length);
00237     }
00238 
00239     int runSerialExternalizable()
00240         throws Exception {
00241 
00242         fo.reset();
00243         SerialOutput oos = new SerialOutput(fo, jtc);
00244         oos.writeObject(new Data2());
00245         byte[] bytes = fo.toByteArray();
00246         FastInputStream fi = new FastInputStream(bytes);
00247         SerialInput ois = new SerialInput(fi, jtc);
00248         ois.readObject();
00249         return (bytes.length - SerialOutput.getStreamHeader().length);
00250     }
00251 
00252     void initTuple()
00253         throws Exception {
00254 
00255         buf = new byte[500];
00256         to = new TupleOutput(buf);
00257     }
00258 
00259     int runTuple()
00260         throws Exception {
00261 
00262         to.reset();
00263         new Data().writeTuple(to);
00264 
00265         TupleInput ti = new TupleInput(
00266                           to.getBufferBytes(), to.getBufferOffset(),
00267                           to.getBufferLength());
00268         new Data().readTuple(ti);
00269 
00270         return to.getBufferLength();
00271     }
00272 
00273     void initXmlSax()
00274         throws Exception {
00275 
00276         buf = new byte[500];
00277         fo = new FastOutputStream();
00278         SAXParserFactory saxFactory = SAXParserFactory.newInstance();
00279         saxFactory.setNamespaceAware(true);
00280         parser = saxFactory.newSAXParser().getXMLReader();
00281     }
00282 
00283     int runXmlSax()
00284         throws Exception {
00285 
00286         fo.reset();
00287         OutputStreamWriter writer = new OutputStreamWriter(fo);
00288         new Data().writeXmlText(writer);
00289 
00290         byte[] bytes = fo.toByteArray();
00291         FastInputStream fi = new FastInputStream(bytes);
00292         InputSource input = new InputSource(fi);
00293         parser.parse(input);
00294 
00295         //InputStreamReader reader = new InputStreamReader(fi);
00296         //new Data().readXmlText(??);
00297 
00298         return bytes.length;
00299     }
00300 
00301     static class Data2 extends Data implements Externalizable {
00302 
00303         public Data2() {}
00304 
00305         public void readExternal(ObjectInput in)
00306             throws IOException, ClassNotFoundException {
00307 
00308             field1 = in.readUTF();
00309             field2 = in.readUTF();
00310             field3 = in.readInt();
00311             field4 = in.readInt();
00312             field5 = in.readUTF();
00313         }
00314 
00315         public void writeExternal(ObjectOutput out)
00316             throws IOException {
00317 
00318             out.writeUTF(field1);
00319             out.writeUTF(field2);
00320             out.writeInt(field3);
00321             out.writeInt(field4);
00322             out.writeUTF(field5);
00323         }
00324     }
00325 
00326     static class Data implements Serializable {
00327 
00328         String field1 = "field1";
00329         String field2 = "field2";
00330         int field3 = 333;
00331         int field4 = 444;
00332         String field5 = "field5";
00333 
00334         void readTuple(TupleInput _input) {
00335 
00336             field1 = _input.readString();
00337             field2 = _input.readString();
00338             field3 = _input.readInt();
00339             field4 = _input.readInt();
00340             field5 = _input.readString();
00341         }
00342 
00343         void writeTuple(TupleOutput _output) {
00344 
00345             _output.writeString(field1);
00346             _output.writeString(field2);
00347             _output.writeInt(field3);
00348             _output.writeInt(field4);
00349             _output.writeString(field5);
00350         }
00351 
00352         void writeXmlText(Writer writer) throws IOException {
00353 
00354             writer.write("<Data><Field1>");
00355             writer.write(field1);
00356             writer.write("</Field1><Field2>");
00357             writer.write(field2);
00358             writer.write("</Field2><Field3>");
00359             writer.write(String.valueOf(field3));
00360             writer.write("</Field3><Field4>");
00361             writer.write(String.valueOf(field4));
00362             writer.write("</Field4><Field5>");
00363             writer.write(field5);
00364             writer.write("</Field5></Data>");
00365             writer.flush();
00366         }
00367     }
00368 }

Generated on Sun Dec 25 12:14:53 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2