Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the performance issue that test explorer will keep refreshing when opening a large project #533

Merged
merged 7 commits into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions java-extension/com.microsoft.java.test.plugin/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension point="org.eclipse.jdt.ls.core.delegateCommandHandler">
<delegateCommandHandler class="com.microsoft.java.test.plugin.handler.TestDelegateCommandHandler">
<command id="vscode.java.test.runtime.classpath" />
<command id="vscode.java.test.project.info" />
<command id="vscode.java.test.search.items" />
<command id="vscode.java.test.search.items.all" />
<command id="vscode.java.test.search.codelens" />
</delegateCommandHandler>
</extension>
<extension point="org.eclipse.jdt.ls.core.delegateCommandHandler">
<delegateCommandHandler class="com.microsoft.java.test.plugin.handler.TestDelegateCommandHandler">
<command id="vscode.java.test.get.testpath" />
<command id="vscode.java.test.runtime.classpath" />
<command id="vscode.java.test.project.info" />
<command id="vscode.java.test.search.items" />
<command id="vscode.java.test.search.items.all" />
<command id="vscode.java.test.search.codelens" />
</delegateCommandHandler>
</extension>
</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package com.microsoft.java.test.plugin.handler;

import com.microsoft.java.test.plugin.util.ProjectInfoFetcher;
import com.microsoft.java.test.plugin.util.ProjectUtils;
import com.microsoft.java.test.plugin.util.RuntimeClassPathUtils;
import com.microsoft.java.test.plugin.util.TestSearchUtils;

Expand All @@ -23,6 +24,7 @@
@SuppressWarnings("restriction")
public class TestDelegateCommandHandler implements IDelegateCommandHandler {

private static final String GET_TEST_SOURCE_PATH = "vscode.java.test.get.testpath";
private static final String COMPUTE_RUNTIME_CLASSPATH = "vscode.java.test.runtime.classpath";
private static final String GET_PROJECT_INFO = "vscode.java.test.project.info";
private static final String SEARCH_TEST_ITEMS = "vscode.java.test.search.items";
Expand All @@ -33,6 +35,8 @@ public class TestDelegateCommandHandler implements IDelegateCommandHandler {
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor monitor) throws Exception {

switch (commandId) {
case GET_TEST_SOURCE_PATH:
return ProjectUtils.getTestSourcePaths(arguments, monitor);
case COMPUTE_RUNTIME_CLASSPATH:
return RuntimeClassPathUtils.resolveRuntimeClassPath(arguments);
case GET_PROJECT_INFO:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
Expand All @@ -25,7 +26,9 @@
import org.eclipse.jdt.internal.core.ClasspathEntry;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
Expand All @@ -38,6 +41,37 @@ public final class ProjectUtils {
private static final String MAVEN_SCOPE_ATTRIBUTE = "maven.scope";
private static final String GRADLE_SCOPE_ATTRIBUTE = "gradle_scope";

/**
* Method to get the valid paths which contains test code
*
* @param arguments Array of the workspace folder path
* @param monitor
* @throws URISyntaxException
* @throws JavaModelException
*/
@SuppressWarnings("unchecked")
public static String[] getTestSourcePaths(List<Object> arguments, IProgressMonitor monitor)
throws URISyntaxException, JavaModelException {

final List<String> resultList = new ArrayList<>();
if (arguments == null || arguments.size() == 0) {
return new String[0];
}

final ArrayList<String> uriArray = ((ArrayList<String>) arguments.get(0));
for (final String uri : uriArray) {
final Set<IJavaProject> projectSet = parseProjects(new URI(uri));
for (final IJavaProject project : projectSet) {
for (final IPath path : getTestPath(project)) {
final IPath projectBasePath = project.getProject().getLocation();
final IPath relativePath = path.makeRelativeTo(project.getPath());
resultList.add(projectBasePath.append(relativePath).toOSString());
}
}
}
return resultList.toArray(new String[resultList.size()]);
}

public static Set<IJavaProject> parseProjects(URI rootFolderURI) {
final IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
final IProject[] projects = workspaceRoot.getProjects();
Expand Down
1 change: 1 addition & 0 deletions src/constants/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export namespace JavaLanguageServerCommands {
}

export namespace JavaTestRunnerDelegateCommands {
export const GET_TEST_SOURCE_PATH: string = 'vscode.java.test.get.testpath';
export const SEARCH_TEST_ITEMS: string = 'vscode.java.test.search.items';
export const SEARCH_TEST_ITEMS_ALL: string = 'vscode.java.test.search.items.all';
export const SEARCH_TEST_CODE_LENS: string = 'vscode.java.test.search.codelens';
Expand Down
20 changes: 3 additions & 17 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as fse from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import { commands, Disposable, Extension, ExtensionContext, extensions, FileSystemWatcher, languages, Uri, window, workspace } from 'vscode';
import { commands, Disposable, Extension, ExtensionContext, extensions, languages, window } from 'vscode';
import { dispose as disposeTelemetryWrapper, initializeFromJsonFile, instrumentOperation } from 'vscode-extension-telemetry-wrapper';
import { testCodeLensProvider } from './codeLensProvider';
import { debugTests, runTests } from './commands/executeTests';
Expand All @@ -17,6 +17,7 @@ import { TestTreeNode } from './explorer/TestTreeNode';
import { logger } from './logger/logger';
import { ITestItem } from './protocols';
import { runnerExecutor } from './runners/runnerExecutor';
import { testFileWatcher } from './testFileWatcher';
import { testReportProvider } from './testReportProvider';
import { testResultManager } from './testResultManager';
import { testStatusBarProvider } from './testStatusBarProvider';
Expand All @@ -37,21 +38,7 @@ async function doActivate(_operationId: string, context: ExtensionContext): Prom
throw new Error('Could not find Java home.');
}

const watcher: FileSystemWatcher = workspace.createFileSystemWatcher('**/*.{[jJ][aA][vV][aA]}');
watcher.onDidChange((uri: Uri) => {
const node: TestTreeNode | undefined = explorerNodeManager.getNode(uri.fsPath);
if (node) {
testExplorer.refresh(node);
}
});
watcher.onDidDelete((uri: Uri) => {
explorerNodeManager.removeNode(uri.fsPath);
testExplorer.refresh();
});
watcher.onDidCreate(() => {
testExplorer.refresh();
});

testFileWatcher.initialize(context);
testExplorer.initialize(context);
runnerExecutor.initialize(javaHome, context);
testReportProvider.initialize(context);
Expand All @@ -67,7 +54,6 @@ async function doActivate(_operationId: string, context: ExtensionContext): Prom
testResultManager,
testReportProvider,
logger,
watcher,
languages.registerCodeLensProvider({ scheme: 'file', language: 'java' }, testCodeLensProvider),
instrumentAndRegisterCommand(JavaTestRunnerCommands.OPEN_DOCUMENT_FOR_NODE, async (node: TestTreeNode) => await openTextDocumentForNode(node)),
instrumentAndRegisterCommand(JavaTestRunnerCommands.REFRESH_EXPLORER, (node: TestTreeNode) => testExplorer.refresh(node)),
Expand Down
46 changes: 46 additions & 0 deletions src/testFileWatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { Disposable, ExtensionContext, FileSystemWatcher, RelativePattern, Uri, workspace, WorkspaceFolder } from 'vscode';
import { explorerNodeManager } from './explorer/explorerNodeManager';
import { testExplorer } from './explorer/testExplorer';
import { TestTreeNode } from './explorer/TestTreeNode';
import { logger } from './logger/logger';
import { getTestSourcePaths } from './utils/commandUtils';

class TestFileWatcher {

public async initialize(context: ExtensionContext): Promise<void> {
if (workspace.workspaceFolders) {
try {
const paths: string[] = await getTestSourcePaths(workspace.workspaceFolders.map((workspaceFolder: WorkspaceFolder) => workspaceFolder.uri.toString()));
for (const path of paths) {
const pattern: RelativePattern = new RelativePattern(Uri.file(path).fsPath, '**/*.{[jJ][aA][vV][aA]}');
const watcher: FileSystemWatcher = workspace.createFileSystemWatcher(pattern, true /* ignoreCreateEvents */);
this.registerWatcherListeners(watcher, context.subscriptions);
context.subscriptions.push(watcher);
}
} catch (error) {
logger.error('Failed to get the test paths', error);
const watcher: FileSystemWatcher = workspace.createFileSystemWatcher('**/*.{[jJ][aA][vV][aA]}');
this.registerWatcherListeners(watcher, context.subscriptions);
context.subscriptions.push(watcher);
}
}

}

private registerWatcherListeners(watcher: FileSystemWatcher, disposables: Disposable[]): void {
watcher.onDidChange((uri: Uri) => {
const node: TestTreeNode | undefined = explorerNodeManager.getNode(uri.fsPath);
testExplorer.refresh(node);
}, null, disposables);

watcher.onDidDelete((uri: Uri) => {
explorerNodeManager.removeNode(uri.fsPath);
testExplorer.refresh();
}, null, disposables);
}
}

export const testFileWatcher: TestFileWatcher = new TestFileWatcher();
5 changes: 5 additions & 0 deletions src/utils/commandUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { commands, Uri } from 'vscode';
import { JavaLanguageServerCommands, JavaTestRunnerDelegateCommands } from '../constants/commands';
import { IProjectInfo, ISearchTestItemParams, ITestItem } from '../protocols';

export async function getTestSourcePaths(uri: string[]): Promise<string[]> {
return await executeJavaLanguageServerCommand<string[]>(
JavaTestRunnerDelegateCommands.GET_TEST_SOURCE_PATH, uri) || [];
}

export async function searchTestItems(params: ISearchTestItemParams): Promise<ITestItem[]> {
return await executeJavaLanguageServerCommand<ITestItem[]>(
JavaTestRunnerDelegateCommands.SEARCH_TEST_ITEMS, JSON.stringify(params)) || [];
Expand Down