001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.load.xsem;
018:
019: import java.io.InputStreamReader;
020: import java.io.Reader;
021:
022: import org.compass.core.config.CompassEnvironment;
023: import org.compass.core.config.CompassSettings;
024: import org.compass.core.converter.mapping.xsem.XmlContentMappingConverter;
025: import org.compass.core.converter.xsem.XmlContentConverter;
026: import org.compass.core.xml.XmlObject;
027: import org.compass.core.xml.XmlXPathExpression;
028: import org.compass.core.xml.dom4j.converter.SAXReaderXmlContentConverter;
029: import org.compass.core.xml.dom4j.converter.XPP3ReaderXmlContentConverter;
030: import org.compass.core.xml.dom4j.converter.XPPReaderXmlContentConverter;
031: import org.compass.core.xml.javax.converter.NodeXmlContentConverter;
032:
033: /**
034: * @author kimchy
035: */
036: public class XmlContentConverterLoadTester {
037:
038: public static void main(String[] args) throws Exception {
039:
040: XmlContentConverter[] converters = new XmlContentConverter[] {
041: new SAXReaderXmlContentConverter(),
042: new XPPReaderXmlContentConverter(),
043: new XPP3ReaderXmlContentConverter(),
044: new NodeXmlContentConverter() };
045:
046: for (int i = 0; i < converters.length; i++) {
047: System.gc();
048: CompassSettings settings = new CompassSettings();
049: settings.setSetting(
050: CompassEnvironment.Converter.XmlContent.TYPE,
051: converters[i].getClass().getName());
052: settings
053: .setSetting(
054: CompassEnvironment.Converter.XmlContent.WRAPPER,
055: CompassEnvironment.Converter.XmlContent.WRAPPER_PROTOTYPE);
056: XmlContentMappingConverter xmlContentMappingConverter = new XmlContentMappingConverter();
057: xmlContentMappingConverter.configure(settings);
058: testConverter(xmlContentMappingConverter
059: .getXmlContentConverter(), 500);
060: }
061: }
062:
063: private static void testConverter(XmlContentConverter converter,
064: int iterNum) throws Exception {
065: long now = System.nanoTime();
066: for (int i = 0; i < iterNum; i++) {
067: converter.fromXml("a", readData("data2"));
068: }
069: long total = System.nanoTime() - now;
070: System.out.println("[" + converter
071: + "] Average fromXml nano time: " + (total / iterNum));
072:
073: XmlObject xmlObject = converter.fromXml("a", readData("data2"));
074:
075: now = System.nanoTime();
076: for (int i = 0; i < iterNum; i++) {
077: converter.toXml(xmlObject);
078: }
079: total = System.nanoTime() - now;
080: System.out.println("[" + converter
081: + "] Average toXml nano time: " + (total / iterNum));
082:
083: XmlXPathExpression expr = xmlObject
084: .compile("/xml-fragment/data/id");
085: now = System.nanoTime();
086: for (int i = 0; i < iterNum; i++) {
087: expr.select(xmlObject);
088: }
089: total = System.nanoTime() - now;
090: System.out.println("[" + converter
091: + "] Average xpath nano time: " + (total / iterNum));
092: }
093:
094: private static Reader readData(String path) {
095: path = "org/compass/core/test/xml/" + path + ".xml";
096: return new InputStreamReader(Thread.currentThread()
097: .getContextClassLoader().getResourceAsStream(path));
098: }
099:
100: }
|