forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockPriceReadJPA.java
More file actions
187 lines (176 loc) · 6.64 KB
/
Copy pathStockPriceReadJPA.java
File metadata and controls
187 lines (176 loc) · 6.64 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
package net.sdo;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import net.sdo.stock.StockOptionPrice;
import net.sdo.stock.StockPrice;
import net.sdo.stock.StockPriceUtils;
import net.sdo.stockimpl.StockPriceEagerLazyImpl;
import net.sdo.stockimpl.StockPricePK;
public class StockPriceReadJPA {
private static long msPerDay = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
private static EntityManagerFactory emf;
private static volatile BigDecimal bd;
private static boolean batch;
private static boolean processOptions = true;
private static boolean eagerMtO;
private static boolean lazyOtM;
private static boolean join;
private static boolean debug;
private static boolean doSingle;
private static boolean queryCache;
private static long run(String query) {
long then = System.currentTimeMillis();
EntityManager em = emf.createEntityManager();
Query q = em.createNamedQuery(query);
if (batch) {
q.setHint("eclipselink.JDBC_FETCH_SIZE", "1");
}
if (debug) {
System.out.println("Start Execute Query");
}
List<StockPrice> l = q.getResultList();
if (debug) {
System.out.println("End Execute Query");
}
String lastSymbol = "";
for (StockPrice sp : l) {
if (!sp.getSymbol().equals(lastSymbol)) {
lastSymbol = sp.getSymbol();
if (debug) System.out.println("Processing " + lastSymbol);
}
if (debug) {
System.out.println("Start Visit Stock price");
}
bd = sp.getClosingPrice();
if (processOptions) {
Collection<? extends StockOptionPrice> options = sp.getOptions();
for (StockOptionPrice sop : options) {
if (debug) {
System.out.println("Start Visit Option price");
}
bd = sop.getPrice().add(bd);
}
}
}
em.close();
return System.currentTimeMillis() - then;
}
private static long run() {
long then = System.currentTimeMillis();
EntityManager em = emf.createEntityManager();
String lastSymbol = "";
for (int i = 0; i < 128; i++) {
String symbol = StockPriceUtils.makeSymbol(i);
for (Date d : validDates) {
if (debug) {
System.out.println("Do single lookup ");
}
StockPrice sp = em.find(StockPriceEagerLazyImpl.class, new StockPricePK(symbol, d));
if (sp != null) {
bd = sp.getClosingPrice();
if (processOptions) {
Collection<? extends StockOptionPrice> options = sp.getOptions();
for (StockOptionPrice sop : options) {
if (debug) {
System.out.println("Do single option lookup ");
}
bd = sop.getPrice().add(bd);
}
}
}
}
}
em.close();
return System.currentTimeMillis() - then;
}
private static ArrayList<Date> validDates = new ArrayList<>();
private static void makeDates() {
Date d = new Date("1/1/13");
Date endDate = new Date("12/31/13");
Calendar calendar = Calendar.getInstance();
while (d.before(endDate)) {
calendar.setTime(d);
if (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY
&& calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
validDates.add((Date) d.clone());
}
d.setTime(d.getTime() + msPerDay);
}
}
public static void main(String[] args) throws IOException {
int curArg = 0;
while (curArg < args.length) {
switch(args[curArg++]) {
case "-b": batch = Boolean.parseBoolean(args[curArg]); break;
case "-p": processOptions = Boolean.parseBoolean(args[curArg]); break;
case "-e": eagerMtO = Boolean.parseBoolean(args[curArg]); break;
case "-l": lazyOtM = Boolean.parseBoolean(args[curArg]); break;
case "-j": join = Boolean.parseBoolean(args[curArg]); break;
case "-d": debug = Boolean.parseBoolean(args[curArg]); break;
case "-s": doSingle = Boolean.parseBoolean(args[curArg]); break;
case "-q": queryCache = Boolean.parseBoolean(args[curArg]); break;
}
curArg++;
}
if (doSingle) {
makeDates();
}
String stockPU;
String query;
if (lazyOtM) {
if (eagerMtO) {
stockPU = "StockPULazyEager";
query = "findAll";
System.out.println("Using LazyEager");
} else {
stockPU = "StockPULazyLazy";
query = "findAll";
System.out.println("Using LazyLazy");
}
}
else {
if (eagerMtO) {
stockPU = "StockPUEagerEager";
query = "findAll";
System.out.println("Using EagerEager");
} else {
if (queryCache) {
stockPU = "StockPUEagerLazyQueryCache";
query = "findAll";
System.out.println("Using EagerLazyQueryCache");
}
else {
stockPU = "StockPUEagerLazy";
query = "findAll";
System.out.println("Using EagerLazy");
}
}
}
if (join) {
query = "findJoin";
}
if (doSingle) {
query = null;
}
emf = Persistence.createEntityManagerFactory(stockPU);
System.out.println("Starting First pass");
long elapsed = (query != null) ? run(query) : run();
System.out.println("First pass completed in " + elapsed);
System.out.println("Starting second pass");
elapsed = (query != null) ? run(query) : run();
System.out.println("Second pass completed in " + elapsed);
}
}