/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.serial.benchmarks;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import org.jboss.jrunit.controller.ThreadLocalBenchmark;
import org.jboss.serial.DataSerializationTest;
import org.jboss.serial.data.NonSerializableTest;
import org.jboss.serial.io.JBossObjectInputStream;
import org.jboss.serial.io.JBossObjectOutputStream;
import org.jboss.serial.objectmetamodel.DataContainer;
import org.jboss.serial.util.StringUtilBuffer;
/**
* $Id: SingleThreadBenchmarkTestCase.java 303 2006-05-23 16:53:58Z csuconic $
* @author Clebert Suconic
*/
public class SingleThreadBenchmarkTestCase extends DataSerializationTest
{
public SingleThreadBenchmarkTestCase(String file)
{
super(file);
}
public SingleThreadBenchmarkTestCase()
{
super();
}
private static final boolean useJRunit=false;
/** As I'm comparing performance between JavaSerialization and JbossSerialization I guess
* I should a single point of creating an object, Just in case*/
public void executTest(Object dataObject) throws Throwable
{
if (dataObject instanceof NonSerializableTest)
{
return;
}
System.out.println();
System.out.println("Starting " + dataObject.getClass().getName() + "...........................:");
doTtestJbossSerializationMeasure(dataObject);
doTestJbossSerializationUsingByteArrayMeasure(dataObject);
doTestJavaSerializationMeasure(dataObject);
doTestJbossSerializationIndirect(dataObject);
if (useJRunit) ThreadLocalBenchmark.sendData();
}
public void doTtestJbossSerializationMeasure(Object myTest) throws Exception
{
long original = 0;
HashMap metaData = new HashMap();
metaData.put("method","doTtestJbossSerializationMeasure");
StringUtilBuffer buffer = new StringUtilBuffer(10*1024,10*1024);
for (int exec=0;exec<LOOPS;exec++)
{
if (exec==1)
{
if (useJRunit) ThreadLocalBenchmark.openBench(myTest.getClass().getName(),metaData);
original = System.currentTimeMillis();
}
DataContainer container = new DataContainer(false,buffer);
ObjectOutput out = container.getOutput();
out.writeObject(myTest);
container.flush();
ObjectInput input = container.getInput();
Object value = input.readObject();
if (!(myTest instanceof String[]))
{
assertEquals(value,myTest);
}
assertSame(value.getClass(),myTest.getClass());
}
System.out.println("Total time for doTtestJbossSerializationMeasure(" + myTest.getClass().getName() +") = " + (System.currentTimeMillis() - original));
if (useJRunit) ThreadLocalBenchmark.closeBench( myTest.getClass().getName());
}
public void doTestJbossSerializationUsingByteArrayMeasure(Object myTest) throws Exception
{
long original = 0;
HashMap metaData = new HashMap();
metaData.put("method","doTestJbossSerializationUsingByteArrayMeasure");
StringUtilBuffer buffer = new StringUtilBuffer(10*1024,10*1024);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
for (int exec=0;exec<LOOPS;exec++)
{
if (exec==1)
{
if (useJRunit) ThreadLocalBenchmark.openBench(myTest.getClass().getName(),metaData);
original = System.currentTimeMillis();
}
byteOut.reset();
JBossObjectOutputStream out = new JBossObjectOutputStream(byteOut,buffer);
out.writeObject(myTest);
out.flush();
ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOut.toByteArray());
JBossObjectInputStream input = new JBossObjectInputStream(byteInput,buffer);
Object value = input.readObject();
assertTrue(value!=myTest);
assertTrue(value.getClass()==myTest.getClass());
if (!(myTest instanceof String[]))assertTrue(value.equals(myTest));
}
System.out.println("Total time for doTestJbossSerializationUsingByteArrayMeasure(" + myTest.getClass().getName() +") = " + (System.currentTimeMillis() - original));
if (useJRunit) ThreadLocalBenchmark.closeBench(myTest.getClass().getName());
}
public void doTestJbossSerializationIndirect(Object myTest) throws Exception
{
long original = 0;
HashMap metaData = new HashMap();
metaData.put("method","doTestJbossSerializationIndirect");
StringUtilBuffer buffer = new StringUtilBuffer(10*1024,10*1024);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
for (int exec=0;exec<LOOPS;exec++)
{
if (exec==1)
{
if (useJRunit) ThreadLocalBenchmark.openBench(myTest.getClass().getName(),metaData);
original = System.currentTimeMillis();
}
byteOut.reset();
JBossObjectOutputStream out = new JBossObjectOutputStream(byteOut,buffer);
out.writeObjectUsingDataContainer(myTest);
out.flush();
ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOut.toByteArray());
JBossObjectInputStream input = new JBossObjectInputStream(byteInput,buffer);
Object value = input.readObjectUsingDataContainer();
assertTrue(value!=myTest);
assertTrue(value.getClass()==myTest.getClass());
if (!(myTest instanceof String[]))assertTrue(value.equals(myTest));
}
System.out.println("Total time for doTestJbossSerializationIndirect(" + myTest.getClass().getName() +") = " + (System.currentTimeMillis() - original));
if (useJRunit) ThreadLocalBenchmark.closeBench(myTest.getClass().getName());
}
public void doTestJavaSerializationMeasure(Object myTest) throws Exception
{
long original = 0;
HashMap metaData = new HashMap();
metaData.put("method","doTestJavaSerializationMeasure");
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
for (int exec=0;exec<LOOPS;exec++)
{
if (exec==1)
{
if (useJRunit) ThreadLocalBenchmark.openBench(myTest.getClass().getName(),metaData);
original=System.currentTimeMillis();
}
byteOutput.reset();
ObjectOutputStream objectOut = new ObjectOutputStream(byteOutput);
objectOut.writeObject(myTest);
ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray());
ObjectInputStream objInput = new ObjectInputStream(byteInput);
Object value = objInput.readObject();
assertTrue(value!=myTest);
assertTrue(value.getClass()==myTest.getClass());
if (!(myTest instanceof String[]))assertTrue(value.equals(myTest));
}
System.out.println("Total time for doTestJavaSerializationMeasure(" + myTest.getClass().getName() +") = " + (System.currentTimeMillis() - original));
if (useJRunit) ThreadLocalBenchmark.closeBench(myTest.getClass().getName());
}
}