blob: ee379f1bc97d591e9beeee067dae34d413635ee1 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2003, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.osgi.tests.perf;
import java.io.*;
import java.util.Random;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.osgi.service.resolver.*;
import org.eclipse.osgi.tests.services.resolver.AbstractStateTest;
import org.eclipse.test.performance.Performance;
import org.eclipse.test.performance.PerformanceMeter;
public class StatePerformanceTest extends AbstractStateTest {
// value copied from StateObjectFactoryImpl
private static final byte GREATER_OR_EQUAL = 4;
private Random random;
public static Test suite() {
return new TestSuite(StatePerformanceTest.class);
}
public StatePerformanceTest(String name) {
super(name);
}
private State buildRandomState(int size) {
State state = buildEmptyState();
StateObjectFactory stateFactory = state.getFactory();
BundleDescription[] bundles = new BundleDescription[size];
int exportedPackages = 0;
for (int i = 0; i < bundles.length; i++) {
long bundleId = i;
String symbolicName = "bundle" + i;
Version version = new Version(1, 0, 0);
int packageCount = random.nextInt(10);
PackageSpecification[] packages = new PackageSpecification[packageCount];
for (int j = 0; j < packages.length; j++) {
boolean export = false;
String packageName;
if (exportedPackages == 0 || (exportedPackages < packages.length && random.nextInt(10) > 6)) {
export = true;
packageName = "package." + ++exportedPackages;
} else
packageName = "package." + (random.nextInt(exportedPackages) + 1);
Version packageVersion = new Version("1.0.0");
packages[j] = stateFactory.createPackageSpecification(packageName, packageVersion, export);
}
BundleSpecification[] requiredBundles = new BundleSpecification[Math.min(i, random.nextInt(5))];
for (int j = 0; j < requiredBundles.length; j++) {
int requiredIndex = random.nextInt(i);
String requiredName = bundles[requiredIndex].getSymbolicName();
Version requiredVersion = bundles[requiredIndex].getVersion();
boolean export = random.nextInt(10) > 6;
boolean optional = random.nextInt(10) > 8;
requiredBundles[j] = stateFactory.createBundleSpecification(requiredName, requiredVersion, GREATER_OR_EQUAL, export, optional);
}
int providedPackageCount = random.nextInt(10);
String[] providedPackages = new String[providedPackageCount];
for (int j = 0; j < providedPackages.length; j++)
providedPackages[j] = symbolicName + ".package" + j;
bundles[i] = stateFactory.createBundleDescription(bundleId, symbolicName, version, symbolicName, requiredBundles, (HostSpecification) null, packages, providedPackages, random.nextDouble() > 0.05);
state.addBundle(bundles[i]);
}
return state;
}
private void runPerformanceTest(Runnable operation, int iterations) {
Performance perf = Performance.getDefault();
PerformanceMeter meter = perf.createPerformanceMeter(perf.getDefaultScenarioId(this));
try {
for (int i = 0; i < iterations; i++) {
meter.start();
operation.run();
meter.stop();
}
meter.commit();
perf.assertPerformance(meter);
} finally {
meter.dispose();
}
}
protected void setUp() throws Exception {
super.setUp();
// uses a constant seed to prevent variation on results
this.random = new Random(0);
}
private State storeAndRetrieve(State toStore) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
toStore.getFactory().writeState(toStore, dos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
DataInputStream dis = new DataInputStream(bais);
return toStore.getFactory().readState(dis);
}
public void testCreation() {
final int stateSize = 5000;
runPerformanceTest(new Runnable() {
public void run() {
buildRandomState(stateSize);
}
}, 10);
}
private void testResolution(int stateSize) throws IOException {
final State originalState = buildRandomState(stateSize);
runPerformanceTest(new Runnable() {
public void run() {
originalState.resolve();
}
}, 10);
}
public void testResolution100() throws IOException {
testResolution(100);
}
public void testResolution1000() throws IOException {
testResolution(1000);
}
public void testResolution500() throws IOException {
testResolution(500);
}
public void testResolution5000() throws IOException {
testResolution(5000);
}
public void testStoreAndRetrieve() {
int stateSize = 5000;
final State originalState = buildRandomState(stateSize);
runPerformanceTest(new Runnable() {
public void run() {
try {
storeAndRetrieve(originalState);
} catch (IOException e) {
StatePerformanceTest.this.fail("", e);
}
}
}, 10);
}
}