| /******************************************************************************* | |
| * 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.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Random; | |
| import javax.persistence.EntityManager; | |
| import javax.persistence.EntityManagerFactory; | |
| import javax.persistence.Persistence; | |
| import model.Customer; | |
| import model.Order; | |
| import model.OrderLine; | |
| /** | |
| * Test the performance of reading an Order object model. | |
| * @author James Sutherland | |
| */ | |
| public abstract class Test extends PerformanceTest { | |
| public static int SIZE = 10000; | |
| long[] orderIds = new long[SIZE]; | |
| long[] customerIds = new long[SIZE/10]; | |
| Random random = new Random(); | |
| Map<String, EntityManagerFactory> factories = new HashMap<String, EntityManagerFactory>(); | |
| public void setup() { | |
| System.out.println("Begin setup"); | |
| EntityManagerFactory factory = Persistence.createEntityManagerFactory("database"); | |
| EntityManager em = factory.createEntityManager(); | |
| em.getTransaction().begin(); | |
| em.createQuery("Delete from OrderLine").executeUpdate(); | |
| em.createQuery("Delete from Order").executeUpdate(); | |
| em.createQuery("Delete from Customer").executeUpdate(); | |
| em.getTransaction().commit(); | |
| em.close(); | |
| populate(factory); | |
| this.factories.put("database", factory); | |
| factory = Persistence.createEntityManagerFactory("cache"); | |
| factory.createEntityManager().close();; | |
| this.factories.put("cache", factory); | |
| factory = Persistence.createEntityManagerFactory("protected"); | |
| factory.createEntityManager().close();; | |
| this.factories.put("protected", factory); | |
| factory = Persistence.createEntityManagerFactory("data-cache"); | |
| factory.createEntityManager().close();; | |
| this.factories.put("data-cache", factory); | |
| factory = Persistence.createEntityManagerFactory("read-only"); | |
| factory.createEntityManager().close();; | |
| this.factories.put("read-only", factory); | |
| factory = Persistence.createEntityManagerFactory("read-only-protected"); | |
| factory.createEntityManager().close();; | |
| this.factories.put("read-only-protected", factory); | |
| factory = Persistence.createEntityManagerFactory("query-cache"); | |
| factory.createEntityManager().close();; | |
| this.factories.put("query-cache", factory); | |
| factory = Persistence.createEntityManagerFactory("db-query-cache"); | |
| factory.createEntityManager().close();; | |
| this.factories.put("db-query-cache", factory); | |
| factory = Persistence.createEntityManagerFactory("in-memory"); | |
| // ** comment this persistence unit out for very large database sizes (can't load all into memory). | |
| /*em = factory.createEntityManager(); | |
| List<Order> results = em.createQuery("Select o from Order o").getResultList(); | |
| for (Order result : results) { | |
| result.getCustomer().toString(); | |
| result.getOrderLines().size(); | |
| } | |
| em.close();*/ | |
| this.factories.put("in-memory", factory); | |
| } | |
| public void populate(EntityManagerFactory factory) { | |
| EntityManager em = factory.createEntityManager(); | |
| int index = 0; | |
| for (int customerIndex = 0; customerIndex < (SIZE /10); customerIndex++) { | |
| em.getTransaction().begin(); | |
| Customer customer = new Customer(); | |
| customer.setName("Customer-" + customerIndex); | |
| em.persist(customer); | |
| this.customerIds[customerIndex] = customer.getId(); | |
| for (int orderIndex = 0; orderIndex < 10; orderIndex++) { | |
| Order order = new Order(); | |
| order.setDescription("Order-" + index); | |
| order.setCustomer(customer); | |
| order.addOrderLine(new OrderLine("line1", new BigDecimal(10))); | |
| order.addOrderLine(new OrderLine("line2", new BigDecimal(5))); | |
| order.addOrderLine(new OrderLine("line3", new BigDecimal(99))); | |
| em.persist(order); | |
| this.orderIds[index] = order.getId(); | |
| index++; | |
| } | |
| em.getTransaction().commit(); | |
| em.clear(); | |
| System.out.println("Insert batch - " + customerIndex); | |
| } | |
| em.close(); | |
| } | |
| public void test() { | |
| this.executeRun("Database", new Runnable() { | |
| public void run() { | |
| runTest(factories.get("database")); | |
| } | |
| }); | |
| System.out.println("Begin benchmark"); | |
| this.executeRun("DBQueryCache", new Runnable() { | |
| public void run() { | |
| runTest(factories.get("db-query-cache")); | |
| } | |
| }); | |
| this.executeRun("Cache", new Runnable() { | |
| public void run() { | |
| runTest(factories.get("cache")); | |
| } | |
| }); | |
| this.executeRun("Protected", new Runnable() { | |
| public void run() { | |
| runTest(factories.get("protected")); | |
| } | |
| }); | |
| this.executeRun("DataCache", new Runnable() { | |
| public void run() { | |
| runTest(factories.get("data-cache")); | |
| } | |
| }); | |
| this.executeRun("ReadOnly", new Runnable() { | |
| public void run() { | |
| runTest(factories.get("read-only")); | |
| } | |
| }); | |
| this.executeRun("ProtectedReadOnly", new Runnable() { | |
| public void run() { | |
| runTest(factories.get("read-only-protected")); | |
| } | |
| }); | |
| this.executeRun("QueryCache", new Runnable() { | |
| public void run() { | |
| runTest(factories.get("query-cache")); | |
| } | |
| }); | |
| this.executeRun("InMemoryCache", new Runnable() { | |
| public void run() { | |
| runTest(factories.get("in-memory")); | |
| } | |
| }); | |
| System.out.println("End benchmark"); | |
| } | |
| public abstract void runTest(EntityManagerFactory factory); | |
| } |