Benchmark Test - beckchr/staxon GitHub Wiki

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.codehaus.jettison.badgerfish.BadgerFishXMLInputFactory;
import org.codehaus.jettison.badgerfish.BadgerFishXMLOutputFactory;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;

import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
import com.carrotsearch.junitbenchmarks.BenchmarkRule;

import de.odysseus.staxon.json.JsonXMLInputFactory;
import de.odysseus.staxon.json.JsonXMLOutputFactory;
import de.odysseus.staxon.json.JsonXMLStreamConstants;
import de.odysseus.staxon.json.stream.JsonStreamFactory;
import de.odysseus.staxon.json.stream.jackson.JacksonStreamFactory;

@BenchmarkOptions(benchmarkRounds = 5, warmupRounds = 5)
public class Benchmark {
	static int height = 15;

	static ByteArrayOutputStream json = new ByteArrayOutputStream();
	static ByteArrayOutputStream xml = new ByteArrayOutputStream();

	static JsonStreamFactory streamFactory = new JacksonStreamFactory();

	static XMLInputFactory createJsonXMLInputFactory() {
		return new JsonXMLInputFactory(streamFactory);
	}

	static XMLOutputFactory createJsonXMLOutputFactory(boolean autoArray) {
		JsonXMLOutputFactory factory = new JsonXMLOutputFactory(streamFactory);
		factory.setProperty(JsonXMLOutputFactory.PROP_AUTO_ARRAY, autoArray);
		return factory;
	}

	@BeforeClass
	public static void beforeClass() throws XMLStreamException {
		write(createJsonXMLOutputFactory(false).createXMLStreamWriter(json), true);
		write(XMLOutputFactory.newInstance().createXMLStreamWriter(xml), false);
		System.out.println("#Nodes: " + ((1 << (height + 1)) - 1));
	}

	static InputStream jsonInput() {
		return new ByteArrayInputStream(json.toByteArray());
	}
	
	static InputStream xmlInput() {
		return new ByteArrayInputStream(xml.toByteArray());
	}

	static OutputStream tmpOutput() {
		return new ByteArrayOutputStream();
	}
	
	static void read(XMLStreamReader reader) throws XMLStreamException {
		while (reader.hasNext()) {
			reader.next();
		}
	}

	static void write(XMLStreamWriter writer, boolean multiplePI, int depth) throws XMLStreamException {
		if (depth < height) {
			if (multiplePI) {
				writer.writeProcessingInstruction(JsonXMLStreamConstants.MULTIPLE_PI_TARGET, "bob");
			}
			writer.writeStartElement("http://foo", "bob");
			writer.writeAttribute("david", "edgar");
			write(writer, multiplePI, depth + 1);
			writer.writeEndElement();

			writer.writeStartElement("http://foo", "bob");
			writer.writeAttribute("david", "charlie");
			write(writer, multiplePI, depth + 1);
			writer.writeEndElement();
		}
	}

	static void write(XMLStreamWriter writer, boolean multiplePI) throws XMLStreamException {
		writer.setDefaultNamespace("http://foo");
		writer.writeStartDocument();
		writer.writeStartElement("alice");
		writer.writeDefaultNamespace("http://foo");
		write(writer, multiplePI, 0);
		writer.writeEndElement();
		writer.writeEndDocument();
		writer.close();
	}

	@Rule
	public MethodRule benchmarkRun = new BenchmarkRule();

	@Test
	public void testReadJettison() throws XMLStreamException {
		read(new BadgerFishXMLInputFactory().createXMLStreamReader(jsonInput()));
	}

	@Test
	public void testWriteJettison_AutoArray() throws XMLStreamException {
		write(new BadgerFishXMLOutputFactory().createXMLStreamWriter(tmpOutput()), false);
	}

	@Test
	public void testReadStAXON() throws XMLStreamException {
		read(createJsonXMLInputFactory().createXMLStreamReader(jsonInput()));
	}

	@Test
	public void testWriteStAXON() throws XMLStreamException {
		write(createJsonXMLOutputFactory(false).createXMLStreamWriter(tmpOutput()), true);
	}

	@Test
	public void testWriteStAXON_AutoArray() throws XMLStreamException {
		write(createJsonXMLOutputFactory(true).createXMLStreamWriter(tmpOutput()), false);
	}

	@Test
	public void testReadXML() throws XMLStreamException {
		read(XMLInputFactory.newInstance().createXMLStreamReader(xmlInput()));
	}

	@Test
	public void testWriteXML() throws XMLStreamException {
		write(XMLOutputFactory.newInstance().createXMLStreamWriter(tmpOutput()), false);
	}
}