001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.performance.serialize;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028:
029: import junit.framework.TestCase;
030: import test.performance.PerformanceSUITE;
031:
032: /**
033: * Tests the speed of serialization
034: *
035: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
036: */
037: public class SerializeTEST extends TestCase {
038: // Attributes ----------------------------------------------------------------
039:
040: /**
041: * The object to test
042: */
043: private Object obj;
044:
045: /**
046: * The description of the test
047: */
048: private String desc;
049:
050: // Constructor ---------------------------------------------------------------
051:
052: /**
053: * Construct the test
054: */
055: public SerializeTEST(String s, Object obj, String desc) {
056: super (s);
057: this .obj = obj;
058: this .desc = desc;
059: }
060:
061: /**
062: * Test Serialization
063: */
064: public void testIt() {
065: System.out.println("\n" + desc);
066: System.out.println(PerformanceSUITE.SERIALIZE_ITERATION_COUNT
067: + " Serializations, Repeat: x"
068: + PerformanceSUITE.REPEAT_COUNT);
069: System.out.println("(this may take a while...)");
070:
071: long start = 0, end = 0;
072: float avg = 0l;
073: int size = 0;
074:
075: try {
076:
077: Object result = null;
078: ByteArrayOutputStream baos = new ByteArrayOutputStream();
079:
080: // drop the first batch (+1)
081: for (int testIterations = 0; testIterations < PerformanceSUITE.REPEAT_COUNT + 1; ++testIterations) {
082: start = System.currentTimeMillis();
083: for (int invocationIterations = 0; invocationIterations < PerformanceSUITE.SERIALIZE_ITERATION_COUNT; ++invocationIterations) {
084: // Serialize it
085: baos.reset();
086: ObjectOutputStream oos = new ObjectOutputStream(
087: baos);
088: oos.writeObject(obj);
089:
090: // Deserialize it
091: ByteArrayInputStream bais = new ByteArrayInputStream(
092: baos.toByteArray());
093: ObjectInputStream ois = new ObjectInputStream(bais);
094: result = ois.readObject();
095: }
096: end = System.currentTimeMillis();
097:
098: if (testIterations != 0) {
099: long time = end - start;
100: System.out.print(time + " ");
101: avg += time;
102: }
103: }
104:
105: System.out.print("\nAverage: "
106: + (avg / PerformanceSUITE.REPEAT_COUNT));
107: System.out.println(" Size: " + baos.toByteArray().length);
108: } catch (Exception e) {
109: fail(e.toString());
110: }
111: }
112: }
|