package org.libj.xquery.performance;

import org.junit.Test;
import org.libj.xquery.XQuery;
import org.libj.xquery.xml.XML;
import org.libj.xquery.xml.str.StringXML;

import static org.junit.Assert.assertTrue;
import static org.libj.xquery.Asserts.*;
import static org.libj.xquery.Asserts.number;
import static org.libj.xquery.Compile.compile;

public class TestXPathFilterPerformance {
    @Test
    public void testFilter() {
        String xquery = "let $fv1 := $arg\n" +
                        "let $x := $fv1/ID\n" +
                        "where $x = '2'\n" +
                        "return $fv1";
        XML x = new StringXML("<x><ID>1</ID></x>");
        assertRepeatedEvalMillisVar(xquery, 1000*1000*3, 1000, "arg", x);
    }

    public static void assertRepeatedEvalMillisVar(String script, int n, int targetMillis, String varName, Object var) {
        XQuery q = compile(script, varName);
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            q.eval(var);
        }
        long end = System.currentTimeMillis();
        long executionMillis = end - start;
        logTime(script, executionMillis, n);
        logSpeed(n, executionMillis);
        assertTrue(String.format("Timeout: %d > %d (loop %s) for script: " + breakLine(script), executionMillis, targetMillis, number(n)), executionMillis <= targetMillis);
    }
}

@@ -117,16 +117,67 @@
 ml.XML;%0A
+import org.libj.xquery.xml.str.StringNamespaceXML;%0A
 import o
@@ -210,16 +210,16 @@
 ingXML;%0A
-
 %0Aimport 
@@ -461,22 +461,25 @@
 oid test
-Filter
+StringXML
 () %7B%0A   
@@ -758,14 +758,396 @@
 1000
-*
+ * 
 1000
-*
+ * 3, 1000, %22arg%22, x);%0A    %7D%0A%0A    @Test%0A    public void testStringNamespaceXML() %7B%0A        String xquery = %22let $fv1 := $arg%5Cn%22 +%0A                        %22let $x := $fv1/ID%5Cn%22 +%0A                        %22where $x = '2'%5Cn%22 +%0A                        %22return $fv1%22;%0A        XML x = new StringNamespaceXML(%22%3Cx%3E%3CID%3E1%3C/ID%3E%3C/x%3E%22);%0A        assertRepeatedEvalMillisVar(xquery, 1000 * 1000 * 
 3, 1

test StringNamespaceXML xpath filter