From f8a95baf80e419d45f52143f231a3dfe42832627 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 16 May 2016 20:08:48 +0300 Subject: [PATCH] performance: ignore whitespace during spell checking, fix test to perform actual inspections instead of skipping them altogether --- .../tokenizer/SpellcheckingStrategy.java | 3 + .../SpellcheckerInspectionTestCase.java | 4 +- .../SpellcheckerPerformanceTest.java | 58 ++++++++++++++++--- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/spellchecker/src/com/intellij/spellchecker/tokenizer/SpellcheckingStrategy.java b/spellchecker/src/com/intellij/spellchecker/tokenizer/SpellcheckingStrategy.java index 378928bc1bb3..0db8217ae1fc 100644 --- a/spellchecker/src/com/intellij/spellchecker/tokenizer/SpellcheckingStrategy.java +++ b/spellchecker/src/com/intellij/spellchecker/tokenizer/SpellcheckingStrategy.java @@ -52,6 +52,9 @@ public class SpellcheckingStrategy { @NotNull public Tokenizer getTokenizer(PsiElement element) { + if (element instanceof PsiWhiteSpace) { + return EMPTY_TOKENIZER; + } if (element instanceof PsiLanguageInjectionHost && InjectedLanguageUtil.hasInjections((PsiLanguageInjectionHost)element)) { return EMPTY_TOKENIZER; } diff --git a/spellchecker/testSrc/com/intellij/spellchecker/inspection/SpellcheckerInspectionTestCase.java b/spellchecker/testSrc/com/intellij/spellchecker/inspection/SpellcheckerInspectionTestCase.java index 8910cb259456..0203a80ea17c 100644 --- a/spellchecker/testSrc/com/intellij/spellchecker/inspection/SpellcheckerInspectionTestCase.java +++ b/spellchecker/testSrc/com/intellij/spellchecker/inspection/SpellcheckerInspectionTestCase.java @@ -25,7 +25,7 @@ public abstract class SpellcheckerInspectionTestCase extends LightPlatformCodeIn return true; } - public static String getSpellcheckerTestDataPath() { + static String getSpellcheckerTestDataPath() { return "/spellchecker/testData/"; } @@ -34,7 +34,7 @@ public abstract class SpellcheckerInspectionTestCase extends LightPlatformCodeIn myFixture.testHighlighting(false, false, true, file); } - public static LocalInspectionTool[] getInspectionTools() { + static LocalInspectionTool[] getInspectionTools() { return new LocalInspectionTool[]{new SpellCheckingInspection()}; } } diff --git a/spellchecker/testSrc/com/intellij/spellchecker/inspection/SpellcheckerPerformanceTest.java b/spellchecker/testSrc/com/intellij/spellchecker/inspection/SpellcheckerPerformanceTest.java index 079869b572e7..d1f23783b70e 100644 --- a/spellchecker/testSrc/com/intellij/spellchecker/inspection/SpellcheckerPerformanceTest.java +++ b/spellchecker/testSrc/com/intellij/spellchecker/inspection/SpellcheckerPerformanceTest.java @@ -15,10 +15,19 @@ */ package com.intellij.spellchecker.inspection; +import com.intellij.codeHighlighting.Pass; +import com.intellij.codeHighlighting.TextEditorHighlightingPass; +import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; +import com.intellij.codeInsight.daemon.impl.HighlightInfo; +import com.intellij.codeInsight.daemon.impl.TextEditorHighlightingPassRegistrarEx; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.testFramework.PlatformTestUtil; -import com.intellij.util.ThrowableRunnable; +import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl; +import com.intellij.util.ArrayUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.List; /** * @author peter @@ -46,14 +55,47 @@ public class SpellcheckerPerformanceTest extends SpellcheckerInspectionTestCase myFixture.enableInspections(getInspectionTools()); start = System.currentTimeMillis(); - assertSize(typoCount, myFixture.doHighlighting()); + assertSize(typoCount, runLocalInspections()); System.out.println("warm-up took " + (System.currentTimeMillis() - start) + " ms"); - PlatformTestUtil.startPerformanceTest("many typos highlighting", 1000, new ThrowableRunnable() { - @Override - public void run() { - assertSize(typoCount, myFixture.doHighlighting()); - } - }).cpuBound().useLegacyScaling().assertTiming(); + PlatformTestUtil.startPerformanceTest("many typos highlighting", 10000, () -> { + DaemonCodeAnalyzer.getInstance(getProject()).restart(); + assertSize(typoCount, runLocalInspections()); + }).cpuBound().assertTiming(); + } + + public void testManyWhitespaces() { + final int count = 100000; + String text = StringUtil.repeat("//\n \t \t \n \n", count); + + long start = System.currentTimeMillis(); + VirtualFile file = myFixture.addFileToProject("foo.java", text).getVirtualFile(); + System.out.println("creation took " + (System.currentTimeMillis() - start) + " ms"); + + start = System.currentTimeMillis(); + myFixture.configureFromExistingVirtualFile(file); + System.out.println("configure took " + (System.currentTimeMillis() - start) + " ms"); + + start = System.currentTimeMillis(); + List infos = runLocalInspections(); + assertEmpty(infos); + System.out.println("warm-up took " + (System.currentTimeMillis() - start) + " ms"); + + PlatformTestUtil.startPerformanceTest("many whitespaces highlighting", 4000, () -> { + DaemonCodeAnalyzer.getInstance(getProject()).restart(); + assertEmpty(runLocalInspections()); + }).cpuBound().assertTiming(); + } + + @NotNull + private List runLocalInspections() { + myFixture.enableInspections(getInspectionTools()); + TextEditorHighlightingPassRegistrarEx passRegistrar = TextEditorHighlightingPassRegistrarEx.getInstanceEx(getProject()); + List passes = passRegistrar.instantiatePasses(myFixture.getFile(), myFixture.getEditor(), new int[0]); + int[] toIgnore = passes.stream().mapToInt(TextEditorHighlightingPass::getId).toArray(); + int i = ArrayUtil.find(toIgnore, Pass.LOCAL_INSPECTIONS); + toIgnore[i] = 0; // ignore everything except Pass.LOCAL_INSPECTIONS + + return CodeInsightTestFixtureImpl.instantiateAndRun(myFixture.getFile(), myFixture.getEditor(), toIgnore, false); } }