diff options
author | Tor Norbye <tnorbye@google.com> | 2013-04-19 13:32:49 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2013-04-19 13:32:49 -0700 |
commit | b569bc6aa78f6eacf72e8b90622d300e1a9db25f (patch) | |
tree | 309048e52eab434a381cf8554436f14de6342cbf /platform/platform-tests/testSrc | |
parent | d1a59a0799588a226d255d9b45c4825b19651554 (diff) | |
download | idea-b569bc6aa78f6eacf72e8b90622d300e1a9db25f.tar.gz |
Snapshot 0b0329a61f47b6070bb9084be8e176635a16fa5c from master branch of git://git.jetbrains.org/idea/community.git
Change-Id: I2e8857776f197bed2d768ea37c67d2e2a1e97952
Diffstat (limited to 'platform/platform-tests/testSrc')
2 files changed, 238 insertions, 40 deletions
diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/FileEditorManagerTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/FileEditorManagerTest.java new file mode 100644 index 000000000000..c8f4c78a9ba1 --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/FileEditorManagerTest.java @@ -0,0 +1,159 @@ +/* + * Copyright 2000-2013 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.openapi.fileEditor; + +import com.intellij.ide.ui.UISettings; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.components.ExpandMacroToPathMap; +import com.intellij.openapi.fileEditor.impl.EditorWithProviderComposite; +import com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl; +import com.intellij.openapi.util.JDOMUtil; +import com.intellij.testFramework.PlatformTestCase; +import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; +import com.intellij.ui.docking.DockManager; +import com.intellij.util.Function; +import com.intellij.util.containers.ContainerUtil; +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jetbrains.jps.model.serialization.PathMacroUtil; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +/** + * @author Dmitry Avdeev + * Date: 4/16/13 + */ +public class FileEditorManagerTest extends LightPlatformCodeInsightFixtureTestCase { + + private FileEditorManagerImpl myManager; + + @SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors") + public FileEditorManagerTest() { + PlatformTestCase.initPlatformLangPrefix(); + } + + public void testTabOrder() throws Exception { + + openFiles(); + assertOpenFiles("1.txt", "foo.xml", "2.txt", "3.txt"); + } + + public void testTabLimit() throws Exception { + + int limit = UISettings.getInstance().EDITOR_TAB_LIMIT; + try { + UISettings.getInstance().EDITOR_TAB_LIMIT = 2; + openFiles(); + // note that foo.xml is pinned + assertOpenFiles("foo.xml", "3.txt"); + } + finally { + UISettings.getInstance().EDITOR_TAB_LIMIT = limit; + } + } + + private void assertOpenFiles(String... fileNames) { + EditorWithProviderComposite[] files = myManager.getSplitters().getEditorsComposites(); + List<String> names = ContainerUtil.map(files, new Function<EditorWithProviderComposite, String>() { + @Override + public String fun(EditorWithProviderComposite composite) { + return composite.getFile().getName(); + } + }); + assertEquals(Arrays.asList(fileNames), names); + } + + private void openFiles() throws IOException, JDOMException, InterruptedException, ExecutionException { + Document document = JDOMUtil.loadDocument(" <component name=\"FileEditorManager\">\n" + + " <leaf>\n" + + " <file leaf-file-name=\"1.txt\" pinned=\"false\" current=\"false\" current-in-tab=\"false\">\n" + + " <entry file=\"file://$PROJECT_DIR$/src/1.txt\">\n" + + " <provider selected=\"true\" editor-type-id=\"text-editor\">\n" + + " <state line=\"0\" column=\"0\" selection-start=\"0\" selection-end=\"0\" vertical-scroll-proportion=\"0.0\">\n" + + " </state>\n" + + " </provider>\n" + + " </entry>\n" + + " </file>\n" + + " <file leaf-file-name=\"foo.xml\" pinned=\"true\" current=\"false\" current-in-tab=\"false\">\n" + + " <entry file=\"file://$PROJECT_DIR$/src/foo.xml\">\n" + + " <provider selected=\"true\" editor-type-id=\"text-editor\">\n" + + " <state line=\"0\" column=\"0\" selection-start=\"0\" selection-end=\"0\" vertical-scroll-proportion=\"0.0\">\n" + + " </state>\n" + + " </provider>\n" + + " </entry>\n" + + " </file>\n" + + " <file leaf-file-name=\"2.txt\" pinned=\"false\" current=\"true\" current-in-tab=\"true\">\n" + + " <entry file=\"file://$PROJECT_DIR$/src/2.txt\">\n" + + " <provider selected=\"true\" editor-type-id=\"text-editor\">\n" + + " <state line=\"0\" column=\"0\" selection-start=\"0\" selection-end=\"0\" vertical-scroll-proportion=\"0.0\">\n" + + " </state>\n" + + " </provider>\n" + + " </entry>\n" + + " </file>\n" + + " <file leaf-file-name=\"3.txt\" pinned=\"false\" current=\"false\" current-in-tab=\"false\">\n" + + " <entry file=\"file://$PROJECT_DIR$/src/3.txt\">\n" + + " <provider selected=\"true\" editor-type-id=\"text-editor\">\n" + + " <state line=\"0\" column=\"0\" selection-start=\"0\" selection-end=\"0\" vertical-scroll-proportion=\"0.0\">\n" + + " </state>\n" + + " </provider>\n" + + " </entry>\n" + + " </file>\n" + + " </leaf>\n" + + " </component>\n"); + Element rootElement = document.getRootElement(); + ExpandMacroToPathMap map = new ExpandMacroToPathMap(); + map.addMacroExpand(PathMacroUtil.PROJECT_DIR_MACRO_NAME, getTestDataPath()); + map.substitute(rootElement, true, true); + + myManager.readExternal(rootElement); + + Future<?> future = ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { + @Override + public void run() { + myManager.getMainSplitters().openFiles(); + } + }); + future.get(); + } + + public void setUp() throws Exception { + super.setUp(); + myManager = new FileEditorManagerImpl(getProject(), DockManager.getInstance(getProject())); + } + + @Override + protected void tearDown() throws Exception { + myManager.closeAllFiles(); + super.tearDown(); + } + + @Override + protected String getTestDataPath() { + return PlatformTestUtil.getCommunityPath().replace(File.separatorChar, '/') + "/platform/platform-tests/testData/fileEditorManager"; + } + + @Override + protected boolean isWriteActionRequired() { + return false; + } +} diff --git a/platform/platform-tests/testSrc/com/intellij/util/StringBuilderSpinAllocatorTest.java b/platform/platform-tests/testSrc/com/intellij/util/StringBuilderSpinAllocatorTest.java index ac61b53a5717..183abe63b7fb 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/StringBuilderSpinAllocatorTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/StringBuilderSpinAllocatorTest.java @@ -15,59 +15,98 @@ */ package com.intellij.util; -import org.junit.Test; +import com.intellij.concurrency.JobLauncher; +import com.intellij.testFramework.PlatformTestCase; +import com.intellij.testFramework.PlatformTestUtil; + +import java.util.Collections; +import java.util.Random; -import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; -public class StringBuilderSpinAllocatorTest { - private final String[] myStrings = new String[]{ - "First String is the smallest", - "Second String is definitely larger than the first one", - "Third String is a bit larger than the first one", - "Fourth String is the largest amongst all the myStrings. Congrats! It must be even larger than it is." - }; +public class StringBuilderSpinAllocatorTest extends PlatformTestCase { - @Test - public void testPerformance() { - assumeTrue(!com.intellij.testFramework.PlatformTestUtil.COVERAGE_ENABLED_BUILD); - doTest(true); - doTest(false); + public static final int THREADS = 1000; + + public void testSequentialPerformance() { + assumeTrue(!PlatformTestUtil.COVERAGE_ENABLED_BUILD); + for (int i=0; i<10; i++) { + long spinTime = time(count, spinAlloc); + long regularTime = time(count, regularAlloc); + System.out.println("regular: " + regularTime + "; spin :" +spinTime+"; ratio: "+(10*spinTime/regularTime)/10.0+" times"); + } + } + public void testConcurrentPerformance() { + assumeTrue(!PlatformTestUtil.COVERAGE_ENABLED_BUILD); + for (int i=0; i<10; i++) { + long spinTime = concurrentTime(count/THREADS, spinAlloc); + long regularTime = concurrentTime(count/THREADS, regularAlloc); + System.out.println("concurrent regular: " + regularTime + "; spin :" +spinTime+"; ratio: "+(10*spinTime/regularTime)/10.0+" times"); + } } - private void doTest(boolean warmUp) { - StringBuilder builder; - int count = warmUp ? 1000 : 1000000; + private static long concurrentTime(int count, final Runnable action) { + return time(count, new Runnable() { + @Override + public void run() { + boolean ok = + JobLauncher.getInstance().invokeConcurrentlyUnderProgress(Collections.nCopies(THREADS, null), null, true, new Processor<Object>() { + @Override + public boolean process(Object o) { + action.run(); + return true; + } + }); - System.gc(); - System.runFinalization(); - TimeoutUtil.sleep(1000); + assertTrue(ok); + } + }); + } - long start = System.nanoTime(); - for (int i = 0; i < count; ++i) { - builder = new StringBuilder(); - builder.append(myStrings[i & 3]); - builder.append(builder.toString()); + static final int count = 100000; + static final int iter = 1000; + static Runnable spinAlloc = new Runnable() { + @Override + public void run() { + for (int i = 0; i < iter; ++i) { + StringBuilder builder = null; + try { + builder = StringBuilderSpinAllocator.alloc(); + if (randomField == 0x78962343) { + System.out.println("xxx"+builder); + } + } + finally { + StringBuilderSpinAllocator.dispose(builder); + } + } } - long regularTime = (System.nanoTime() - start) / 1000; - - System.gc(); - System.runFinalization(); - TimeoutUtil.sleep(1000); + }; + static Runnable regularAlloc = new Runnable() { + @Override + public void run() { + for (int i = 0; i < iter; ++i) { + StringBuilder builder = new StringBuilder(); + if (randomField == 0x78962343) { + System.out.println("xxx"+builder); + } + } + } + }; + private static volatile int randomField = new Random().nextInt(); - start = System.nanoTime(); + private static long time(int count, final Runnable action) { + long start = System.nanoTime(); for (int i = 0; i < count; ++i) { - builder = StringBuilderSpinAllocator.alloc(); - builder.append(myStrings[i & 3]); - builder.append(builder.toString()); - StringBuilderSpinAllocator.dispose(builder); + action.run(); } long spinTime = (System.nanoTime() - start) / 1000; + randomField++; + return spinTime; + } - if (!warmUp) { - System.out.println("StringBuilder regular allocations took: " + regularTime); - System.out.println("StringBuilder spin allocations took: " + spinTime); - assertTrue("regular:" + regularTime + "mks, spin:" + spinTime + "mks", spinTime < regularTime); - } + @Override + protected boolean isRunInWriteAction() { + return false; } } |