forked from alexey-ernest/go-hft-orderbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minpq_test.go
148 lines (120 loc) · 2.75 KB
/
minpq_test.go
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
package hftorderbook
import (
"testing"
"math/rand"
//"fmt"
)
func TestMinPQOne(t *testing.T) {
minpq := NewMinPQ(10)
minpq.Insert(5.0)
res := minpq.Top()
if res != 5.0 {
t.Errorf("actual %+v != expected %+v", res, 5)
}
}
func TestMinPQTwo(t *testing.T) {
minpq := NewMinPQ(10)
minpq.Insert(6.0)
minpq.Insert(5.0)
res := [2]float64{}
res[0] = minpq.Top()
minpq.DelTop()
res[1] = minpq.Top()
exp := [2]float64{5.0, 6.0}
if res != exp {
t.Errorf("actual %+v != expected %+v", res, exp)
}
}
func TestMinPQThree(t *testing.T) {
minpq := NewMinPQ(10)
minpq.Insert(6.0)
minpq.Insert(5.0)
minpq.Insert(4.0)
res := [3]float64{}
res[0] = minpq.Top()
minpq.DelTop()
res[1] = minpq.Top()
minpq.DelTop()
res[2] = minpq.Top()
minpq.DelTop()
exp := [3]float64{4.0, 5.0, 6.0}
if res != exp {
t.Errorf("actual %+v != expected %+v", res, exp)
}
if !minpq.IsEmpty() {
t.Errorf("pq should be empty")
}
}
func TestMinPQRandom(t *testing.T) {
minpq := NewMinPQ(100)
for i := 0; i < 1000; i += 1 {
if minpq.Size() == 100 {
minpq.DelTop()
}
minpq.Insert(float64(rand.Intn(100)))
}
res := [100]float64{}
for i := range res {
res[i] = minpq.Top()
minpq.DelTop()
}
if !minpq.IsEmpty() {
t.Errorf("pq should be empty after all")
}
for i := 1; i < 100; i += 1 {
if res[i] < res[i-1] {
t.Errorf("invalid order")
break
}
}
}
func benchmarkMinPQLimitedRandomInsertWithCaching(n int, b *testing.B) {
pq := NewMinPQ(n)
// maximum number of levels in average is ~10k
limitslist := make([]float64, n)
for i := range limitslist {
limitslist[i] = rand.Float64()
}
// preallocate empty orders
orders := make([]*Order, 0, b.N)
for i := 0; i < b.N; i += 1 {
orders = append(orders, &Order{})
}
// measure insertion time
b.ResetTimer()
limitscache := make(map[float64]*LimitOrder)
for i := 0; i < b.N; i += 1 {
// create a new order
o := orders[i]
o.Id = i
o.Volume = rand.Float64()
// o := &Order{
// Id: i,
// Volume: rand.Float64(),
// }
// set the price
price := limitslist[rand.Intn(len(limitslist))]
// append order to the limit price
if limitscache[price] != nil {
// append to the existing limit in cache
limitscache[price].Enqueue(o)
} else {
// new limit
l := NewLimitOrder(price)
l.Enqueue(o)
// caching limit
limitscache[price] = &l
// inserting into heap
pq.Insert(price)
}
}
}
func BenchmarkMinPQ5kLevelsRandomInsertWithCaching(b *testing.B) {
benchmarkMinPQLimitedRandomInsertWithCaching(5000, b)
}
func BenchmarkMinPQ10kLevelsRandomInsertWithCaching(b *testing.B) {
benchmarkMinPQLimitedRandomInsertWithCaching(10000, b)
}
func BenchmarkMinPQ20kLevelsRandomInsertWithCaching(b *testing.B) {
benchmarkMinPQLimitedRandomInsertWithCaching(20000, b)
}