-
Notifications
You must be signed in to change notification settings - Fork 8
/
JavaUS.java
194 lines (156 loc) · 5.03 KB
/
JavaUS.java
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
188
189
190
191
192
193
194
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class JavaUS
{
private static final Unsafe unsafe;
static
{
try
{
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe)field.get(null);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
private static final int NUM_RECORDS = 50 * 1000 * 444;
private static long address;
private static final DirectMemoryTrade flyweight = new DirectMemoryTrade();
public static void main(final String[] args)
{
for (int i = 0; i < 5; i++)
{
System.gc();
perfRun(i);
}
}
private static void perfRun(final int runNum)
{
long start = System.currentTimeMillis();
init();
long buyCost = 0;
long sellCost = 0;
for (int i = 0; i < NUM_RECORDS; i++)
{
final DirectMemoryTrade trade = get(i);
if (trade.getSide() == 'B')
{
buyCost += (trade.getPrice() * trade.getQuantity());
}
else
{
sellCost += (trade.getPrice() * trade.getQuantity());
}
}
long duration = System.currentTimeMillis() - start;
System.out.println(runNum + " - duration " + duration + "ms");
System.out.println("buyCost = " + buyCost + " sellCost = " + sellCost);
destroy();
}
private static DirectMemoryTrade get(final int index)
{
final long offset = address + (index * DirectMemoryTrade.getObjectSize());
flyweight.setObjectOffset(offset);
return flyweight;
}
public static void init()
{
final long requiredHeap = NUM_RECORDS * DirectMemoryTrade.getObjectSize();
address = unsafe.allocateMemory(requiredHeap);
for (int i = 0; i < NUM_RECORDS; i++)
{
DirectMemoryTrade trade = get(i);
trade.setTradeId(i);
trade.setClientId(1);
trade.setVenueCode(123);
trade.setInstrumentCode(321);
trade.setPrice(i);
trade.setQuantity(i);
trade.setSide((i & 1) == 0 ? 'B' : 'S');
}
}
private static void destroy()
{
unsafe.freeMemory(address);
}
private static class DirectMemoryTrade
{
private static long offset = 0;
private static final long tradeIdOffset = offset += 0;
private static final long clientIdOffset = offset += 8;
private static final long venueCodeOffset = offset += 8;
private static final long instrumentCodeOffset = offset += 4;
private static final long priceOffset = offset += 4;
private static final long quantityOffset = offset += 8;
private static final long sideOffset = offset += 8;
private static final long objectSize = offset += 2;
private long objectOffset;
public static long getObjectSize()
{
return objectSize;
}
void setObjectOffset(final long objectOffset)
{
this.objectOffset = objectOffset;
}
public long getTradeId()
{
return unsafe.getLong(objectOffset + tradeIdOffset);
}
public void setTradeId(final long tradeId)
{
unsafe.putLong(objectOffset + tradeIdOffset, tradeId);
}
public long getClientId()
{
return unsafe.getLong(objectOffset + clientIdOffset);
}
public void setClientId(final long clientId)
{
unsafe.putLong(objectOffset + clientIdOffset, clientId);
}
public int getVenueCode()
{
return unsafe.getInt(objectOffset + venueCodeOffset);
}
public void setVenueCode(final int venueCode)
{
unsafe.putInt(objectOffset + venueCodeOffset, venueCode);
}
public int getInstrumentCode()
{
return unsafe.getInt(objectOffset + instrumentCodeOffset);
}
public void setInstrumentCode(final int instrumentCode)
{
unsafe.putInt(objectOffset + instrumentCodeOffset, instrumentCode);
}
public long getPrice()
{
return unsafe.getLong(objectOffset + priceOffset);
}
public void setPrice(final long price)
{
unsafe.putLong(objectOffset + priceOffset, price);
}
public long getQuantity()
{
return unsafe.getLong(objectOffset + quantityOffset);
}
public void setQuantity(final long quantity)
{
unsafe.putLong(objectOffset + quantityOffset, quantity);
}
public char getSide()
{
return unsafe.getChar(objectOffset + sideOffset);
}
public void setSide(final char side)
{
unsafe.putChar(objectOffset + sideOffset, side);
}
}
}