forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockPriceCreateJPA.java
More file actions
117 lines (108 loc) · 4.57 KB
/
Copy pathStockPriceCreateJPA.java
File metadata and controls
117 lines (108 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
package net.sdo;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import net.sdo.stock.StockPrice;
import net.sdo.stock.StockPriceUtils;
import net.sdo.stockimpl.MockStockPriceEntityManagerFactory;
import net.sdo.stockimpl.StockOptionPK;
import net.sdo.stockimpl.StockOptionPriceEagerLazyImpl;
import net.sdo.stockimpl.StockPriceEagerLazyImpl;
import net.sdo.stockimpl.StockPricePK;
public class StockPriceCreateJPA extends Thread {
private static final NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
private static long msPerDay = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
private static DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
private static Date startDate;
private static Date endDate;
private static EntityManager mockEM = new MockStockPriceEntityManagerFactory("MockEntityManager").createEntityManager();
private static int nTransactions = 1;
private static boolean reuseEM;
private static boolean reuseTX;
private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("StockPU");
private int startStock;
private int numStocks;
public StockPriceCreateJPA(int start, int num) {
startStock = start;
numStocks = num;
}
@Override
public void run() {
try {
EntityManager em = emf.createEntityManager();
EntityTransaction txn = em.getTransaction();
txn.begin();
for (int i = startStock; i < numStocks; i++) {
String symbol = StockPriceUtils.makeSymbol(i);
Date curDate = new Date(startDate.getTime());
System.out.println(Thread.currentThread() + ": Processing " + i + ": " + symbol);
while (!curDate.after(endDate)) {
StockPrice sp = mockEM.find(StockPriceEagerLazyImpl.class, new StockPricePK(symbol, (Date) curDate.clone()));
if (sp != null) {
em.persist(sp);
for (int j = 0; j < 5; j++) {
StockOptionPriceEagerLazyImpl sop = new StockOptionPriceEagerLazyImpl();
sop.setId(new StockOptionPK(sp.getSymbol(), sp.getDate(), j));
sop.setPrice(sp.getClosingPrice().multiply(new BigDecimal(1 + j / 100.)));
em.persist(sop);
}
}
curDate.setTime(curDate.getTime() + msPerDay);
}
if ((i % nTransactions) == nTransactions - 1) {
txn.commit();
if (!reuseEM) {
em.close();
em = emf.createEntityManager();
txn = em.getTransaction();
}
if (reuseEM && !reuseTX) {
txn = em.getTransaction();
}
txn.begin();
}
}
txn.commit();
em.close();
System.out.println(Thread.currentThread() + ": Complete");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws ParseException {
int curArg = 0;
int nThreads = Integer.parseInt(args[curArg++]);
int num = Integer.parseInt(args[curArg++]);
int numPerThread = num / nThreads;
startDate = df.parse(args[curArg++]);
endDate = df.parse(args[curArg++]);
nTransactions = Integer.parseInt(args[curArg++]);
reuseEM = Boolean.parseBoolean(args[curArg++]);
reuseTX = Boolean.parseBoolean(args[curArg++]);
StockPriceCreateJPA[] threads = new StockPriceCreateJPA[nThreads];
for (int i = 0; i < nThreads; i++) {
threads[i] = new StockPriceCreateJPA(numPerThread * i, numPerThread * i + numPerThread);
}
for (int i = 0; i < nThreads; i++) {
threads[i].start();
}
for (int i = 0; i < nThreads; i++) {
try {
threads[i].join();
} catch (InterruptedException ex) {
System.err.println("Thread join interrupted");
}
}
}
}