<commit_before>/**
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  <NAME>
 * <p/>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package jlibs.xml.sax.sniff;

import jlibs.xml.DefaultNamespaceContext;
import jlibs.xml.dom.DOMUtil;
import jlibs.xml.sax.helpers.SAXHandler;
import jlibs.xml.sax.helpers.NamespaceSupportReader;
import org.w3c.dom.*;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.CharArrayWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Enumeration;

/**
 * @author <NAME>
 */
public class XPathPerformanceTest{
    public List<List<String>> translateJDKResults(TestCase testCase){
        List<List<String>> results = new ArrayList<List<String>>();
        int test = 0;
        for(NodeList nodeSet: testCase.jdkResult){
            testCase.hasAttributes.add(false);
            List<String> result = new ArrayList<String>();
            for(int i=0; i<nodeSet.getLength(); i++){
                Node node = nodeSet.item(i);
                if(node instanceof Attr){
                    result.add(node.getNodeValue());
                    testCase.hasAttributes.set(test, true);
                }else if(node instanceof Element){
                    StringBuilder buff = new StringBuilder();
                    while(!(node instanceof Document)){
                        String prefix = testCase.nsContext.getPrefix(node.getNamespaceURI());
                        buff.insert(0, "["+DOMUtil.getPosition((Element)node)+"]");
                        buff.insert(0, prefix.length()==0 ? node.getLocalName() : prefix+':'+node.getLocalName());
                        buff.insert(0, '/');
                        node = node.getParentNode();
                    }
                    result.add(buff.toString());
                }else if(node instanceof Text)
                    result.add(node.getNodeValue());
            }
            results.add(result);
            test++;
        }
        return results;
    }

    private List<TestCase> testCases = new ArrayList<TestCase>();

    public void readTestCases(String configFile) throws Exception{
        new NamespaceSupportReader(true).parse(configFile, new SAXHandler(){
            TestCase testCase;
            CharArrayWriter contents = new CharArrayWriter();

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
                if(localName.equals("testcase")){
                    testCases.add(testCase = new TestCase());
                    Enumeration<String> enumer = nsSupport.getPrefixes();
                    while(enumer.hasMoreElements()){
                        String prefix = enumer.nextElement();
                        testCase.nsContext.declarePrefix(prefix, nsSupport.getURI(prefix));
                    }
                    if(nsSupport.getURI("")!=null)
                        testCase.nsContext.declarePrefix("", nsSupport.getURI(""));
                }
                contents.reset();
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException{
                contents.write(ch, start, length);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException{
                if(localName.equals("file"))
                    testCase.file = contents.toString().trim();
                else if(localName.equals("xpath"))
                    testCase.xpaths.add(contents.toString().trim());

            }
        });
    }

    public void run(String configFile) throws Exception{
        readTestCases(configFile);

        long time = System.nanoTime();
        for(TestCase testCase: testCases)
            testCase.dogResult = testCase.usingXMLDog();
        long dogTime = System.nanoTime() - time;

        time = System.nanoTime();
        for(TestCase testCase: testCases)
            testCase.jdkResult = testCase.usingJDK();
        long jdkTime = System.nanoTime() - time;

        int total= 0;
        int failed = 0;
        for(TestCase testCase: testCases){
            total += testCase.jdkResult.size();
            List<List<String>> jdkResults = translateJDKResults(testCase);

            PrintStream stream = System.out;

            for(int i=0; i<testCase.xpaths.size(); i++){
                List<String> jdkResult = jdkResults.get(i);
                List<String> dogResult = testCase.dogResult.get(i);

                if(testCase.hasAttributes.get(i)){
                    Collections.sort(jdkResult);
                    Collections.sort(dogResult);
                    System.out.println("sorting results...");
                }
<commit_msg># do println after each engine finishes evaluation so that we can know who is responible of OOM if any occurs<commit_after>/**
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  <NAME>
 * <p/>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package jlibs.xml.sax.sniff;

import jlibs.xml.DefaultNamespaceContext;
import jlibs.xml.dom.DOMUtil;
import jlibs.xml.sax.helpers.SAXHandler;
import jlibs.xml.sax.helpers.NamespaceSupportReader;
import org.w3c.dom.*;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.CharArrayWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Enumeration;

/**
 * @author <NAME>
 */
public class XPathPerformanceTest{
    public List<List<String>> translateJDKResults(TestCase testCase){
        List<List<String>> results = new ArrayList<List<String>>();
        int test = 0;
        for(NodeList nodeSet: testCase.jdkResult){
            testCase.hasAttributes.add(false);
            List<String> result = new ArrayList<String>();
            for(int i=0; i<nodeSet.getLength(); i++){
                Node node = nodeSet.item(i);
                if(node instanceof Attr){
                    result.add(node.getNodeValue());
                    testCase.hasAttributes.set(test, true);
                }else if(node instanceof Element){
                    StringBuilder buff = new StringBuilder();
                    while(!(node instanceof Document)){
                        String prefix = testCase.nsContext.getPrefix(node.getNamespaceURI());
                        buff.insert(0, "["+DOMUtil.getPosition((Element)node)+"]");
                        buff.insert(0, prefix.length()==0 ? node.getLocalName() : prefix+':'+node.getLocalName());
                        buff.insert(0, '/');
                        node = node.getParentNode();
                    }
                    result.add(buff.toString());
                }else if(node instanceof Text)
                    result.add(node.getNodeValue());
            }
            results.add(result);
            test++;
        }
        return results;
    }

    private List<TestCase> testCases = new ArrayList<TestCase>();

    public void readTestCases(String configFile) throws Exception{
        new NamespaceSupportReader(true).parse(configFile, new SAXHandler(){
            TestCase testCase;
            CharArrayWriter contents = new CharArrayWriter();

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
                if(localName.equals("testcase")){
                    testCases.add(testCase = new TestCase());
                    Enumeration<String> enumer = nsSupport.getPrefixes();
                    while(enumer.hasMoreElements()){
                        String prefix = enumer.nextElement();
                        testCase.nsContext.declarePrefix(prefix, nsSupport.getURI(prefix));
                    }
                    if(nsSupport.getURI("")!=null)
                        testCase.nsContext.declarePrefix("", nsSupport.getURI(""));
                }
                contents.reset();
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException{
                contents.write(ch, start, length);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException{
                if(localName.equals("file"))
                    testCase.file = contents.toString().trim();
                else if(localName.equals("xpath"))
                    testCase.xpaths.add(contents.toString().trim());

            }
        });
    }

    public void run(String configFile) throws Exception{
        readTestCases(configFile);

        long time = System.nanoTime();
        for(TestCase testCase: testCases)
            testCase.dogResult = testCase.usingXMLDog();
        long dogTime = System.nanoTime() - time;
        System.out.println("XMLDog finished evaluation");
        
        time = System.nanoTime();
        for(TestCase testCase: testCases)
            testCase.jdkResult = testCase.usingJDK();
        long jdkTime = System.nanoTime() - time;
        System.out.println("Xalan finished");

        int total= 0;
        int failed = 0;
        for(TestCase testCase: testCases){
            total += testCase.jdkResult.size();
            List<List<String>> jdkResults = translateJDKResults(testCase);

            PrintStream stream = System.out;

            for(int i=0; i<testCase.xpaths.size(); i++){
                List<String> jdkResult = jdkResults.get(i);
                List<String> dogResult = testCase.dogResult.get(i);

                if(testCase.hasAttributes.get(i)){
                    Collections.sort(jdkResult);
                    Collections.sort(dogResult);
                    System.out.println("sorting results...");
                }

{'fraction_non_alphanumeric': 0.10646017699115044, 'fraction_numerical': 0.002831858407079646, 'mean_word_length': 2.1391666666666667, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4226439', 'n_tokens_mistral': 3013, 'n_tokens_neox': 2871, 'n_words': 719}