/*
 * Copyright (c) Joachim Ansorg, mail@ansorg-it.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ansorgit.plugins.bash.editor;

import com.ansorgit.plugins.bash.LightBashCodeInsightFixtureTestCase;
import com.ansorgit.plugins.bash.editor.highlighting.BashTestInspections;
import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.intellij.codeInspection.LocalInspectionEP;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.ThrowableRunnable;

import java.util.ArrayList;

/**
 * @author jansorg
 */
public class BashPerformanceTest extends LightBashCodeInsightFixtureTestCase {
    public void testEditorPerformance() {
        doTest(2);
    }

    // editorPerformanceLarge: 14078% longer. Expected: 407ms. Actual: 57705ms (57.7s). Timings: CPU=47 (23% of the etalon), I/O=13 (13% of the etalon), total=60 (13% of the etalon) 12 cores.
    public void testEditorPerformanceLarge() {
        //doTest(100);
    }

    private void doTest(final int iterations) {
        myFixture.configureByFile("functions_issue96.bash");

        long start = System.currentTimeMillis();
        PlatformTestUtil.startPerformanceTest(getTestName(true), iterations * 500, new ThrowableRunnable() {
            @Override
            public void run() throws Throwable {
                for (int i = 0; i < iterations; i++) {
                    long innerStart = System.currentTimeMillis();
                    Editor editor = myFixture.getEditor();
                    editor.getCaretModel().moveToOffset(editor.getDocument().getTextLength());

                    myFixture.type("\n");
                    myFixture.type("echo \"hello world\"\n");
                    myFixture.type("pri");
                    myFixture.complete(CompletionType.BASIC);

                    System.out.println("Cycle duration: " + (System.currentTimeMillis() - innerStart));
                }
            }
        }).usesAllCPUCores().attempts(1).assertTiming();

        System.out.println("Complete duration: " + (System.currentTimeMillis() - start));
    }

    @Override
    protected String getBasePath() {
        return "editor/highlighting/syntaxHighlighter/performance";
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        enableInspections();
    }

    private void enableInspections() {
        Class[] inspectionClasses = new BashTestInspections().getInspectionClasses();

        ArrayList<InspectionProfileEntry> inspections = new ArrayList<InspectionProfileEntry>();

        LocalInspectionEP[] extensions = Extensions.getExtensions(LocalInspectionEP.LOCAL_INSPECTION);
        for (LocalInspectionEP extension : extensions) {
            for (Class inspectionClass : inspectionClasses) {
                if (extension.implementationClass.equals(inspectionClass.getCanonicalName())) {
                    extension.enabledByDefault = true;

                    inspections.add(extension.instantiateTool());
                }
            }
        }

        myFixture.enableInspections(inspections.toArray(new InspectionProfileEntry[inspections.size()]));
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();

        if (myFixture != null) {
            myFixture.tearDown();
        }
    }

}

@@ -1346,17 +1346,18 @@
  doTest(
-2
+10
 );%0A    %7D
@@ -1605,18 +1605,16 @@
         
-//
 doTest(1
@@ -1733,16 +1733,45 @@
 .bash%22);
+%0A        enableInspections();
 %0A%0A      
@@ -1895,107 +1895,19 @@
 s * 
-5
+10
 00, 
-new ThrowableRunnable() %7B%0A            @Override%0A            public void run() throws Throwable
+() -%3E
  %7B%0A 
@@ -1913,28 +1913,24 @@
             
- 
-   
 for (int i =
@@ -1952,28 +1952,24 @@
 ons; i++) %7B%0A
-    
             
@@ -2014,28 +2014,24 @@
 meMillis();%0A
-    
             
@@ -2089,20 +2089,16 @@
         
-    
 editor.g
@@ -2173,36 +2173,32 @@
                 
-    
 myFixture.type(%22
@@ -2211,36 +2211,32 @@
                 
-   
- 
 myFixture.type(%22
@@ -2257,28 +2257,24 @@
 orld%5C%22%5Cn%22);%0A
-    
             
@@ -2308,36 +2308,32 @@
                 
-    
 myFixture.comple
@@ -2371,28 +2371,24 @@
             
- 
-   
 System.out.p
@@ -2475,54 +2475,29 @@
     
-    %7D%0A            %7D%0A        %7D).usesAllCPUCores
+%7D%0A        %7D).cpuBound
 ().a
@@ -2504,17 +2504,17 @@
 ttempts(
-1
+3
 ).assert

fix test