/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * 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.intellij.psi.codeStyle.autodetect;

import com.intellij.JavaTestUtil;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.autodetect.AbstractIndentAutoDetectionTest;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.testFramework.TeamCityLogger;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;

import static com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions;

public class JavaAutoDetectIndentPerformanceTest extends AbstractIndentAutoDetectionTest {

  @NotNull
  @Override
  protected String getTestDataPath() {
    return JavaTestUtil.getJavaTestDataPath() + "/psi/autodetect/";
  }

  @NotNull
  @Override
  protected String getFileNameWithExtension() {
    return getTestName(true) + ".java";
  }

  public void testBigFile() {
    Ref<IndentOptions> ref = checkPerformanceRelativelyToFileRead();
    //to ensure if worked as expected
    Assert.assertEquals("Detect indent mismatch", 2, ref.get().INDENT_SIZE);

    PlatformTestUtil
      .startPerformanceTest("Detecting indent on hot file", 30, AbstractIndentAutoDetectionTest::detectIndentOptions)
      .cpuBound()
      .assertTiming();
  }

  private static long trackTime(Runnable runnable) {
    long startTime = System.currentTimeMillis();
    runnable.run();
    return System.currentTimeMillis() - startTime;
  }
  
  private Ref<IndentOptions> checkPerformanceRelativelyToFileRead() {
    Ref<IndentOptions> ref = Ref.create();
    long fileLoadTime = trackTime(() -> configureByFile(getFileNameWithExtension()));
    
    long detectingTime = trackTime(() -> ref.set(detectIndentOptions()));
    double ratio = (double)detectingTime / fileLoadTime;
    if (ratio > 0.2) {
      TeamCityLogger.error("Detecting indent have taken too much time proportionally to file read time " + ratio);
    } else {
      String msg = "Detecting indent relatively to file read " + ratio;
      TeamCityLogger.info(msg);
      System.out.println(msg);
    }
    
    return ref;
  }
  
}
@@ -1346,29 +1346,16 @@
 urn 
-getTestName(true) + %22
+%22bigFile
 .jav
@@ -1388,632 +1388,16 @@
 tBig
+Cold
 File
-() %7B%0A    Ref%3CIndentOptions%3E ref = checkPerformanceRelativelyToFileRead();%0A    //to ensure if worked as expected%0A    Assert.assertEquals(%22Detect indent mismatch%22, 2, ref.get().INDENT_SIZE);%0A%0A    PlatformTestUtil%0A      .startPerformanceTest(%22Detecting indent on hot file%22, 30, AbstractIndentAutoDetectionTest::detectIndentOptions)%0A      .cpuBound()%0A      .assertTiming();%0A  %7D%0A%0A  private static long trackTime(Runnable runnable) %7B%0A    long startTime = System.currentTimeMillis();%0A    runnable.run();%0A    return System.currentTimeMillis() - startTime;%0A  %7D%0A  %0A  private Ref%3CIndentOptions%3E checkPerformanceRelativelyToFileRead
 () %7B
@@ -1526,20 +1526,16 @@
 on()));%0A
-    
 %0A    lon
@@ -1963,24 +1963,631 @@
     
-return ref
+//to ensure it worked as expected%0A    Assert.assertEquals(%22Detect indent mismatch%22, 2, ref.get().INDENT_SIZE);%0A  %7D%0A  %0A  public void testBigHotFile() %7B%0A    configureByFile(getFileNameWithExtension());%0A    AbstractIndentAutoDetectionTest.detectIndentOptions();%0A    %0A    PlatformTestUtil%0A      .startPerformanceTest(%22Detecting indent on hot file%22, 30, AbstractIndentAutoDetectionTest::detectIndentOptions)%0A      .cpuBound()%0A      .assertTiming()
 ;%0A  %7D%0A
+%0A
   
+private static long trackTime(Runnable runnable) %7B%0A    long startTime = System.currentTimeMillis();%0A    runnable.run();%0A    return System.currentTimeMillis() - startTime;%0A  %7D
 %0A%7D

Indent detector performance test: test separately hot and cold files