-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathperformance_test.py
139 lines (109 loc) · 3.51 KB
/
performance_test.py
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
'''
Copyright (C) 2020-2021 Bryant Moscon - [email protected]
Please see the LICENSE file for the terms and conditions
associated with this software.
'''
from decimal import Decimal
from functools import wraps
import random
import time
from sortedcontainers import SortedDict as sd
import requests
from order_book import SortedDict, OrderBook
from pyorderbook import OrderBook as PythonOrderbook, SortedDict as PythonSortedDict
data = requests.get("https://api.pro.coinbase.com/products/BTC-USD/book?level=2").json()
def profile(f):
@wraps(f)
def wrapper(*args, **kwargs):
startt = time.time()
ret = f(*args, **kwargs)
total_time = time.time() - startt
print("Time:", total_time)
return ret
return wrapper
@profile
def profile_orderbook():
ob = OrderBook()
for side, d in data.items():
if side == 'bids':
for price, size, _ in d:
ob.bids[Decimal(price)] = size
elif side == 'asks':
for price, size, _ in d:
ob.asks[Decimal(price)] = size
ob.to_dict()
@profile
def profile_orderbook_sd():
ob = {'bid': sd(), 'ask': sd()}
for side, d in data.items():
if side == 'bids':
for price, size, _ in d:
ob['bid'][Decimal(price)] = size
elif side == 'asks':
for price, size, _ in d:
ob['ask'][Decimal(price)] = size
@profile
def profile_orderbook_python():
ob = PythonOrderbook()
for side, d in data.items():
if side == 'bids':
for price, size, _ in d:
ob['bid'][Decimal(price)] = size
elif side == 'asks':
for price, size, _ in d:
ob['ask'][Decimal(price)] = size
ob.to_dict()
def random_data_test(size):
random.seed()
values = []
asc = SortedDict(ordering='ASC')
sorteddict = sd()
raw_python = {}
python_sd = PythonSortedDict(ordering='ASC')
while len(values) != size:
for _ in range(size):
values.append(random.uniform(-100000.0, 100000.0))
values = set(values)
@profile
def test_ordered(dictionary):
for v in values:
dictionary[v] = str(v)
previous = None
for key in dictionary:
assert key in values
assert str(key) == dictionary[key]
if previous:
assert previous < key
previous = key
@profile
def test_unordered(unordered):
for v in values:
unordered[v] = str(v)
previous = None
for key in unordered:
assert key in values
assert str(key) == unordered[key]
if previous:
assert previous != key
previous = key
print(f"C lib with {size} entries")
test_ordered(asc)
print(f"SortedDict Python lib with {size} entries")
test_ordered(sorteddict)
print(f"Orderbook SortedDict Python lib with {size} entries")
test_ordered(python_sd)
print(f"Python dict (non sorted) with {size} entries")
test_unordered(raw_python)
def random_data_performance():
for size in (10, 100, 200, 400, 500, 1000, 2000, 10000, 100000, 200000, 500000):
random_data_test(size)
if __name__ == "__main__":
print("Sorted Dict Performance\n")
random_data_performance()
print("\n\nOrderbook Overall\n")
print("C lib OrderBook")
profile_orderbook()
print("Sortedcontainers OrderBook")
profile_orderbook_sd()
print("Python OrderBook")
profile_orderbook_python()