Hi,
I have a J2EE Web services application which uses DOM for XML construction and
Apache SOAP API for constructing SOAP envelope out of this Document object.
After looking into the promise offered by AXIOM API over performance, I rewrote
the Document construction and SOAP envelope construction code. Now instead of
constructing a Document object, i constuct an OMElement object and use AXIOM
API to construct the SOAP envelope instead of using Apache SOAP API.
I've copied the code below.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.soap.Body;
import org.apache.soap.Envelope;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class SOAPEnvelopeCreator {
static String fileName = "D:\\Gijish\\test2.xml";
static long apacheSOAPDocConstructionTime = 0;
static long apacheSOAPEnvelopeConstructionTime = 0;
static long apacheAXIOMConstructionTime = 0;
static long apacheAXISEnvelopeConstructionTime = 0;
private static final int iterations = 10;
private static void createEnvelopeUsingApacheSOAP(int count) {
long docStartTime = System.currentTimeMillis();
System.out.println("START TIME of building Document Object from
XML " + count + " ---> " + docStartTime);
try {
Document xmlDoc = null;
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
FileReader reader1 = new FileReader(fileName);
xmlDoc = builder.parse(new InputSource(reader1));
long docEndTime = System.currentTimeMillis();
apacheSOAPDocConstructionTime =
apacheSOAPDocConstructionTime + (docEndTime - docStartTime);
System.out.println("END TIME of building Document
Object from XML " + count + " <--- " + docEndTime);
long envelopeStartTime = System.currentTimeMillis();
System.out.println("START TIME of constructing SOAP
Envelope " + count + " ---> " + envelopeStartTime);
Envelope envelope = new Envelope();
Vector bodyElements = new Vector();
bodyElements.add(xmlDoc.getDocumentElement());
Body body = new Body();
body.setBodyEntries(bodyElements);
envelope.setBody(body);
long envelopeEndTime = System.currentTimeMillis();
apacheSOAPEnvelopeConstructionTime =
apacheSOAPEnvelopeConstructionTime + (envelopeEndTime - envelopeStartTime);
System.out.println("END TIME of constructing SOAP
Envelope " + count + " <--- " + envelopeEndTime);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void createEnvelopeUsingApacheAxis(int count) {
long docStartTime = System.currentTimeMillis();
System.out.println("START TIME of building AXIOM Object from
XML " + count + " ---> " + docStartTime);
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = null;
reader = factory.createXMLStreamReader(new
FileInputStream(fileName));
StAXOMBuilder parser = new StAXOMBuilder(reader);
OMElement document = parser.getDocumentElement();
long docEndTime = System.currentTimeMillis();
apacheAXIOMConstructionTime =
apacheAXIOMConstructionTime + (docEndTime - docStartTime);
System.out.println("END TIME of building AXIOM Object
from XML " + count + " <--- " + docEndTime);
long envelopeStartTime = System.currentTimeMillis();
System.out.println("START TIME of constructing SOAP
Envelope " + count + " ---> " + envelopeStartTime);
SOAPFactory soapFactory =
OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope soapEnvelope =
soapFactory.getDefaultEnvelope();
soapEnvelope.getBody().addChild(document);
long envelopeEndTime = System.currentTimeMillis();
apacheAXISEnvelopeConstructionTime =
apacheAXISEnvelopeConstructionTime + (envelopeEndTime - envelopeStartTime);
System.out.println("END TIME of constructing SOAP
Envelope " + count + " <--- " + envelopeEndTime);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("######## START OF " + iterations + " CALLS
- APACHE SOAP " + "#######");
long apacheStartTime = System.currentTimeMillis();
for( int i = 1; i <= iterations; i++) {
SOAPEnvelopeCreator.createEnvelopeUsingApacheSOAP(i);
}
long apacheEndTime = System.currentTimeMillis();
System.out.println("######## END OF " + iterations + " CALLS -
APACHE SOAP " + "#######");
System.out.println();
System.out.println();
System.out.println("######## START OF " + iterations + " CALLS
- APACHE AXIS " + "#######");
long axisStartTime = System.currentTimeMillis();
for( int i = 1; i <= iterations; i++) {
SOAPEnvelopeCreator.createEnvelopeUsingApacheAxis(i);
}
long axisEndTime = System.currentTimeMillis();
System.out.println("######## END OF " + iterations + " CALLS -
APACHE AXIS " + "#######");
System.err.println("Average time taken for Document
construction in Apache SOAP is " + (apacheSOAPDocConstructionTime / iterations)
+ " ms");
System.err.println("Average time taken for Document
construction using Apache AXIS is " + (apacheAXIOMConstructionTime /
iterations) + " ms");
System.err.println("Average time taken for Envelope
construction in Apache SOAP is " + (apacheSOAPEnvelopeConstructionTime /
iterations) + " ms");
System.err.println("Average time taken for Envelope
construction in Apache AXIS is " + (apacheAXISEnvelopeConstructionTime /
iterations) + " ms");
System.err.println("Average time taken for in total by Apache
SOAP is " + ((apacheEndTime - apacheStartTime) / iterations) + " ms");
System.err.println("Average time taken for in total by Apache
AXIS is " + ((axisEndTime - axisStartTime) / iterations) + " ms");
}
}
After running this code I get the following output.
Average time taken for Document construction in Apache SOAP is 73 ms
Average time taken for Document construction using Apache AXIS is 18 ms
Average time taken for Envelope construction in Apache SOAP is 1 ms
Average time taken for Envelope construction in Apache AXIS is 44 ms
Average time taken for in total by Apache SOAP is 74 ms
Average time taken for in total by Apache AXIS is 62 ms
From the results I can assume that the time taken for creating an OMElement is
18 ms whereas time taken to for constructing a Document object is 73 ms which
shows that AXIOM gives better performance. My concern here is with respect to
the SOAP Envelope construction part wherein using Apache SOAP API (1 ms) gives
far better results that using AXIS 2 API (44 ms).
The code for constructing SOAP envelope using both Apache SOAP and AXIS2 looks
mostly the same and i couldn't find any major difference in the LOC.
I couldn't find any reason for this vast difference (1 ms vs 44 ms).
Any suggestions on this are most welcome as I am not able to decide on this
one. Also I'm not very sure whether the way I test the performance of these two
API's is correct.
Thanks a lot in advance.
Regards,
Gijish