### Eclipse Workspace Patch 1.0 #P org.eclipse.core.tests.resources Index: src/org/eclipse/core/tests/internal/localstore/ExportSnapshotTest.java =================================================================== RCS file: src/org/eclipse/core/tests/internal/localstore/ExportSnapshotTest.java diff -N src/org/eclipse/core/tests/internal/localstore/ExportSnapshotTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/tests/internal/localstore/ExportSnapshotTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,139 @@ +/******************************************************************************* + * Copyright (c) 2000, 2010 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Francis Lynch (Wind River) - adapted from FileSystemResourceManagerTest + *******************************************************************************/ +package org.eclipse.core.tests.internal.localstore; + +import java.net.URI; +import junit.framework.Test; +import junit.framework.TestSuite; +import org.eclipse.core.internal.resources.ICoreConstants; +import org.eclipse.core.resources.*; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.tests.resources.ResourceDeltaVerifier; + +// +public class ExportSnapshotTest extends LocalStoreTest implements ICoreConstants { + + /** location of refresh snapshot file */ + private static final String REFRESH_SNAPSHOT_FILE_LOCATION = "resource-index.zip"; + + public ExportSnapshotTest() { + super(); + } + + public ExportSnapshotTest(String name) { + super(name); + } + + public static Test suite() { + return new TestSuite(ExportSnapshotTest.class); + } + + private void populateProject(IProject project) { + // add files and folders to project + IFile file = project.getFile("file"); + ensureExistsInFileSystem(file); + IFolder folder = projects[0].getFolder("folder"); + IFolder subfolder = folder.getFolder("subfolder"); + IFile subfile = folder.getFile("subfile"); + ensureExistsInFileSystem(folder); + ensureExistsInFileSystem(subfolder); + ensureExistsInFileSystem(subfile); + } + + private URI getSnapshotLocation(IProject project) { + IPath projPath = project.getLocation(); + projPath = projPath.append(REFRESH_SNAPSHOT_FILE_LOCATION); + return org.eclipse.core.filesystem.URIUtil.toURI(projPath); + } + + /* + * Create project and populate with resources. Save snapshot. + * Delete project (also delete resources on disk). Import project + * with snapshot. All resources must be marked as "exists" in the + * resource tree, although there is no resource actually on disk. + */ + public void testImportNoRefresh() throws Throwable { + final IProject project = projects[0]; + // add files and folders to project + populateProject(project); + // perform refresh to ensure new resources in tree + project.refreshLocal(IResource.DEPTH_INFINITE, null); + // save project refresh snapshot outside the project + URI snapshotLocation = getSnapshotLocation(projects[1]); + project.saveSnapshot(IProject.SNAPSHOT_TREE, snapshotLocation, null); + // close and delete project contents + project.close(null); + // delete the project and import refresh snapshot + project.delete(true, false, null); + project.create(null); + project.loadSnapshot(IProject.SNAPSHOT_TREE, snapshotLocation, null); + project.open(IResource.NONE, null); + // verify that the resources are thought to exist + IFile file = project.getFile("file"); + IFolder folder = project.getFolder("folder"); + IFolder subfolder = folder.getFolder("subfolder"); + IFile subfile = folder.getFile("subfile"); + assertTrue("1.1", file.exists()); + assertTrue("1.2", folder.exists()); + assertTrue("1.3", subfolder.exists()); + assertTrue("1.4", subfile.exists()); + } + + /* + * Create project and populate with resources. Save snapshot. + * Delete project (also delete resources on disk). Import project + * with snapshot and perform a refresh. Resource delta must be created, + * and none of the snapshot resources is found any more. + */ + public void testImportWithRefresh() throws Throwable { + final IProject project = projects[0]; + // add files and folders to project + populateProject(project); + // perform refresh to ensure new resources in tree + project.refreshLocal(IResource.DEPTH_INFINITE, null); + // save project refresh snapshot outside the project + URI snapshotLocation = getSnapshotLocation(projects[1]); + project.saveSnapshot(IProject.SNAPSHOT_TREE, snapshotLocation, null); + // close and delete project contents + project.close(null); + // delete the project and import refresh snapshot + project.delete(true, false, null); + project.create(null); + project.loadSnapshot(IProject.SNAPSHOT_TREE, snapshotLocation, null); + project.open(IResource.NONE, null); + // set up resource delta verifier + ResourceDeltaVerifier verifier = new ResourceDeltaVerifier(); + ResourcesPlugin.getWorkspace().addResourceChangeListener(verifier); + verifier.reset(); + IFile file = project.getFile("file"); + IFolder folder = project.getFolder("folder"); + IFolder subfolder = folder.getFolder("subfolder"); + IFile subfile = folder.getFile("subfile"); + verifier.addExpectedChange(file, IResourceDelta.REMOVED, 0); + verifier.addExpectedChange(folder, IResourceDelta.REMOVED, 0); + verifier.addExpectedChange(subfolder, IResourceDelta.REMOVED, 0); + verifier.addExpectedChange(subfile, IResourceDelta.REMOVED, 0); + verifier.addExpectedChange(project, IResourceDelta.CHANGED, IResourceDelta.DESCRIPTION); + IFile dotProject = project.getFile(IProjectDescription.DESCRIPTION_FILE_NAME); + verifier.addExpectedChange(dotProject, IResourceDelta.CHANGED, IResourceDelta.CONTENT); + // perform refresh to create resource delta against snapshot + project.refreshLocal(IResource.DEPTH_INFINITE, null); + verifier.verifyDelta(null); + assertTrue("1.0 " + verifier.getMessage(), verifier.isDeltaValid()); + // verify that the resources are no longer thought to exist + assertTrue("1.1", !file.exists()); + assertTrue("1.2", !folder.exists()); + assertTrue("1.3", !subfolder.exists()); + assertTrue("1.4", !subfile.exists()); + } + +} Index: src/org/eclipse/core/tests/internal/localstore/SnapshotImportPerformanceTest.java =================================================================== RCS file: src/org/eclipse/core/tests/internal/localstore/SnapshotImportPerformanceTest.java diff -N src/org/eclipse/core/tests/internal/localstore/SnapshotImportPerformanceTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/tests/internal/localstore/SnapshotImportPerformanceTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,164 @@ +/******************************************************************************* + * Copyright (c) 2000, 2010 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Francis Lynch (Wind River) - [301563] adapted from + * RefreshLocalPerformanceTest + *******************************************************************************/ +package org.eclipse.core.tests.internal.localstore; + +import java.io.File; +import java.net.URI; +import java.util.Date; +import junit.framework.Test; +import junit.framework.TestSuite; +import org.eclipse.core.resources.*; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.tests.resources.ResourceDeltaVerifier; +import org.eclipse.core.tests.resources.ResourceTest; + +// +public class SnapshotImportPerformanceTest extends ResourceTest { + /** big site default volume (windows) */ + public static final String bigSiteDevice = "d:"; + + /** big site initial location */ + public static final IPath bigSiteLocation = new Path(bigSiteDevice, "/bigsite"); + + /** settings directory name */ + private static final String DIR_NAME = ".settings"; + + /** location of refresh snapshot file */ + private static final String REFRESH_SNAPSHOT_FILE_LOCATION = ".settings/resource-index.zip"; + + /** benchmark */ + public Date startDate; + + public SnapshotImportPerformanceTest() { + super(); + } + + public SnapshotImportPerformanceTest(String name) { + super(name); + } + + protected int countChildren(File root) { + String[] children = root.list(); + if (children == null) + return 0; + int result = 0; + for (int i = 0; i < children.length; i++) { + File child = new File(root, children[i]); + if (child.isDirectory()) + result += countChildren(child); + result++; + } + return result; + } + + public String dispTime(long diff) { + return String.valueOf(diff); + } + + public void startClock() { + startDate = new Date(); + } + + public long stopClock() { + Date stopDate = new Date(); + return stopDate.getTime() - startDate.getTime(); + } + + // this test should not be in AllTests because it is only a performance test + public static Test suite() { + TestSuite suite = new TestSuite(SnapshotImportPerformanceTest.class.getName()); + suite.addTest(new SnapshotImportPerformanceTest("testSnapshotImportPerformance")); + return suite; + } + + /** + * Open a project and export a refresh snapshot. Re-open the project using + * the snapshot and compare the times for opening with and without the + * snapshot. + */ + public void testSnapshotImportPerformance() throws Exception { + // test if the test can be done in this machine + if (!bigSiteLocation.toFile().isDirectory()) + return; + + // create common objects + IProject project = getWorkspace().getRoot().getProject("MyTestProject"); + IProjectDescription description = getWorkspace().newProjectDescription(project.getName()); + description.setLocation(bigSiteLocation); + + // performance data + long originalOpen = 0; + long snapshotOpen = 0; + long refresh2Open = 0; + + // report the number of files to be refreshed + int numberOfFiles = countChildren(bigSiteLocation.toFile()); + System.out.println("Number of local resources: " + numberOfFiles); + + // create the project from the location + project.create(description, null); + + // open the project, timing the initial refresh + startClock(); + project.open(null); + originalOpen = stopClock(); + + // dump the snapshot refresh info + ensureExistsInWorkspace(project.getFolder(DIR_NAME), true); + IPath projPath = project.getLocation(); + projPath = projPath.append(REFRESH_SNAPSHOT_FILE_LOCATION); + URI snapshotLocation = org.eclipse.core.filesystem.URIUtil.toURI(projPath); + project.saveSnapshot(IProject.SNAPSHOT_TREE, snapshotLocation, null); + + // close and delete project but leave contents + project.close(null); + project.delete(false, false, null); + + // open the project and import refresh snapshot + project.create(description, null); + startClock(); + project.loadSnapshot(IProject.SNAPSHOT_TREE, snapshotLocation, null); + project.open(IResource.NONE, null); + snapshotOpen = stopClock(); + + // now refresh the project, verifying zero resource delta + // (except for the creation of .settings/resource-index.zip) + ResourceDeltaVerifier verifier = new ResourceDeltaVerifier(); + ResourcesPlugin.getWorkspace().addResourceChangeListener(verifier); + verifier.reset(); + IFolder settings = project.getFolder(DIR_NAME); + IFile snapshot = project.getFile(REFRESH_SNAPSHOT_FILE_LOCATION); + verifier.addExpectedChange(settings, IResourceDelta.CHANGED, 0); + verifier.addExpectedChange(snapshot, IResourceDelta.ADDED, 0); + project.refreshLocal(IResource.DEPTH_INFINITE, null); + verifier.verifyDelta(null); + assertTrue("2.0 " + verifier.getMessage(), verifier.isDeltaValid()); + + // close and delete project but leave contents + project.close(null); + project.delete(false, false, null); + IPath snapshotFile = bigSiteLocation.append(REFRESH_SNAPSHOT_FILE_LOCATION); + snapshotFile.toFile().delete(); + + // open the project again with standard refresh + project.create(description, null); + startClock(); + project.open(null); + refresh2Open = stopClock(); + + System.out.println("Original open: " + originalOpen); + System.out.println("Snapshot open: " + snapshotOpen); + System.out.println("Second refresh open: " + refresh2Open); + } +}