package org.libj.xquery.performance;

import org.junit.Test;
import org.libj.xquery.Callback;
import org.libj.xquery.XQuery;

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 TestCallbackPerformance {
    @Test
    public void testTrivial() {
        assertRepeatedEvalMillisCallback(" 1 ", 1000*1000*1000, 1000);
        assertRepeatedEvalMillisCallback(" $w ", 1000*1000*1000, 1000);
        assertRepeatedEvalMillisCallback(" let $x := 1 return $x ", 1000*1000*100, 1000);
        assertRepeatedEvalMillisCallback(" let $x := $w return $x ", 1000*1000*10, 1000);
        assertRepeatedEvalMillisCallback(" for $x in $w return $x ", 1000*1000*10, 1000);
    }

    public static void assertRepeatedEvalMillisCallback(String script, int n, int targetMillis) {
        Callback callback = new Callback() {
            public void call(Object result) {
                // do nothing
            }
        };
        XQuery q = compile(script, "w");
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            q.eval(callback, 1);
        }
        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);
    }
}

@@ -494,38 +494,42 @@
 ack(%22 $w %22, 1000
-*
+ * 
 1000
-*
+ * 
 1000, 1000);%0A   
@@ -517,32 +517,86 @@
  * 1000, 1000);%0A
+    %7D%0A%0A    @Test%0A    public void testLetNonObject() %7B%0A
         assertRe
@@ -660,16 +660,17 @@
 1000*100
+0
 , 1000);
@@ -662,32 +662,83 @@
 00*1000, 1000);%0A
+    %7D%0A%0A    @Test%0A    public void testLetObject() %7B%0A
         assertRe
@@ -786,38 +786,42 @@
 eturn $x %22, 1000
-*
+ * 
 1000
-*
+ * 
 10, 1000);%0A     
@@ -807,32 +807,77 @@
 00 * 10, 1000);%0A
+    %7D%0A%0A    @Test%0A    public void testFor() %7B%0A
         assertRe
@@ -962,21 +962,22 @@
 %7D%0A%0A    p
-ublic
+rivate
  static 

update performance test