| /******************************************************************************* |
| * Copyright (c) 1998, 2012 Oracle and/or its affiliates. All rights reserved. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
| * which accompanies this distribution. |
| * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| * and the Eclipse Distribution License is available at |
| * http://www.eclipse.org/org/documents/edl-v10.php. |
| * |
| * Contributors: |
| * Oracle - initial impl |
| ******************************************************************************/ |
| package example; |
| |
| import java.math.BigDecimal; |
| import java.util.List; |
| import java.util.Random; |
| |
| import javax.persistence.EntityManager; |
| import javax.persistence.EntityManagerFactory; |
| import javax.persistence.Persistence; |
| import javax.persistence.Query; |
| |
| import model.Customer; |
| import model.Order; |
| import model.OrderLine; |
| |
| /** |
| * Test the performance of a mix of crud+q operations on an Order object model. |
| * @author James Sutherland |
| */ |
| public class Test extends PerformanceTest { |
| public static double INSERT = 0.2; |
| public static double UPDATE = 0.1; |
| public static double FIND = 0.4; |
| public static double QUERY = 0.3; |
| |
| public static int SIZE = 100; |
| public static int ORDER_PER_CUSTOMER_SIZE = 10; |
| public static int ORDER_LINES_SIZE = 5; |
| |
| long firstOrderId; |
| long firstCustomerId; |
| Random random = new Random(); |
| EntityManagerFactory factory; |
| |
| //Map junk = new HashMap(); |
| |
| public static void main(String[] args) throws Exception { |
| try { |
| Test test = new Test(); |
| test.setup(); |
| test.test(); |
| } catch (Exception error) { |
| error.printStackTrace(); |
| } |
| System.exit(0); |
| } |
| |
| public void setup() { |
| /*for (int count = 0; count < 10; count++) { |
| junk.put(new XMLEntityMappingsMappingProject("orm", "xsd"), new XMLEntityMappingsMappingProject("orm", "xsd")); |
| }*/ |
| |
| System.out.println("Begin setup"); |
| this.factory = Persistence.createEntityManagerFactory("benchmark"); |
| EntityManager em = factory.createEntityManager(); |
| em.getTransaction().begin(); |
| Order order = null; |
| em.createQuery("Delete from OrderLine").executeUpdate(); |
| em.createQuery("Delete from Order").executeUpdate(); |
| em.createQuery("Delete from Customer").executeUpdate(); |
| em.getTransaction().commit(); |
| em.close(); |
| em = factory.createEntityManager(); |
| em.getTransaction().begin(); |
| int index = 0; |
| int batchIndex = 0; |
| for (int customerIndex = 0; customerIndex < (SIZE / ORDER_PER_CUSTOMER_SIZE); customerIndex++) { |
| Customer customer = new Customer(); |
| customer.setName("Customer-" + index); |
| em.persist(customer); |
| if (this.firstCustomerId == 0) { |
| this.firstCustomerId = customer.getId(); |
| } |
| for (int orderIndex = 0; orderIndex < ORDER_PER_CUSTOMER_SIZE; orderIndex++) { |
| order = new Order(); |
| order.setDescription("Order-" + index); |
| order.setCustomer(customer); |
| for (int orderLineIndex = 0; orderLineIndex < ORDER_LINES_SIZE; orderLineIndex++) { |
| order.addOrderLine(new OrderLine("line" + orderLineIndex, new BigDecimal(10))); |
| } |
| em.persist(order); |
| if (this.firstOrderId == 0) { |
| this.firstOrderId = order.getId(); |
| } |
| index++; |
| } |
| batchIndex++; |
| if (batchIndex > 10) { |
| em.getTransaction().commit(); |
| em.close(); |
| em = factory.createEntityManager(); |
| em.getTransaction().begin(); |
| batchIndex = 0; |
| } |
| } |
| em.getTransaction().commit(); |
| em.close(); |
| } |
| |
| public void test() { |
| System.out.println("Begin benchmark"); |
| this.executeRun("benchmark", new Runnable() { |
| public void run() { |
| if (random.nextFloat() <= INSERT) { |
| runInsert(factory); |
| } |
| if (random.nextFloat() <= FIND) { |
| runFind(factory); |
| } |
| if (random.nextFloat() <= UPDATE) { |
| runUpdate(factory); |
| } |
| if (random.nextFloat() <= QUERY) { |
| runQuery(factory); |
| } |
| } |
| }); |
| System.out.println("End benchmark"); |
| } |
| |
| public void reset() { |
| //this.factory.createEntityManager().unwrap(Session.class).setProfiler(new PerformanceMonitor()); |
| } |
| |
| /** Query for n orders by customer id. */ |
| public void runQuery(EntityManagerFactory factory) { |
| EntityManager em = factory.createEntityManager(); |
| Query query = em.createNamedQuery("findOrdersByCustomer"); |
| long id = this.firstCustomerId + this.random.nextInt(SIZE/ORDER_PER_CUSTOMER_SIZE); |
| query.setParameter("customerId", id); |
| List<Order> result = query.getResultList(); |
| for (Order order : result) { |
| order.getCustomer().toString(); |
| order.getOrderLines().size(); |
| } |
| em.close(); |
| } |
| |
| /** Perform a find by id. */ |
| public void runFind(EntityManagerFactory factory) { |
| EntityManager em = factory.createEntityManager(); |
| long id = this.firstOrderId + this.random.nextInt(SIZE); |
| Order order = em.find(Order.class, id); |
| order.getCustomer().toString(); |
| order.getOrderLines().size(); |
| em.close(); |
| } |
| |
| /** Perform an insert. */ |
| public void runInsert(EntityManagerFactory factory) { |
| EntityManager em = factory.createEntityManager(); |
| em.getTransaction().begin(); |
| Customer customer = new Customer(); |
| customer.setName("Customer" + customer.hashCode()); |
| em.persist(customer); |
| Order order = new Order(); |
| order.setDescription("Order-" + order.hashCode()); |
| order.setCustomer(customer); |
| for (int orderLineIndex = 0; orderLineIndex < ORDER_LINES_SIZE; orderLineIndex++) { |
| order.addOrderLine(new OrderLine("line" + orderLineIndex, new BigDecimal(10))); |
| } |
| em.persist(order); |
| em.getTransaction().commit(); |
| em.close(); |
| } |
| |
| /** Perform an update. */ |
| public void runUpdate(EntityManagerFactory factory) { |
| EntityManager em = factory.createEntityManager(); |
| em.getTransaction().begin(); |
| long id = this.firstOrderId + this.random.nextInt(SIZE); |
| Order order = em.find(Order.class, id); |
| order.getCustomer().toString(); |
| int size = order.getOrderLines().size(); |
| if (size > ORDER_LINES_SIZE) { |
| order.removeOrderLine(order.getOrderLines().get(size - 1)); |
| } else { |
| order.addOrderLine(new OrderLine("line" + (size + 1), new BigDecimal(5))); |
| } |
| em.getTransaction().commit(); |
| em.close(); |
| } |
| } |