1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.osgi.tests.perf;
15 
16 import java.util.Random;
17 import junit.framework.Test;
18 import junit.framework.TestSuite;
19 import org.eclipse.osgi.service.resolver.*;
20 import org.eclipse.osgi.tests.services.resolver.AbstractStateTest;
21 import org.osgi.framework.Version;
22 
23 public class BasePerformanceTest extends AbstractStateTest {
24 	private Random random;
25 
suite()26 	public static Test suite() {
27 		return new TestSuite(BasePerformanceTest.class);
28 	}
29 
BasePerformanceTest(String name)30 	public BasePerformanceTest(String name) {
31 		super(name);
32 	}
33 
buildRandomState(int size)34 	protected State buildRandomState(int size) {
35 		State state = buildEmptyState();
36 		StateObjectFactory stateFactory = state.getFactory();
37 		BundleDescription[] bundles = new BundleDescription[size];
38 		int exportedPackages = 0;
39 		for (int i = 0; i < bundles.length; i++) {
40 			long bundleId = i;
41 			String symbolicName = "bundle" + i;
42 			Version version = new Version(1, 0, 0);
43 
44 			int exportPackageCount = random.nextInt(5);
45 			ExportPackageDescription[] exportPackages = new ExportPackageDescription[exportPackageCount];
46 			for (int j = 0; j < exportPackages.length; j++) {
47 				String packageName = "package." + exportedPackages++;
48 				Version packageVersion = Version.parseVersion("1.0.0");
49 				exportPackages[j] = stateFactory.createExportPackageDescription(packageName, packageVersion, null, null, true, null);
50 			}
51 			int importPackageCount = Math.min(exportPackageCount, random.nextInt(5));
52 			int importedPackageIndex = random.nextInt(exportPackageCount + 1);
53 			ImportPackageSpecification[] importPackages = new ImportPackageSpecification[importPackageCount];
54 			for (int j = 0; j < importPackages.length; j++) {
55 				int index = importedPackageIndex++;
56 				if (importedPackageIndex > exportPackageCount)
57 					importedPackageIndex = 1;
58 				String packageName = "package." + index;
59 				importPackages[j] = stateFactory.createImportPackageSpecification(packageName, new VersionRange("1.0.0"), null, null, null, null, null);
60 			}
61 
62 			BundleSpecification[] requiredBundles = new BundleSpecification[Math.min(i, random.nextInt(5))];
63 			for (int j = 0; j < requiredBundles.length; j++) {
64 				int requiredIndex = random.nextInt(i);
65 				String requiredName = bundles[requiredIndex].getSymbolicName();
66 				Version requiredVersion = bundles[requiredIndex].getVersion();
67 				boolean export = random.nextInt(10) > 6;
68 				boolean optional = random.nextInt(10) > 8;
69 				requiredBundles[j] = stateFactory.createBundleSpecification(requiredName, new VersionRange(requiredVersion.toString()), export, optional);
70 			}
71 
72 			bundles[i] = stateFactory.createBundleDescription(bundleId, symbolicName, version, symbolicName, requiredBundles, (HostSpecification) null, importPackages, exportPackages, null, random.nextDouble() > 0.05);
73 			state.addBundle(bundles[i]);
74 		}
75 		return state;
76 	}
77 
78 	@Override
setUp()79 	protected void setUp() throws Exception {
80 		super.setUp();
81 		// uses a constant seed to prevent variation on results
82 		this.random = new Random(0);
83 	}
84 
85 }
86