2 Copyright 2010 Sun Microsystems, Inc.
3 All rights reserved. Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package com.mysql.clusterj.jpatest;
21 import com.mysql.clusterj.jpatest.model.A;
22 import com.mysql.clusterj.jpatest.model.B0;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.ArrayList;
27 import javax.persistence.Persistence;
28 import javax.persistence.EntityManagerFactory;
29 import javax.persistence.EntityManager;
30 import javax.persistence.Query;
33 * A benchmark implementation against a JPA-mapped database.
35 public class JpaLoad extends Driver {
37 // JPA database connection
38 protected String driver;
40 protected EntityManagerFactory emf;
41 protected EntityManager em;
42 protected Query delAllA;
43 protected Query delAllB0;
45 protected abstract class JpaOp extends Op {
46 public JpaOp(String name) {
52 public void close() {}
56 protected void initProperties() {
57 super.initProperties();
59 // load the JDBC driver class
60 driver = props.getProperty("openjpa.ConnectionDriverName");
62 throw new RuntimeException("Missing property: "
63 + "openjpa.ConnectionDriverName");
66 Class.forName(driver);
67 } catch (ClassNotFoundException e) {
68 out.println("Cannot load JDBC driver '" + driver
69 + "' from classpath '"
70 + System.getProperty("java.class.path") + "'");
71 throw new RuntimeException(e);
74 url = props.getProperty("openjpa.ConnectionURL");
76 throw new RuntimeException("Missing property: "
77 + "openjpa.ConnectionURL");
80 descr = "->JPA->" + url;
84 protected void printProperties() {
85 super.printProperties();
86 out.println("openjpa.ConnectionDriverName: " + driver);
87 out.println("openjpa.ConnectionURL: " + url);
91 protected void init() throws Exception {
94 out.print("creating EMFactory ...");
96 emf = Persistence.createEntityManagerFactory("crundjpa", props);
97 out.println(" [EMF: 1]");
101 protected void close() throws Exception {
102 out.print("closing EMFactory ...");
107 out.println(" [ok]");
111 protected void initConnection() {
112 out.print("creating EntityManager ...");
114 em = emf.createEntityManager();
115 // not supported by, at least, OpenJPA (JPA spec? Hibernate?):
116 //em = emf.createEntityManager(PersistenceContextType.TRANSACTION);
117 // It seems that the only way to obtain an EntityManager with
118 // transaction persistence context is by container injection.
120 // However, if/where/when tx-PC is supported, we need to check here
121 // against property allowExtendedPC; no need then for em.clear()
122 // in clearPersistenceContext()
123 delAllA = em.createQuery("DELETE FROM A");
124 delAllB0 = em.createQuery("DELETE FROM B0");
125 out.println(" [EM: 1]");
128 protected void closeConnection() {
129 out.print("closing EntityManager ...");
136 out.println(" [ok]");
139 protected int checkFields(A o) {
140 final int cint = o.getCint();
141 final long clong = o.getClong();
142 verify(clong == cint);
143 final float cfloat = o.getCfloat();
144 verify(cfloat == cint);
145 final double cdouble = o.getCdouble();
146 verify(cdouble == cint);
150 protected int checkFields(B0 o) {
151 final int cint = o.getCint();
152 final long clong = o.getClong();
153 verify(clong == cint);
154 final float cfloat = o.getCfloat();
155 verify(cfloat == cint);
156 final double cdouble = o.getCdouble();
157 verify(cdouble == cint);
161 protected void initOperations() {
162 out.print("initializing operations ...");
167 public void run(int countA, int countB) {
168 for (int i = 0; i < countA; i++) {
178 public void run(int countA, int countB) {
179 for (int i = 0; i < countB; i++) {
180 final B0 o = new B0();
188 new JpaOp("setAByPK") {
189 public void run(int countA, int countB) {
190 for (int i = 0; i < countA; i++) {
191 final A o = em.find(A.class, i);
195 o.setCfloat((float)i);
196 o.setCdouble((double)i);
202 new JpaOp("setB0ByPK") {
203 public void run(int countA, int countB) {
204 for (int i = 0; i < countB; i++) {
205 final B0 o = em.find(B0.class, i);
209 o.setCfloat((float)i);
210 o.setCdouble((double)i);
216 new JpaOp("getAByPK") {
217 public void run(int countA, int countB) {
218 for (int i = 0; i < countA; i++) {
219 final A o = em.find(A.class, i);
221 final int id = o.getId();
223 final int j = checkFields(o);
230 new JpaOp("getB0ByPK") {
231 public void run(int countA, int countB) {
232 for (int i = 0; i < countB; i++) {
233 final B0 o = em.find(B0.class, i);
235 final int id = o.getId();
237 final int j = checkFields(o);
244 new JpaOp("setB0->A") {
245 public void run(int countA, int countB) {
246 for (int i = 0; i < countB; i++) {
247 final B0 b0 = em.find(B0.class, i);
249 int aId = i % countA;
250 final A a = em.find(A.class, aId);
258 new JpaOp("navB0->A") {
259 public void run(int countA, int countB) {
260 for (int i = 0; i < countB; i++) {
261 final B0 b0 = em.find(B0.class, i);
263 final A a = b0.getA();
265 final int id = a.getId();
266 verify(id == i % countA);
267 final int j = checkFields(a);
274 new JpaOp("nullB0->A") {
275 public void run(int countA, int countB) {
276 for (int i = 0; i < countB; i++) {
277 final B0 b0 = em.find(B0.class, i);
285 new JpaOp("setA->B0") {
286 public void run(int countA, int countB) {
287 for (int i = 0; i < countA; i++) {
288 final A a = em.find(A.class, i);
290 final Collection<B0> b0s = new ArrayList<B0>();
291 for (int j = i; j < countB; j += countA) {
292 //assert i == j % countA;
293 final B0 b0 = em.find(B0.class, j);
302 // this operation fails on an empty PersistenceContext
303 if (allowExtendedPC) {
305 new JpaOp("navA->B0") {
306 public void run(int countA, int countB) {
307 for (int i = 0; i < countA; i++) {
308 final A a = em.find(A.class, i);
310 final Collection<B0> b0s = a.getB0s();
312 // fails on an empty PC (no managed relationships)
313 verify(b0s.size() > 0);
316 final int id = b0.getId();
317 verify(id % countA == i);
318 final int j = checkFields(b0);
327 new JpaOp("nullA->B0") {
328 public void run(int countA, int countB) {
329 for (int i = 0; i < countA; i++) {
330 final A a = em.find(A.class, i);
338 new JpaOp("delB0ByPK") {
339 public void run(int countA, int countB) {
340 for (int i = 0; i < countB; i++) {
341 final B0 o = em.find(B0.class, i);
349 new JpaOp("delAByPK") {
350 public void run(int countA, int countB) {
351 for (int i = 0; i < countA; i++) {
352 final A o = em.find(A.class, i);
360 new JpaOp("insA_attr") {
361 public void run(int countA, int countB) {
362 for (int i = 0; i < countA; i++) {
366 o.setClong((long)-i);
367 o.setCfloat((float)-i);
368 o.setCdouble((double)-i);
369 o.setCstring(String.valueOf(i));
376 new JpaOp("insB0_attr") {
377 public void run(int countA, int countB) {
378 for (int i = 0; i < countB; i++) {
379 final B0 o = new B0();
382 o.setClong((long)-i);
383 o.setCfloat((float)-i);
384 o.setCdouble((double)-i);
385 o.setCstring(String.valueOf(i));
392 new JpaOp("delAllB0") {
393 public void run(int countA, int countB) {
394 int del = em.createQuery("DELETE FROM B0").executeUpdate();
395 assert del == countB;
400 new JpaOp("delAllA") {
401 public void run(int countA, int countB) {
402 int del = em.createQuery("DELETE FROM A").executeUpdate();
403 assert del == countA;
408 for (Iterator<Driver.Op> i = ops.iterator(); i.hasNext();) {
409 ((JpaOp)i.next()).init();
411 out.println(" [JpaOp: " + ops.size() + "]");
414 protected void closeOperations() {
415 out.print("closing operations ...");
419 for (Iterator<Driver.Op> i = ops.iterator(); i.hasNext();) {
420 ((JpaOp)i.next()).close();
424 out.println(" [ok]");
427 protected void beginTransaction() {
428 em.getTransaction().begin();
431 protected void commitTransaction() {
432 em.getTransaction().commit();
435 protected void rollbackTransaction() {
436 em.getTransaction().rollback();
439 protected void clearPersistenceContext() {
440 // as long as we haven't created the EM with a Tx PC scope
441 // (i.e. emf.createEntityManager(PersistenceContextType.TRANSACTION))
442 // we effectively prevent caching beyond Tx scope by clearing
447 protected void clearData() {
448 out.print("deleting all objects ...");
451 em.getTransaction().begin();
452 int delB0 = delAllB0.executeUpdate();
453 out.print(" [B0: " + delB0);
455 int delA = delAllA.executeUpdate();
456 out.print(", A: " + delA);
458 em.getTransaction().commit();
464 // ----------------------------------------------------------------------
466 static public void main(String[] args) {
467 clearPropFileNames();
468 System.out.println("JpaLoad.main()");
469 parseArguments(args);
471 System.out.println();
472 System.out.println("JpaLoad.main(): done.");