forked from alexey-ernest/go-hft-orderbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexminpq_test.go
135 lines (112 loc) · 2.51 KB
/
indexminpq_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
package hftorderbook
import (
"testing"
"math/rand"
//"fmt"
)
func TestIndexMinPQOne(t *testing.T) {
minpq := NewIndexMinPQ(10)
minpq.Insert(0, 5.0)
res := minpq.Top()
if res != 5.0 {
t.Errorf("actual %+v != expected %+v", res, 5)
}
}
func TestIndexMinPQTwo(t *testing.T) {
minpq := NewIndexMinPQ(10)
minpq.Insert(0, 6.0)
minpq.Insert(1, 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 TestIndexMinPQThree(t *testing.T) {
minpq := NewIndexMinPQ(10)
minpq.Insert(0, 6.0)
minpq.Insert(1, 5.0)
minpq.Insert(2, 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 TestIndexMinPQRandom(t *testing.T) {
minpq := NewIndexMinPQ(100)
emptyindex := 0
for i := 0; i < 1000; i += 1 {
emptyindex = i
if minpq.Size() == 100 {
emptyindex = minpq.DelTop()
}
minpq.Insert(emptyindex, 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")
}
}
}
func BenchmarkIndexMinPQLimitedRandomInsertWithCaching(b *testing.B) {
pq := NewIndexMinPQ(10000)
// maximum number of levels in average is 10k
limitslist := make([]float64, 10000)
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(len(limitscache)-1, price)
}
}
}