Project

Profile

Help

Bug #2929 » SaxonTransformerDomTraversalPerformanceTest.java

Thomas Åkesson, 2016-09-08 23:14

 
/**
* Copyright (C) 2009-2013 Simonsoft Nordic AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package se.simonsoft.cms.xmlsource.transform;


import java.io.FileInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Date;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import net.sf.saxon.s9api.Axis;
import net.sf.saxon.s9api.Destination;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmItem;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.XdmNodeKind;
import net.sf.saxon.s9api.XdmSequenceIterator;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;
import net.sf.saxon.s9api.XsltTransformer;

import org.junit.Test;

public class SaxonTransformerDomTraversalPerformanceTest {
private static long numTransformed = 0;

@Test
public void testPerformance() throws SaxonApiException {
InputStream xsl = this.getClass().getClassLoader().getResourceAsStream(
"se/simonsoft/cms/xmlsource/transform/identity.xsl");
InputStream xml = this.getClass().getClassLoader().getResourceAsStream(
"se/simonsoft/cms/xmlsource/handler/test1.xml");
transformElements(xsl, xml);
}
private static void transformElements(InputStream xsl, InputStream xml) throws SaxonApiException {
Source xsltSource = new StreamSource(xsl);
Processor processor = new Processor(false);
DocumentBuilder db = processor.newDocumentBuilder();
XsltCompiler compiler = processor.newXsltCompiler();
XsltExecutable xsltCompiled;
xsltCompiled = compiler.compile(xsltSource);
XsltTransformer transformer = xsltCompiled.load();
XdmNode xdmDoc = db.build(new StreamSource(xml));
StringWriter bulkWriter = new StringWriter();
Destination destination = processor.newSerializer(bulkWriter);
transformer.setDestination(destination);
transformChildren(xdmDoc, transformer);
System.out.println("Transformations: " + numTransformed);
//assertTrue("number of transformations should be high", numTransformed > 10000);
}
private static void transformChildren(XdmNode root, XsltTransformer transformer) throws SaxonApiException {
XdmSequenceIterator it1 = root.axisIterator(Axis.CHILD);
while (it1.hasNext()) {
XdmItem xdmItem = it1.next();
transformer.setInitialContextNode((XdmNode) xdmItem);
transformer.transform();
numTransformed++;
if (((XdmNode) xdmItem).getNodeKind() == XdmNodeKind.ELEMENT) {
XdmNode elem = (XdmNode) xdmItem;
transformChildren(elem, transformer);
}
}
}
public static void main(String[] args) {
Date start = new Date();
try {
FileInputStream xsl = new FileInputStream("/Users/takesson/eclipse/simonsoft/cms-xmlsource/src/test/resources/se/simonsoft/cms/xmlsource/transform/identity.xsl");
// Use some 500kB - 1MB XML file.
FileInputStream xml = new FileInputStream("/Users/takesson/eclipse/simonsoft/cms-xmlsource/src/test/resources/se/simonsoft/cms/xmlsource/handler/test1.xml");
System.out.println("Transforming...");
transformElements(xsl, xml);
} catch (Exception e) {
System.out.println("Failed: " + e.getMessage());
}

Date end = new Date();
System.out.println("Transformations took: " + (end.getTime() - start.getTime()) + " ms.");
}


}
(3-3/5)