Skip to content

Instantly share code, notes, and snippets.

@jwcastillo
Forked from raphw/Benchmark
Created January 11, 2023 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwcastillo/ddeb4f0819a73d00133b9ed8f27397e2 to your computer and use it in GitHub Desktop.
Save jwcastillo/ddeb4f0819a73d00133b9ed8f27397e2 to your computer and use it in GitHub Desktop.
XML/JSON benchmark
package test;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
import foo.SampleBean;
import foo.SampleBeans;
import org.xml.sax.InputSource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.stream.*;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class JaxbTest {
public static void main(String[] args) throws Exception {
int size = 100_000, batchSize = 1_000;
SampleBeans sampleBeans = new SampleBeans();
for (int i = 0; i < size; i++) {
SampleBean bean = new SampleBean();
bean.setName("val" + i);
sampleBeans.getFoos().add(bean);
}
JAXBContext context = JAXBContext.newInstance(SampleBeans.class, SampleBean.class);
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
System.out.println(context.getClass());
System.out.println(inputFactory.getClass());
System.out.println(outputFactory.getClass());
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);
// HardCodedMapper hardCodedMapper = new HardCodedMapper();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(objectMapper.getTypeFactory()));
System.out.println("Size: " + size + ", batch: " + batchSize);
System.out.println("---------- write ----------");
List<String> bxml = batchedJaxb(context, sampleBeans, batchSize);
String sxml = streamedJaxb(context, outputFactory, sampleBeans);
List<String> bjxml = batchedJackson(xmlMapper, sampleBeans, batchSize);
String sjxml = streamedJackson(xmlMapper, outputFactory, sampleBeans);
List<String> bjjson = batchedJacksonJson(objectMapper, sampleBeans, batchSize);
String sjjson = streamedJacksonJson(objectMapper, sampleBeans);
// String sdxml = streamedDirect(hardCodedMapper, sbs);
System.out.println("---------- read ----------");
SampleBeans bbean = batchedJaxb(context, bxml);
SampleBeans sbean = streamedJaxb(context, inputFactory, sxml);
SampleBeans bjbean = batchedJackson(xmlMapper, bjxml);
SampleBeans sjbean = streamedJackson(xmlMapper, inputFactory, sjxml);
SampleBeans bjjsonbean = batchedJacksonJson(objectMapper, bjjson);
SampleBeans sjjsonbean = streamedJacksonJson(objectMapper, sjjson);
// SampleBeans sdbean = streamedDirect(hardCodedMapper, sdxml);
System.out.println("---------- control ----------");
System.out.println("Batched JAXB: " + bbean.getFoos().size());
System.out.println("Streamed JAXB: " + sbean.getFoos().size());
System.out.println("Batched Jackson: " + bjbean.getFoos().size());
System.out.println("Streamed Jackson: " + sjbean.getFoos().size());
System.out.println("Batched Jackson (JSON): " + bjjsonbean.getFoos().size());
System.out.println("Streamed Jackson (JSON): " + sjjsonbean.getFoos().size());
// System.out.println("Streamed direct: " + sdbean.getFoos().size());
}
private static List<String> batchedJaxb(JAXBContext context, SampleBeans sbs, int batchSize) throws Exception {
long time = System.currentTimeMillis();
Marshaller marshaller = context.createMarshaller();
List<String> result = new ArrayList<>();
for (int index = 0; index < sbs.getFoos().size() / batchSize; index++) {
StringWriter sw = new StringWriter(batchSize * 100);
SampleBeans batch = new SampleBeans();
batch.getFoos().addAll(sbs.getFoos().subList(index * batchSize, (index + 1) * batchSize));
marshaller.marshal(batch, sw);
result.add(sw.toString());
}
System.out.println("JAXB|batch|write: " + (System.currentTimeMillis() - time));
return result;
}
private static String streamedJaxb(JAXBContext context, XMLOutputFactory outputFactory, SampleBeans sbs) throws Exception {
long time = System.currentTimeMillis();
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
StringWriter sw = new StringWriter(sbs.getFoos().size() * 100);
XMLStreamWriter w = outputFactory.createXMLStreamWriter(sw);
w.writeStartDocument();
w.writeStartElement("foos");
for (SampleBean sb : sbs.getFoos()) {
marshaller.marshal(new JAXBElement<>(new QName("foo"), SampleBean.class, sb), w);
}
w.writeEndElement();
w.writeEndDocument();
w.close();
String result = sw.toString();
System.out.println("JAXB|stream|write: " + (System.currentTimeMillis() - time));
return result;
}
// private static String streamedDirect(HardCodedMapper mapper, SampleBeans sbs) throws Exception {
// long time = System.currentTimeMillis();
// String result = null;
//
// StringWriter sw = new StringWriter(sbs.getFoos().size() * 100);
// sw.append("<foos>");
//
// for (SampleBean sb : sbs.getFoos()) {
// mapper.toXml(sb, sw);
// }
//
// sw.append("</foos>");
// result = sw.toString();
//
// System.out.println("Streamed write with direct took " + (System.currentTimeMillis() - time));
// return result;
// }
private static List<String> batchedJackson(XmlMapper mapper, SampleBeans sbs, int batchSize) throws Exception {
long time = System.currentTimeMillis();
List<String> result = new ArrayList<>();
for (int index = 0; index < sbs.getFoos().size() / batchSize; index++) {
SampleBeans batch = new SampleBeans();
batch.getFoos().addAll(sbs.getFoos().subList(index * batchSize, (index + 1) * batchSize));
result.add(mapper.writeValueAsString(sbs));
}
System.out.println("Jackson|XML|batch|write: " + (System.currentTimeMillis() - time));
return result;
}
private static String streamedJackson(XmlMapper xmlMapper, XMLOutputFactory outputFactory, SampleBeans sbs) throws Exception {
long time = System.currentTimeMillis();
StringWriter sw = new StringWriter(sbs.getFoos().size() * 100);
XMLStreamWriter w = outputFactory.createXMLStreamWriter(sw);
w.writeStartDocument();
w.writeStartElement("foos");
for (SampleBean sb : sbs.getFoos()) {
xmlMapper.writeValue(w, sb);
}
w.writeEndElement();
w.writeEndDocument();
w.close();
String result = sw.toString();
System.out.println("Jackson|XML|stream|write: " + (System.currentTimeMillis() - time));
return result;
}
private static List<String> batchedJacksonJson(ObjectMapper mapper, SampleBeans sbs, int batchSize) throws Exception {
long time = System.currentTimeMillis();
List<String> result = new ArrayList<>();
for (int index = 0; index < sbs.getFoos().size() / batchSize; index++) {
SampleBeans batch = new SampleBeans();
batch.getFoos().addAll(sbs.getFoos().subList(index * batchSize, (index + 1) * batchSize));
result.add(mapper.writeValueAsString(batch));
}
System.out.println("Jackson|JSON|batch|write: " + (System.currentTimeMillis() - time));
return result;
}
private static String streamedJacksonJson(ObjectMapper mapper, SampleBeans sbs) throws Exception {
long time = System.currentTimeMillis();
StringWriter sw = new StringWriter(sbs.getFoos().size() * 100);
JsonGenerator generator = mapper.getFactory().createGenerator(sw);
for (SampleBean sb : sbs.getFoos()) {
mapper.writeValue(generator, sb);
}
String result = sw.toString();
System.out.println("Jackson|JSON|stream|write: " + (System.currentTimeMillis() - time));
return result;
}
private static SampleBeans batchedJaxb(JAXBContext context, List<String> xml) throws Exception {
long time = System.currentTimeMillis();
SampleBeans result = new SampleBeans();
for (String v : xml) {
SampleBeans i = (SampleBeans) context.createUnmarshaller().unmarshal(new InputSource(new StringReader(v)));
result.getFoos().addAll(i.getFoos());
}
System.out.println("JAXB|XML|batch|read: " + (System.currentTimeMillis() - time));
return result;
}
private static SampleBeans streamedJaxb(JAXBContext context, XMLInputFactory inputFactory, String xml) throws Exception {
long time = System.currentTimeMillis();
Unmarshaller unmarshaller = context.createUnmarshaller();
XMLStreamReader xer = inputFactory.createXMLStreamReader(new StringReader(xml));
xer.nextTag();
SampleBeans result = new SampleBeans();
while (xer.nextTag() == XMLStreamConstants.START_ELEMENT) {
SampleBean sampleBean = unmarshaller.unmarshal(xer, SampleBean.class).getValue();
result.getFoos().add(sampleBean);
}
System.out.println("JAXB|XML|stream|read: " + (System.currentTimeMillis() - time));
return result;
}
private static SampleBeans batchedJackson(XmlMapper xmlMapper, List<String> xml) throws Exception {
long time = System.currentTimeMillis();
SampleBeans result = new SampleBeans();
for (String x : xml) {
SampleBeans y = xmlMapper.readValue(x, SampleBeans.class);
result.getFoos().addAll(y.getFoos());
}
System.out.println("Jackson|XML|batch|read: " + (System.currentTimeMillis() - time));
return result;
}
private static SampleBeans streamedJackson(XmlMapper xmlMapper, XMLInputFactory inputFactory, String xml) throws Exception {
long time = System.currentTimeMillis();
XMLStreamReader xer = inputFactory.createXMLStreamReader(new StringReader(xml));
xer.nextTag();
SampleBeans result = new SampleBeans();
while (xer.nextTag() == XMLStreamConstants.START_ELEMENT) {
SampleBean sampleBean = xmlMapper.readValue(xer, SampleBean.class);
result.getFoos().add(sampleBean);
}
System.out.println("Jackson|XML|stream|read: " + (System.currentTimeMillis() - time));
return result;
}
// private static SampleBeans streamedDirect(HardCodedMapper mapper, String xml) throws Exception {
// char[] buffer = new char[1024];
// int foos = "<foos>".length();
// int foo = "<foo>".length();
//
// long time = System.currentTimeMillis();
//
// StringReader sr = new StringReader(xml);
// SampleBeans result = new SampleBeans();
//
// sr.read(buffer, 0, foos);
//
// sr.read(buffer, 0, foo);
// while (buffer[0] == '<' && buffer[1] == 'f' && buffer[2] == 'o' && buffer[3] == 'o' && buffer[4] == '>') {
// SampleBean sb = new SampleBean();
// mapper.fromXml(sb, sr);
// result.getFoos().add(sb);
//
// sr.read(buffer, 0, foo);
// }
//
// System.out.println("Streamed read with direct took " + (System.currentTimeMillis() - time));
// return result;
// }
private static SampleBeans batchedJacksonJson(ObjectMapper mapper, List<String> xml) throws Exception {
long time = System.currentTimeMillis();
SampleBeans result = new SampleBeans();
for (String x : xml) {
SampleBeans b = mapper.readValue(x, SampleBeans.class);
result.getFoos().addAll(b.getFoos());
}
System.out.println("JAckson|JSON|batch|read: " + (System.currentTimeMillis() - time));
return result;
}
private static SampleBeans streamedJacksonJson(ObjectMapper mapper, String xml) throws Exception {
long time = System.currentTimeMillis();
JsonParser parser = mapper.getFactory().createParser(xml);
parser.setCodec(mapper);
SampleBeans result = new SampleBeans();
Iterator<SampleBean> sb = parser.readValuesAs(SampleBean.class);
while (sb.hasNext()) {
result.getFoos().add(sb.next());
}
System.out.println("Jackson|JSON|stream|read: " + (System.currentTimeMillis() - time));
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment