forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockPriceHistoryBatcher.java
More file actions
90 lines (82 loc) · 2.9 KB
/
Copy pathStockPriceHistoryBatcher.java
File metadata and controls
90 lines (82 loc) · 2.9 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
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
package net.sdo;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import net.sdo.stock.StockPriceHistory;
import net.sdo.stock.StockPriceUtils;
import net.sdo.stockimpl.MockStockPriceEntityManagerFactory;
import net.sdo.stockimpl.StockPriceHistoryImpl;
import net.sdo.stockimpl.StockPriceHistoryLogger;
public class StockPriceHistoryBatcher {
private static final NumberFormat nf =
NumberFormat.getCurrencyInstance(Locale.US);
private static int numStocks;
private static int mode;
private static EntityManagerFactory emf;
private static EntityManager em;
private static void initEM() {
String s = System.getProperty("MockEntityManager");
if (s != null) {
emf = new MockStockPriceEntityManagerFactory(s);
} else {
emf = Persistence.createEntityManagerFactory("StockPU");
}
em = emf.createEntityManager();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws ParseException {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
Date startDate;
Date endDate;
int save = 0;
numStocks = (args.length < 1) ? 10000 : Integer.parseInt(args[0]);
if (args.length < 2) {
startDate = df.parse("01/01/12");
endDate = df.parse("12/31/12");
} else {
startDate = df.parse(args[1]);
endDate = df.parse(args[2]);
}
if (args.length < 3) {
mode = 0;
}
else {
mode = Integer.parseInt(args[3]);
}
if (args.length > 4) {
save = Integer.parseInt(args[4]);
}
System.out.println("Num stocks " + numStocks + " " +
startDate + " " + endDate);
initEM();
StockPriceHistory[] saved = new StockPriceHistory[save];
for (int i = 0; i < numStocks; i++) {
String symbol = StockPriceUtils.makeSymbol(i);
StockPriceHistory sph;
if (mode == 0) {
sph = new StockPriceHistoryImpl(symbol, startDate, endDate, em);
}
else {
sph = new StockPriceHistoryLogger(symbol, startDate,
endDate, em);
}
System.out.println("For " + sph.getSymbol()
+ ": High " + nf.format(sph.getHighPrice())
+ ", Low " + nf.format(sph.getLowPrice())
+ ", Standard Deviation: " + sph.getStdDev().doubleValue());
if (save > 0) {
saved[i % save] = sph;
}
}
}
}