20 package com.mysql.cluster.crund;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.Arrays;
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;
31 import javax.persistence.PersistenceContextType;
44 protected String connectionRetainMode;
45 protected String brokerFactory;
46 protected String ndbConnectString;
47 protected String ndbDatabase;
50 protected EntityManagerFactory emf;
51 protected EntityManager em;
52 protected Query delAllA;
53 protected Query delAllB0;
59 protected void initProperties() {
60 super.initProperties();
62 out.print(
"setting jpa properties ...");
64 final StringBuilder
msg =
new StringBuilder();
65 final String eol = System.getProperty(
"line.separator");
68 driver = props.getProperty(
"openjpa.ConnectionDriverName");
70 throw new RuntimeException(
"Missing property: "
71 +
"openjpa.ConnectionDriverName");
74 Class.forName(driver);
75 }
catch (ClassNotFoundException e) {
76 out.println(
"Cannot load JDBC driver '" + driver
77 +
"' from classpath '"
78 + System.getProperty(
"java.class.path") +
"'");
79 throw new RuntimeException(e);
82 url = props.getProperty(
"openjpa.ConnectionURL");
84 throw new RuntimeException(
"Missing property: "
85 +
"openjpa.ConnectionURL");
88 user = props.getProperty(
"openjpa.ConnectionUserName");
89 password = props.getProperty(
"openjpa.ConnectionPassword");
91 connectionRetainMode = props.getProperty(
"openjpa.ConnectionRetainMode");
92 if (connectionRetainMode == null) {
93 throw new RuntimeException(
"Missing property: "
94 +
"openjpa.ConnectionRetainMode");
97 brokerFactory = props.getProperty(
"openjpa.BrokerFactory");
98 ndbConnectString = props.getProperty(
"openjpa.ndb.connectString");
99 ndbDatabase = props.getProperty(
"openjpa.ndb.database");
100 if (
"ndb".equals(brokerFactory)) {
101 if (ndbConnectString == null) {
102 throw new RuntimeException(
"Missing property: "
103 +
"openjpa.ndb.connectString");
105 if (ndbDatabase == null) {
106 throw new RuntimeException(
"Missing property: "
107 +
"openjpa.ndb.database");
111 if (msg.length() == 0) {
112 out.println(
" [ok]");
115 out.print(msg.toString());
119 final String c = (
"ndb".equals(brokerFactory)
120 ? (
"clusterj(" + ndbConnectString +
")")
122 descr =
"->jpa->" + c;
125 protected void printProperties() {
126 super.printProperties();
129 out.println(
"jpa settings ...");
130 out.println(
"openjpa.ConnectionDriverName: " + driver);
131 out.println(
"openjpa.ConnectionURL: " + url);
132 out.println(
"openjpa.ConnectionUserName: \"" + user +
"\"");
133 out.println(
"openjpa.ConnectionPassword: \"" + password +
"\"");
134 out.println(
"openjpa.ConnectionRetainMode: " + connectionRetainMode);
135 out.println(
"openjpa.BrokerFactory: " + brokerFactory);
136 out.println(
"openjpa.ndb.connectString: " + ndbConnectString);
137 out.println(
"openjpa.ndb.database: " + ndbDatabase);
140 protected void initLoad()
throws Exception {
145 out.print(
"creating JPA EMFactory ...");
148 emf = Persistence.createEntityManagerFactory(
"crundjpa", props);
149 out.println(
" [EMF: 1]");
152 protected void closeLoad()
throws Exception {
154 out.print(
"closing JPA EMFactory ...");
159 out.println(
" [ok]");
174 public void init() {}
176 public void close() {}
179 protected void setCommonFields(
A o,
int i) {
183 o.setCfloat((
float)i);
184 o.setCdouble((
double)i);
187 protected void setCommonFields(
B0 o,
int i) {
191 o.setCfloat((
float)i);
192 o.setCdouble((
double)i);
195 protected void verifyCommonFields(
A o,
int i) {
197 final int id = o.getId();
199 final int cint = o.getCint();
201 final long clong = o.getClong();
203 final float cfloat = o.getCfloat();
205 final double cdouble = o.getCdouble();
206 verify(cdouble == i);
209 protected void verifyCommonFields(
B0 o,
int i) {
211 final int id = o.getId();
213 final int cint = o.getCint();
215 final long clong = o.getClong();
217 final float cfloat = o.getCfloat();
219 final double cdouble = o.getCdouble();
220 verify(cdouble == i);
223 protected void initOperations() {
224 out.print(
"initializing operations ...");
229 public void run(
int nOps) {
231 for (
int i = 0; i < nOps; i++) {
242 public void run(
int nOps) {
244 for (
int i = 0; i < nOps; i++) {
245 final B0 o =
new B0();
254 new JpaOp(
"setAByPK_bulk") {
255 public void run(
int nOps) {
258 final int upd = em.createQuery(
"UPDATE A o SET o.cint = 0-(o.id), o.clong = 0-(o.id), o.cfloat = 0-(o.id), o.cdouble = 0-(o.id)").executeUpdate();
265 new JpaOp(
"setB0ByPK_bulk") {
266 public void run(
int nOps) {
269 final int upd = em.createQuery(
"UPDATE B0 o SET o.cint = 0-(o.id), o.clong = 0-(o.id), o.cfloat = 0-(o.id), o.cdouble = 0-(o.id)").executeUpdate();
276 new JpaOp(
"setAByPK") {
277 public void run(
int nOps) {
279 for (
int i = 0; i < nOps; i++) {
280 final A o = em.find(
A.class, i);
281 setCommonFields(o, i);
288 new JpaOp(
"setB0ByPK") {
289 public void run(
int nOps) {
291 for (
int i = 0; i < nOps; i++) {
292 final B0 o = em.find(
B0.class, i);
293 setCommonFields(o, i);
300 new JpaOp(
"getAByPK") {
301 public void run(
int nOps) {
303 for (
int i = 0; i < nOps; i++) {
304 final A o = em.find(
A.class, i);
305 verifyCommonFields(o, i);
312 new JpaOp(
"getB0ByPK") {
313 public void run(
int nOps) {
315 for (
int i = 0; i < nOps; i++) {
316 final B0 o = em.find(
B0.class, i);
317 verifyCommonFields(o, i);
323 for (
int i = 0, l = 1; l <= maxVarbinaryBytes; l *= 10, i++) {
324 final byte[] b = bytes[
i];
325 assert l == b.length;
328 new JpaOp(
"setVarbinary" + l) {
329 public void run(
int nOps) {
331 for (
int i = 0; i < nOps; i++) {
332 final B0 o = em.find(
B0.class, i);
335 o.setCvarbinary_def(b);
342 new JpaOp(
"getVarbinary" + l) {
343 public void run(
int nOps) {
345 for (
int i = 0; i < nOps; i++) {
346 final B0 o = em.find(
B0.class, i);
348 verify(Arrays.equals(b, o.getCvarbinary_def()));
355 new JpaOp(
"clearVarbinary" + l) {
356 public void run(
int nOps) {
358 for (
int i = 0; i < nOps; i++) {
359 final B0 o = em.find(
B0.class, i);
362 o.setCvarbinary_def(null);
369 for (
int i = 0, l = 1; l <= maxVarcharChars; l *= 10, i++) {
371 assert l == s.length();
374 new JpaOp(
"setVarchar" + l) {
375 public void run(
int nOps) {
377 for (
int i = 0; i < nOps; i++) {
378 final B0 o = em.find(
B0.class, i);
381 o.setCvarchar_def(s);
388 new JpaOp(
"getVarchar" + l) {
389 public void run(
int nOps) {
391 for (
int i = 0; i < nOps; i++) {
392 final B0 o = em.find(
B0.class, i);
394 verify(s.equals(o.getCvarchar_def()));
401 new JpaOp(
"clearVarchar" + l) {
402 public void run(
int nOps) {
404 for (
int i = 0; i < nOps; i++) {
405 final B0 o = em.find(
B0.class, i);
408 o.setCvarchar_def(null);
416 new JpaOp(
"setB0->A") {
417 public void run(
int nOps) {
419 for (
int i = 0; i < nOps; i++) {
420 final B0 b0 = em.find(
B0.class, i);
423 final A a = em.find(
A.class, aId);
432 new JpaOp(
"navB0->A") {
433 public void run(
int nOps) {
435 for (
int i = 0; i < nOps; i++) {
436 final B0 b0 = em.find(
B0.class, i);
438 final A a = b0.getA();
439 verifyCommonFields(a, i % nOps);
446 new JpaOp(
"navA->B0") {
447 public void run(
int nOps) {
449 for (
int i = 0; i < nOps; i++) {
450 final A a = em.find(
A.class, i);
452 final Collection<B0> b0s = a.getB0s();
454 verify(b0s.size() > 0);
456 verifyCommonFields(b0, i % nOps);
464 new JpaOp(
"nullB0->A") {
465 public void run(
int nOps) {
467 for (
int i = 0; i < nOps; i++) {
468 final B0 b0 = em.find(
B0.class, i);
485 final JpaOp setB0ToA = (JpaOp)ops.get(ops.size() - 4);
486 assert setB0ToA.getName().equals(
"setB0->A");
491 new JpaOp(
"nullB0->A_bulk") {
492 public void run(
int nOps) {
495 final int upd = em.createQuery(
"UPDATE B0 o SET o.a = NULL").executeUpdate();
502 new JpaOp(
"delB0ByPK") {
503 public void run(
int nOps) {
505 for (
int i = 0; i < nOps; i++) {
506 final B0 o = em.find(
B0.class, i);
515 new JpaOp(
"delAByPK") {
516 public void run(
int nOps) {
518 for (
int i = 0; i < nOps; i++) {
519 final A o = em.find(
A.class, i);
528 new JpaOp(
"insA_attr") {
529 public void run(
int nOps) {
531 for (
int i = 0; i < nOps; i++) {
534 setCommonFields(o, -i);
542 new JpaOp(
"insB0_attr") {
543 public void run(
int nOps) {
545 for (
int i = 0; i < nOps; i++) {
546 final B0 o =
new B0();
548 setCommonFields(o, -i);
556 new JpaOp(
"delAllB0") {
557 public void run(
int nOps) {
559 int del = em.createQuery(
"DELETE FROM B0").executeUpdate();
566 new JpaOp(
"delAllA") {
567 public void run(
int nOps) {
569 int del = em.createQuery(
"DELETE FROM A").executeUpdate();
576 for (Iterator<CrundDriver.Op> i = ops.iterator(); i.hasNext();) {
577 ((JpaOp)i.next()).init();
579 out.println(
" [JpaOp: " + ops.size() +
"]");
582 protected void closeOperations() {
583 out.print(
"closing operations ...");
587 for (Iterator<CrundDriver.Op> i = ops.iterator(); i.hasNext();) {
588 ((JpaOp)i.next()).close();
592 out.println(
" [ok]");
595 protected void beginTransaction() {
596 em.getTransaction().begin();
599 protected void commitTransaction() {
600 em.getTransaction().commit();
607 protected void initConnection() {
609 out.print(
"creating JPA EntityManager ...");
614 em = emf.createEntityManager();
624 delAllA = em.createQuery(
"DELETE FROM A");
625 delAllB0 = em.createQuery(
"DELETE FROM B0");
626 out.println(
" [EM: 1]");
629 protected void closeConnection() {
631 out.print(
"closing JPA EntityManager ...");
638 out.println(
" [ok]");
641 protected void clearPersistenceContext() {
650 protected void clearData() {
651 out.print(
"deleting all objects ...");
654 em.getTransaction().begin();
655 int delB0 = delAllB0.executeUpdate();
656 out.print(
" [B0: " + delB0);
658 int delA = delAllA.executeUpdate();
659 out.print(
", A: " + delA);
661 em.getTransaction().commit();
669 @SuppressWarnings(
"unchecked")
670 static public
void main(
String[] args) {
671 System.out.println(
"JpaLoad.main()");
674 System.out.println();
675 System.out.println(
"JpaLoad.main(): done.");