| package org.eclipse.papyrus.cdo.uml.benchmarks.creation.tests; |
| |
| import java.util.Properties; |
| |
| import org.eclipse.emf.cdo.common.branch.CDOBranch; |
| import org.eclipse.emf.cdo.common.branch.CDOBranchPoint; |
| import org.eclipse.emf.cdo.explorer.CDOExplorerUtil; |
| import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckout; |
| import org.eclipse.emf.cdo.explorer.repositories.CDORepository; |
| import org.eclipse.emf.cdo.internal.explorer.AbstractElement; |
| import org.eclipse.emf.cdo.internal.explorer.checkouts.CDOCheckoutImpl; |
| import org.eclipse.emf.cdo.internal.explorer.repositories.CDORepositoryImpl; |
| import org.eclipse.emf.cdo.internal.explorer.repositories.RemoteCDORepository; |
| import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration; |
| import org.eclipse.emf.cdo.net4j.CDONet4jUtil; |
| import org.eclipse.emf.cdo.session.CDOSession; |
| import org.eclipse.emf.cdo.transaction.CDOTransaction; |
| import org.eclipse.emf.cdo.util.CommitException; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.net4j.Net4jUtil; |
| import org.eclipse.net4j.connector.IConnector; |
| import org.eclipse.net4j.tcp.TCPUtil; |
| import org.eclipse.net4j.util.container.ContainerUtil; |
| import org.eclipse.net4j.util.container.IManagedContainer; |
| import org.eclipse.net4j.util.security.IPasswordCredentialsProvider; |
| import org.eclipse.net4j.util.security.PasswordCredentialsProvider; |
| import org.eclipse.uml2.uml.UMLFactory; |
| import org.eclipse.uml2.uml.UMLPackage; |
| import org.junit.Assert; |
| import org.junit.Test; |
| |
| //import RequirementWithEMF.Constants; |
| |
| public class CDORemoteTests { |
| |
| @Test |
| public void create_10000_Elements() { |
| create_Elements(10000); |
| } |
| |
| @Test |
| public void create_50000_Elements() { |
| create_Elements(50000); |
| } |
| |
| // @Test |
| public void create_500000_Elements() { |
| create_Elements(500000); |
| } |
| |
| // @Test |
| public void create_1000000_Elements() { |
| create_Elements(1000000); |
| } |
| |
| |
| public void create_Elements(int size) { |
| System.out.println("Creation in Remote CDO file"); |
| System.out.println("create " + size + " elements with the factory " + UMLFactory.eINSTANCE.getClass().getName()); |
| String fileName = size + "_Elements"; |
| String timsestamp = Long.toString(System.currentTimeMillis()); |
| fileName = fileName + "_" + timsestamp; |
| |
| //1. CDO connection |
| try { |
| connect(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| |
| //2 CDO transaction |
| CDOTransaction transaction = getSession().openTransaction(); |
| |
| //3. create CDO resource |
| Resource myResource = transaction.getOrCreateResource("/home/Administrator/" + fileName); |
| |
| |
| long start = System.currentTimeMillis(); |
| |
| // 1. create the root package |
| org.eclipse.uml2.uml.Package rootPackage = UMLFactory.eINSTANCE.createPackage(); |
| rootPackage.setName("RootPackage_" + fileName); |
| myResource.getContents().add(rootPackage); |
| |
| // 2. create the elements |
| for (long i = 0; i < size; i++) { |
| org.eclipse.uml2.uml.Class aclass = UMLFactory.eINSTANCE.createClass(); |
| final StringBuilder builder = new StringBuilder("Class_"); |
| builder.append(i); |
| aclass.setName(builder.toString()); |
| rootPackage.getPackagedElements().add(aclass); |
| } |
| |
| long end = System.currentTimeMillis(); |
| System.out.println("+ " + size + " creations WITH UML done: " + (end - start) + " ms "); |
| |
| ///////////////////////////////// SAVE////////////////////////////////////////////////////// |
| start = System.currentTimeMillis(); |
| |
| try { |
| transaction.commit(); |
| } catch (CommitException e1) { |
| // TODO Auto-generated catch block |
| e1.printStackTrace(); |
| } |
| transaction.close(); |
| getSession().close(); |
| |
| |
| end = System.currentTimeMillis(); |
| System.out.println("+ save " + size + " elements with EMF: " + (end - start) + " ms"); |
| |
| |
| // LOAD in a new ResourceSet |
| try { |
| connect(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| |
| //2 CDO transaction |
| CDOTransaction transaction2 = getSession().openTransaction(); |
| |
| //3. create CDO resource |
| start = System.currentTimeMillis(); |
| Resource resource2 = transaction2.getResource("/home/Administrator/" + fileName); |
| org.eclipse.uml2.uml.Package p = (org.eclipse.uml2.uml.Package) resource2.getContents().get(0); |
| end = System.currentTimeMillis(); |
| Assert.assertNotNull(p); |
| System.out.println("+ load " + size + " elements with EMF, then get root: " + (end - start) + " ms"); |
| System.out.println("\n\n\n"); |
| |
| try { |
| transaction2.commit(); |
| } catch (CommitException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } |
| transaction2.close(); |
| getSession().close(); |
| } |
| |
| |
| //==================CDO METHODS AND FIELDS============================ |
| |
| // CDO fields and methods |
| private static final String HOST_AND_PORT = "localhost:2036"; |
| private static final String USER_NAME = "Administrator"; |
| private static final String PASSWORD = "0000"; |
| final String PAPYRUS_CDO_SECURED_REPOSITORY = "PapyrusCDOSecuredRepository"; |
| |
| |
| private CDOSession session; |
| private CDOCheckout checkout; |
| private CDORepository repository; |
| private CDOTransaction transaction; |
| |
| |
| public final void connect() throws Exception { |
| connect(getHostAndPort(), getUser(), getPassword()); |
| } |
| |
| /** |
| * @return |
| */ |
| private String getPassword() { |
| return PASSWORD; |
| } |
| |
| /** |
| * @return |
| */ |
| private String getUser() { |
| return USER_NAME; |
| } |
| |
| /** |
| * @return |
| */ |
| private String getHostAndPort() { |
| return HOST_AND_PORT; |
| } |
| |
| private void connect(final String serverURI, final String login, final String password) throws Exception { |
| try { |
| // Prepare container |
| final IManagedContainer container = ContainerUtil.createContainer(); |
| Net4jUtil.prepareContainer(container); // Register Net4j factories |
| TCPUtil.prepareContainer(container); // Register TCP factories |
| CDONet4jUtil.prepareContainer(container); // Register CDO factories |
| container.activate(); |
| |
| // Create connector |
| final IConnector connector = TCPUtil.getConnector(container, serverURI); |
| |
| // Create configuration |
| final CDONet4jSessionConfiguration configuration = CDONet4jUtil.createNet4jSessionConfiguration(); |
| configuration.setConnector(connector); |
| configuration.setRepositoryName(PAPYRUS_CDO_SECURED_REPOSITORY); |
| final IPasswordCredentialsProvider credentialsProvider = new PasswordCredentialsProvider(login, password); |
| configuration.setCredentialsProvider(credentialsProvider); |
| |
| final Properties properties = new Properties(); |
| |
| properties.setProperty(AbstractElement.PROP_TYPE, CDORepository.TYPE_REMOTE); |
| properties.setProperty(AbstractElement.PROP_LABEL, PAPYRUS_CDO_SECURED_REPOSITORY); |
| properties.setProperty(CDORepositoryImpl.PROP_NAME, PAPYRUS_CDO_SECURED_REPOSITORY); |
| properties.setProperty(RemoteCDORepository.PROP_CONNECTOR_TYPE, "tcp"); //$NON-NLS-1$ |
| // TODO : is it serverURI? |
| properties.setProperty(RemoteCDORepository.PROP_CONNECTOR_DESCRIPTION, serverURI); |
| |
| repository = CDOExplorerUtil.getRepositoryManager().addRepository(properties, credentialsProvider.getCredentials()); |
| repository.connect(); |
| |
| // Open session |
| session = configuration.openNet4jSession(); |
| |
| // these informations have no impact on the performance |
| session.getPackageRegistry().putEPackage(UMLPackage.eINSTANCE); |
| // session.getPackageRegistry().putEPackage(sysmlPackage.eINSTANCE); |
| |
| this.checkout = createCheckout("PapyrusPerfoTestCheckout" + Long.toString(System.currentTimeMillis())); //$NON-NLS-1$ |
| |
| |
| } catch (final Exception e) { |
| throw e; |
| } |
| |
| } |
| |
| private CDOCheckout createCheckout(final String checkoutName) { |
| final Properties properties = new Properties(); |
| properties.setProperty(AbstractElement.PROP_TYPE, CDOCheckout.TYPE_ONLINE_HISTORICAL); |
| properties.setProperty(AbstractElement.PROP_LABEL, checkoutName); |
| properties.setProperty(CDOCheckoutImpl.PROP_REPOSITORY, this.repository.getID()); |
| properties.setProperty(CDOCheckoutImpl.PROP_BRANCH_ID, Integer.toString(CDOBranch.MAIN_BRANCH_ID)); |
| properties.setProperty(CDOCheckoutImpl.PROP_TIME_STAMP, Long.toString(CDOBranchPoint.UNSPECIFIED_DATE)); |
| properties.setProperty(CDOCheckoutImpl.PROP_READ_ONLY, Boolean.toString(false)); |
| |
| final CDOCheckout checkout = CDOExplorerUtil.getCheckoutManager().addCheckout(properties); |
| checkout.open(); |
| return checkout; |
| } |
| |
| /** |
| * Gets the running session. |
| * |
| * @return the running session |
| */ |
| protected CDOSession getSession() { |
| return this.session; |
| } |
| |
| |
| |
| public void closeTransaction() { |
| if (null != this.transaction) { |
| this.transaction.close(); |
| } |
| transaction = null;// TODO : probably missing in QS |
| } |
| |
| |
| |
| } |