package org.mvel.tests.perftests;

import junit.framework.TestCase;
import org.mvel.MVEL;
import org.mvel.integration.impl.MapVariableResolverFactory;
import org.mvel.tests.core.CoreConfidenceTests;
import org.mvel.tests.core.res.Bar;
import org.mvel.tests.core.res.Base;
import org.mvel.tests.core.res.Foo;
import org.mvel.util.ParseTools;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class CompiledPerformanceTests {
    private static final int ITERATIONS = 100;

    protected Foo foo = new Foo();
    protected Map<String, Object> map = new HashMap<String, Object>();
    protected Base base = new Base();

    public CompiledPerformanceTests() {
        foo.setBar(new Bar());
        map.put("foo", foo);
        map.put("a", null);
        map.put("b", null);
        map.put("c", "cat");
        map.put("BWAH", "");

        map.put("misc", new CoreConfidenceTests.MiscTestClass());

        map.put("pi", "3.14");
        map.put("hour", "60");
        map.put("zero", 0);
    }

//    public void testToListBenchmark() {
//        String text = "misc.toList(foo.bar.name, 'hello', 42, ['key1' : 'value1', c : [ foo.bar.age, 'car', 42 ]], [42, [c : 'value1']] )";
//
//        MapVariableResolverFactory variableTable = new MapVariableResolverFactory(map);
//
//        Serializable compiled = MVEL.compileExpression(text);
//
//        for (int i = 0; i < ITERATIONS; i++) {
//            executeExpression(compiled, variableTable);
//        }
//
//        assertEquals(ITERATIONS, ((CoreConfidenceTests.MiscTestClass) map.get("misc")).getExec());
//    }
//
//
//    public void testToListBenchmark2() {
//        testToListBenchmark();
//    }
//
//    public void testToListBenchmark3() {
//        testToListBenchmark();
//    }

    public void testQuickSort() throws IOException {
        Serializable s = MVEL.compileExpression(new String(ParseTools.loadFromFile(new File("samples/scripts/quicksort.mvel"))));

        MapVariableResolverFactory mvrf = new MapVariableResolverFactory(new HashMap());

        for (int i = 0; i < 1000000; i++) {
            MVEL.executeExpression(s, mvrf);
        }

//        for (int x = 0; x < 4; x++) {
//            Serializable s = MVEL.compileSetExpression("foo.bar.name");
//            long time = System.currentTimeMillis();
//
//            for (int i = 0; i < ITERATIONS; i++) {
//                MVEL.executeSetExpression(s, map, "foobie");
//            }
//
//            System.out.println("SET PERFORMANCE: " + (System.currentTimeMillis() - time));
//
//            time = System.currentTimeMillis();
//
//            s = MVEL.compileExpression("foo.bar.name");
//
//            for (int i = 0; i < ITERATIONS; i++) {
//                MVEL.executeExpression(s, map);
//            }
//
//            System.out.println("GET PERFORMANCE: " + (System.currentTimeMillis() - time));
//
//        }
    }

    public void testQuickSort2() throws IOException {
        testQuickSort();
    }

    public void testQuickSort3() throws IOException {
        testQuickSort();
    }


}

@@ -34,42 +34,8 @@
 %0D%0A%0D%0A
-import junit.framework.TestCase;%0D%0A
 impo
@@ -53,16 +53,16 @@
 .MVEL;%0D%0A
+
 import o
@@ -490,58 +490,8 @@
  %7B%0D%0A
-    private static final int ITERATIONS = 100;%0D%0A%0D%0A
     
@@ -1033,785 +1033,8 @@
 %0D%0A%0D%0A
-//    public void testToListBenchmark() %7B%0D%0A//        String text = %22misc.toList(foo.bar.name, 'hello', 42, %5B'key1' : 'value1', c : %5B foo.bar.age, 'car', 42 %5D%5D, %5B42, %5Bc : 'value1'%5D%5D )%22;%0D%0A//%0D%0A//        MapVariableResolverFactory variableTable = new MapVariableResolverFactory(map);%0D%0A//%0D%0A//        Serializable compiled = MVEL.compileExpression(text);%0D%0A//%0D%0A//        for (int i = 0; i %3C ITERATIONS; i++) %7B%0D%0A//            executeExpression(compiled, variableTable);%0D%0A//        %7D%0D%0A//%0D%0A//        assertEquals(ITERATIONS, ((CoreConfidenceTests.MiscTestClass) map.get(%22misc%22)).getExec());%0D%0A//    %7D%0D%0A//%0D%0A//%0D%0A//    public void testToListBenchmark2() %7B%0D%0A//        testToListBenchmark();%0D%0A//    %7D%0D%0A//%0D%0A//    public void testToListBenchmark3() %7B%0D%0A//        testToListBenchmark();%0D%0A//    %7D%0D%0A
 %0D%0A  

fix performance tests