View | Details | Raw Unified | Return to bug 301563 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/core/tests/internal/localstore/SnapshotImportPerformanceTest.java (+164 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *     Francis Lynch (Wind River) - [301563] adapted from
11
 *     		RefreshLocalPerformanceTest
12
 *******************************************************************************/
13
package org.eclipse.core.tests.internal.localstore;
14
15
import java.io.File;
16
import java.net.URI;
17
import java.util.Date;
18
import junit.framework.Test;
19
import junit.framework.TestSuite;
20
import org.eclipse.core.resources.*;
21
import org.eclipse.core.runtime.IPath;
22
import org.eclipse.core.runtime.Path;
23
import org.eclipse.core.tests.resources.ResourceDeltaVerifier;
24
import org.eclipse.core.tests.resources.ResourceTest;
25
26
//
27
public class SnapshotImportPerformanceTest extends ResourceTest {
28
	/** big site default volume (windows) */
29
	public static final String bigSiteDevice = "d:";
30
31
	/** big site initial location */
32
	public static final IPath bigSiteLocation = new Path(bigSiteDevice, "/bigsite");
33
34
	/** settings directory name */
35
	private static final String DIR_NAME = ".settings";
36
37
	/** location of refresh snapshot file */
38
	private static final String REFRESH_SNAPSHOT_FILE_LOCATION = ".settings/resource-index.zip";
39
40
	/** benchmark */
41
	public Date startDate;
42
43
	public SnapshotImportPerformanceTest() {
44
		super();
45
	}
46
47
	public SnapshotImportPerformanceTest(String name) {
48
		super(name);
49
	}
50
51
	protected int countChildren(File root) {
52
		String[] children = root.list();
53
		if (children == null)
54
			return 0;
55
		int result = 0;
56
		for (int i = 0; i < children.length; i++) {
57
			File child = new File(root, children[i]);
58
			if (child.isDirectory())
59
				result += countChildren(child);
60
			result++;
61
		}
62
		return result;
63
	}
64
65
	public String dispTime(long diff) {
66
		return String.valueOf(diff);
67
	}
68
69
	public void startClock() {
70
		startDate = new Date();
71
	}
72
73
	public long stopClock() {
74
		Date stopDate = new Date();
75
		return stopDate.getTime() - startDate.getTime();
76
	}
77
78
	// this test should not be in AllTests because it is only a performance test
79
	public static Test suite() {
80
		TestSuite suite = new TestSuite(SnapshotImportPerformanceTest.class.getName());
81
		suite.addTest(new SnapshotImportPerformanceTest("testSnapshotImportPerformance"));
82
		return suite;
83
	}
84
85
	/**
86
	 * Open a project and export a refresh snapshot. Re-open the project using
87
	 * the snapshot and compare the times for opening with and without the
88
	 * snapshot.
89
	 */
90
	public void testSnapshotImportPerformance() throws Exception {
91
		// test if the test can be done in this machine
92
		if (!bigSiteLocation.toFile().isDirectory())
93
			return;
94
95
		// create common objects
96
		IProject project = getWorkspace().getRoot().getProject("MyTestProject");
97
		IProjectDescription description = getWorkspace().newProjectDescription(project.getName());
98
		description.setLocation(bigSiteLocation);
99
100
		// performance data
101
		long originalOpen = 0;
102
		long snapshotOpen = 0;
103
		long refresh2Open = 0;
104
105
		// report the number of files to be refreshed
106
		int numberOfFiles = countChildren(bigSiteLocation.toFile());
107
		System.out.println("Number of local resources: " + numberOfFiles);
108
109
		// create the project from the location
110
		project.create(description, null);
111
112
		// open the project, timing the initial refresh
113
		startClock();
114
		project.open(null);
115
		originalOpen = stopClock();
116
117
		// dump the snapshot refresh info
118
		ensureExistsInWorkspace(project.getFolder(DIR_NAME), true);
119
		IPath projPath = project.getLocation();
120
		projPath = projPath.append(REFRESH_SNAPSHOT_FILE_LOCATION);
121
		URI snapshotLocation = org.eclipse.core.filesystem.URIUtil.toURI(projPath);
122
		project.saveSnapshot(IProject.SNAPSHOT_TREE, snapshotLocation, null);
123
124
		// close and delete project but leave contents
125
		project.close(null);
126
		project.delete(false, false, null);
127
128
		// open the project and import refresh snapshot
129
		project.create(description, null);
130
		startClock();
131
		project.loadSnapshot(IProject.SNAPSHOT_TREE, snapshotLocation, null);
132
		project.open(IResource.NONE, null);
133
		snapshotOpen = stopClock();
134
135
		// now refresh the project, verifying zero resource delta
136
		// (except for the creation of .settings/resource-index.zip)
137
		ResourceDeltaVerifier verifier = new ResourceDeltaVerifier();
138
		ResourcesPlugin.getWorkspace().addResourceChangeListener(verifier);
139
		verifier.reset();
140
		IFolder settings = project.getFolder(DIR_NAME);
141
		IFile snapshot = project.getFile(REFRESH_SNAPSHOT_FILE_LOCATION);
142
		verifier.addExpectedChange(settings, IResourceDelta.CHANGED, 0);
143
		verifier.addExpectedChange(snapshot, IResourceDelta.ADDED, 0);
144
		project.refreshLocal(IResource.DEPTH_INFINITE, null);
145
		verifier.verifyDelta(null);
146
		assertTrue("2.0 " + verifier.getMessage(), verifier.isDeltaValid());
147
148
		// close and delete project but leave contents
149
		project.close(null);
150
		project.delete(false, false, null);
151
		IPath snapshotFile = bigSiteLocation.append(REFRESH_SNAPSHOT_FILE_LOCATION);
152
		snapshotFile.toFile().delete();
153
154
		// open the project again with standard refresh
155
		project.create(description, null);
156
		startClock();
157
		project.open(null);
158
		refresh2Open = stopClock();
159
160
		System.out.println("Original open: " + originalOpen);
161
		System.out.println("Snapshot open: " + snapshotOpen);
162
		System.out.println("Second refresh open: " + refresh2Open);
163
	}
164
}

Return to bug 301563