Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Measure the effects of profile indexer in Papyrus
package org.eclipse.papyrus.uml.profile.measures;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.papyrus.infra.core.editor.ModelSetServiceFactory;
import org.eclipse.papyrus.infra.core.resource.ModelException;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ModelsReader;
import org.eclipse.papyrus.infra.core.services.ExtensionServicesRegistry;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.uml.profile.UMLProfileHelper;
import org.eclipse.papyrus.uml.tools.indexer.service.UMLModelIndexerService;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;
import com.google.common.base.Stopwatch;
public class ProfileHelperMeasurements implements IApplication {
@Override
public Object start(IApplicationContext context) throws Exception {
ServicesRegistry registry = new ExtensionServicesRegistry();
registry.startServicesByClassKeys(ModelSet.class);
ModelSet modelSet = ServiceUtils.getInstance().getModelSet(registry);
// Instantiate a Model set
if (modelSet == null) {
modelSet = new ModelSet();
ModelSetServiceFactory.setServiceRegistry(modelSet, registry);
}
// System.out.println("Unindexed");
// measureUnindexed(registry, modelSet);
// System.out.println("Indexed during model load");
// measurePreindexed(registry, modelSet);
System.out.println("Indexed at a later point");
measureIndexed(registry, modelSet);
return Integer.valueOf(0);
}
private void measureIndexed(ServicesRegistry registry, ModelSet modelSet) throws ModelException, ServiceException {
// Read all Model from selected file
Stopwatch loadTime = Stopwatch.createStarted();
ModelsReader modelsReader = new ModelsReader();
modelsReader.readModel(modelSet);
modelSet.loadModels(URI.createPlatformPluginURI("org.eclipse.papyrus.uml.profile.measures/obfuscated.di", true));
loadTime.stop();
Stopwatch indexTime = Stopwatch.createStarted();
registry.startServicesByClassKeys(UMLModelIndexerService.class);
indexTime.stop();
Stopwatch traverseTime = Stopwatch.createStarted();
TreeIterator<Notifier> it = modelSet.getAllContents();
UMLProfileHelper helper = new UMLProfileHelper();
while(it.hasNext()) {
Notifier obj = it.next();
if (obj instanceof EObject) {
helper.getAppliedProfiles((EObject)obj);
}
}
traverseTime.stop();
System.out.printf("Model load: %d ms\n", loadTime.elapsed(TimeUnit.MILLISECONDS));
System.out.printf("Model index: %d ms\n", indexTime.elapsed(TimeUnit.MILLISECONDS));
System.out.printf("Model traverse: %d ms\n", traverseTime.elapsed(TimeUnit.MILLISECONDS));
}
private void measurePreindexed(ServicesRegistry registry, ModelSet modelSet) throws ModelException, ServiceException {
registry.startServicesByClassKeys(UMLModelIndexerService.class);
// Read all Model from selected file
Stopwatch loadTime = Stopwatch.createStarted();
ModelsReader modelsReader = new ModelsReader();
modelsReader.readModel(modelSet);
modelSet.loadModels(URI.createPlatformPluginURI("org.eclipse.papyrus.uml.profile.measures/obfuscated.di", true));
loadTime.stop();
Stopwatch traverseTime = Stopwatch.createStarted();
TreeIterator<Notifier> it = modelSet.getAllContents();
UMLProfileHelper helper = new UMLProfileHelper();
while(it.hasNext()) {
Notifier obj = it.next();
if (obj instanceof EObject) {
helper.getAppliedProfiles((EObject)obj);
}
}
traverseTime.stop();
System.out.printf("Model load: %d ms\n", loadTime.elapsed(TimeUnit.MILLISECONDS));
System.out.printf("Model traverse: %d ms\n", traverseTime.elapsed(TimeUnit.MILLISECONDS));
}
private void measureUnindexed(ServicesRegistry registry, ModelSet modelSet) throws ModelException, ServiceException {
// Read all Model from selected file
Stopwatch loadTime = Stopwatch.createStarted();
ModelsReader modelsReader = new ModelsReader();
modelsReader.readModel(modelSet);
modelSet.loadModels(URI.createPlatformPluginURI("org.eclipse.papyrus.uml.profile.measures/obfuscated.di", true));
loadTime.stop();
Stopwatch traverseTime = Stopwatch.createStarted();
TreeIterator<Notifier> it = modelSet.getAllContents();
while(it.hasNext()) {
Notifier obj = it.next();
if (obj instanceof EObject) {
getAppliedProfiles((EObject)obj);
}
}
traverseTime.stop();
System.out.printf("Model load: %d ms\n", loadTime.elapsed(TimeUnit.MILLISECONDS));
System.out.printf("Model traverse: %d ms\n", traverseTime.elapsed(TimeUnit.MILLISECONDS));
}
private Collection<EPackage> getAppliedProfiles(EObject model) {
ArrayList<EPackage> result = new ArrayList<EPackage>();
if (!(model instanceof Element)) {
return result;
}
Element element = (Element) model;
Package p = element.getNearestPackage();
if (p == null) {
return result;
}
for (Profile profile : p.getAllAppliedProfiles()) {
if (!result.contains(profile.getDefinition())) {
result.add(profile.getDefinition());
}
}
return result;
}
@Override
public void stop() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment