Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a511fc0d1c33c9f80efe45cd76eee3179a22cc74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*******************************************************************************
 *  Copyright (c) 2011 Wind River 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:
 *     Wind River - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.artifact.repository;

import java.io.File;
import java.net.URI;
import java.util.*;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.query.IQueryResult;
import org.eclipse.equinox.p2.repository.artifact.*;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;

public class Bug351944 extends AbstractProvisioningTest {

	File artifactRepoFile = null;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		File testData = getTestData("artifact repository", "testData/bug351944");
		artifactRepoFile = getTempFolder();
		copy("Copy to temporary folder", testData, artifactRepoFile);
		changeWritePermission(artifactRepoFile, false);
	}

	/**
	 * it doesn't work on Windows to make a folder read-only then can't create
	 * new file in it
	 */
	private void changeWritePermission(File target, boolean canWrite) {
		if (target.exists()) {
			target.setWritable(canWrite);
			if (target.isDirectory()) {
				for (File child : target.listFiles()) {
					child.setWritable(canWrite);
				}
			}
		}
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		changeWritePermission(artifactRepoFile, true);
		delete(artifactRepoFile);
	}

	public void testSimpleRepositoryPerformanceOnLoadReadonlyLocalRepository() throws ProvisionException {
		if (!Platform.OS_WIN32.equals(Platform.getOS())) {
			final URI testRepo = artifactRepoFile.toURI();
			IArtifactRepositoryManager artifactRepositoryManager = getArtifactRepositoryManager();
			IArtifactRepository repo = artifactRepositoryManager.loadRepository(testRepo, new NullProgressMonitor());
			IQueryResult<IArtifactKey> allArtifactKeys = repo.query(ArtifactKeyQuery.ALL_KEYS, new NullProgressMonitor());
			Set<IArtifactKey> keySet = allArtifactKeys.toUnmodifiableSet();

			Collection<IArtifactRequest> requests = new ArrayList<IArtifactRequest>();
			for (IArtifactKey key : keySet)
				requests.add(artifactRepositoryManager.createMirrorRequest(key, repo, null, null));

			long start = System.currentTimeMillis();
			IArtifactRequest[] toBeRequests = getRequestsForRepository(repo, requests.toArray(new IArtifactRequest[requests.size()]));
			long end = System.currentTimeMillis();
			long queryArtifactOneByOne = end - start;

			start = System.currentTimeMillis();
			IArtifactRequest[] toBeRequests2 = getRequestsForRepository2(repo, requests.toArray(new IArtifactRequest[requests.size()]));
			end = System.currentTimeMillis();
			long queryAllArtifacts = end - start;

			assertEquals("Test case has problem, not find same requests.", toBeRequests.length, toBeRequests2.length);
			assertEquals("Querying artifact key from simple repository has performance issue.", queryAllArtifacts, queryArtifactOneByOne, 10);
		}
	}

	/**
	 * copy from {@link org.eclipse.equinox.internal.p2.engine.DownloadManager}
	 * @param repository
	 * @param requestsToProcess
	 * @return
	 */
	private IArtifactRequest[] getRequestsForRepository(IArtifactRepository repository, IArtifactRequest[] requestsToProcess) {
		ArrayList<IArtifactRequest> applicable = new ArrayList<IArtifactRequest>();
		for (IArtifactRequest request : requestsToProcess) {
			if (repository.contains(request.getArtifactKey()))
				applicable.add(request);
		}
		return applicable.toArray(new IArtifactRequest[applicable.size()]);
	}

	private IArtifactRequest[] getRequestsForRepository2(IArtifactRepository repository, IArtifactRequest[] requestsToProcess) {
		Set<IArtifactKey> keys = repository.query(ArtifactKeyQuery.ALL_KEYS, new NullProgressMonitor()).toSet();
		ArrayList<IArtifactRequest> applicable = new ArrayList<IArtifactRequest>();
		for (IArtifactRequest request : requestsToProcess) {
			if (keys.contains(request.getArtifactKey()))
				applicable.add(request);
		}
		return applicable.toArray(new IArtifactRequest[applicable.size()]);
	}
}

Back to the top