forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockHistorySerializeTest.java
More file actions
122 lines (109 loc) · 4.81 KB
/
Copy pathStockHistorySerializeTest.java
File metadata and controls
122 lines (109 loc) · 4.81 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
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
package net.sdo;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
import javax.persistence.EntityManager;
import net.sdo.stock.StockPriceHistory;
import net.sdo.stockimpl.MockStockPriceEntityManagerFactory;
import net.sdo.stockimpl.StockPriceHistoryCompress;
import net.sdo.stockimpl.StockPriceHistoryCompressLazy;
import net.sdo.stockimpl.StockPriceHistoryCompressUnbuffered;
import net.sdo.stockimpl.StockPriceHistoryEager;
import net.sdo.stockimpl.StockPriceHistoryEagerTransient;
import net.sdo.stockimpl.StockPriceHistoryExternalizable;
import net.sdo.stockimpl.StockPriceHistoryImpl;
public class StockHistorySerializeTest {
private static int nLoops = 10000;
public static void main(String[] args) throws IOException, ClassNotFoundException {
EntityManager em = new MockStockPriceEntityManagerFactory("MockEntityManager").createEntityManager();
StockPriceHistory sph = new StockPriceHistoryImpl("AAAA", new Date("1/01/13"), new Date("12/31/13"), em);
System.out.println("\nStart regular SPH serialize");
test(sph, false);
System.gc();
System.out.println("\nStart regular SPH serialize/deserialize");
test(sph, true);
System.gc();
sph = new StockPriceHistoryCompressUnbuffered("AAAA", new Date("1/01/13"), new Date("12/31/13"), em);
System.out.println("\nStart unbuffered compress SPH serialize");
test(sph, false);
System.gc();
System.out.println("\nStart unbuffered compress SPH serialize/deserialize");
test(sph, true);
System.gc();
sph = new StockPriceHistoryCompress("AAAA", new Date("1/01/13"), new Date("12/31/13"), em);
System.out.println("\nStart compress SPH serialize");
test(sph, false);
System.gc();
System.out.println("\nStart compress SPH serialize/deserialize");
test(sph, true);
System.gc();
sph = new StockPriceHistoryCompressLazy("AAAA", new Date("1/01/13"), new Date("12/31/13"), em);
System.out.println("\nStart lazy compress SPH serialize");
test(sph, false);
System.gc();
System.out.println("\nStart lazy compress SPH serialize/deserialize");
test(sph, true);
System.gc();
sph = new StockPriceHistoryExternalizable("AAAA", new Date("1/01/13"), new Date("12/31/13"), em);
System.out.println("\nStart externalizable SPH serialize");
test(sph, false);
System.gc();
System.out.println("\nStart externalizable SPH serialize/deserialize");
test(sph, true);
System.gc();
sph = new StockPriceHistoryEager("AAAA", new Date("1/01/13"), new Date("12/31/13"), em);
System.out.println("\nStart regular eager SPH serialize");
test(sph, false);
System.gc();
System.out.println("\nStart regular eager SPH serialize/deserialize");
test(sph, true);
System.gc();
sph = new StockPriceHistoryEagerTransient("AAAA", new Date("1/01/13"), new Date("12/31/13"), em);
System.out.println("\nStart regular eager transient SPH serialize");
test(sph, false);
System.gc();
System.out.println("\nStart regular SPH eager transient serialize/deserialize");
test(sph, true);
}
public static void test(StockPriceHistory sph, boolean deserialize) throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(sph);
oos.close();
byte[] b = baos.toByteArray();
System.out.println("Serialized size is " + b.length);
for (int i = 0; i < nLoops; i++) {
baos.reset();
oos = new ObjectOutputStream(baos);
oos.writeObject(sph);
oos.close();
if (deserialize) {
ByteArrayInputStream bais = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(bais);
ois.readObject();
ois.close();
}
}
long then = System.currentTimeMillis();
for (int i = 0; i < nLoops; i++) {
baos.reset();
oos = new ObjectOutputStream(baos);
oos.writeObject(sph);
oos.close();
if (deserialize) {
ByteArrayInputStream bais = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(bais);
ois.readObject();
ois.close();
}
}
long now = System.currentTimeMillis();
System.out.println("Time: " + (now - then));
}
}