-
Notifications
You must be signed in to change notification settings - Fork 47
/
benchmark.js
210 lines (175 loc) · 5.08 KB
/
benchmark.js
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const fetch = require('node-fetch')
const split2 = require('split2')
const WebSocket = require('ws')
const serialize = (options) => {
return encodeURIComponent(JSON.stringify(options))
}
class SimpleWebsocketClient {
constructor(url, onMessageCB, onOpen) {
this._socket = new WebSocket(url)
this._socket.on('open', onOpen)
this._socket.on('message', onMessageCB)
this._socket.on('error', (err) => {
console.log('SimpleWebsocketClient error', err)
})
}
send(payload) {
this._socket.send(JSON.stringify(payload))
}
async closed() {
await new Promise((resolve) => {
this._socket.on('close', () => {
resolve()
})
})
}
}
const EXCHANGE = 'bitmex'
const SYMBOL = 'XBTUSD'
const TRADES_AND_BOOK_FILTERS = [
{
channel: 'trade',
symbols: [SYMBOL]
},
{
channel: 'orderBookL2',
symbols: [SYMBOL]
}
]
const TRADES_AND_BOOK_SUBSCRIPTION_MESSAGES = [
{
op: 'subscribe',
args: [`trade:${SYMBOL}`, `orderBookL2:${SYMBOL}`]
}
]
const FROM_DATE = '2020-02-01'
const TO_DATE = '2020-02-02'
async function httpReplayBenchmark({ JSONParseResponse }) {
const options = {
exchange: EXCHANGE,
filters: TRADES_AND_BOOK_FILTERS,
from: FROM_DATE,
to: TO_DATE
}
const response = await fetch(`http://localhost:8000/replay?options=${serialize(options)}`)
const messagesStream = response.body.pipe(split2())
let messagesCount = 0
let startTime = new Date()
for await (let line of messagesStream) {
if (JSONParseResponse) {
JSON.parse(line)
}
messagesCount++
}
const elapsedSeconds = (new Date() - startTime) / 1000
const messagesPerSecond = Math.round(messagesCount / elapsedSeconds)
console.log('HTTP /replay finished', {
JSONParseResponse,
messagesPerSecond,
messagesCount,
elapsedSeconds
})
}
async function httpReplayNormalizedBenchmark({ computeTBTBookSnapshots }) {
const options = {
exchange: EXCHANGE,
symbols: [SYMBOL],
from: FROM_DATE,
to: TO_DATE,
dataTypes: ['trade', 'book_change']
}
if (computeTBTBookSnapshots) {
options.dataTypes.push('book_snapshot_50_0ms')
}
const response = await fetch(`http://localhost:8000/replay-normalized?options=${serialize(options)}`)
const messagesStream = response.body.pipe(split2())
let messagesCount = 0
let startTime = new Date()
for await (let line of messagesStream) {
messagesCount++
}
const elapsedSeconds = (new Date() - startTime) / 1000
const messagesPerSecond = Math.round(messagesCount / elapsedSeconds)
console.log('HTTP /replay-normalized finished', {
computeTBTBookSnapshots,
messagesPerSecond,
messagesCount,
elapsedSeconds
})
}
async function wsReplayBenchmark({ JSONParseResponse }) {
let messagesCount = 0
let startTime
const simpleBitmexWSClient = new SimpleWebsocketClient(
`ws://localhost:8001/ws-replay?exchange=${EXCHANGE}&from=${FROM_DATE}&to=${TO_DATE}`,
(message) => {
if (!startTime) {
startTime = new Date()
}
if (JSONParseResponse) {
JSON.parse(message)
}
messagesCount++
},
() => {
for (const sub of TRADES_AND_BOOK_SUBSCRIPTION_MESSAGES) {
simpleBitmexWSClient.send(sub)
}
}
)
await simpleBitmexWSClient.closed()
const elapsedSeconds = (new Date() - startTime) / 1000
const messagesPerSecond = Math.round(messagesCount / elapsedSeconds)
console.log('WS /ws-replay finished', {
JSONParseResponse,
messagesPerSecond,
messagesCount,
elapsedSeconds
})
}
async function wsReplayNormalizedBenchmark({ computeTBTBookSnapshots }) {
const options = {
exchange: EXCHANGE,
symbols: [SYMBOL],
from: FROM_DATE,
to: TO_DATE,
dataTypes: ['trade', 'book_change']
}
if (computeTBTBookSnapshots) {
options.dataTypes.push('book_snapshot_50_0ms')
}
let messagesCount = 0
let startTime
const simpleBitmexWSClient = new SimpleWebsocketClient(
`ws://localhost:8001/ws-replay-normalized?options=${serialize(options)}`,
(message) => {
if (!startTime) {
startTime = new Date()
}
messagesCount++
},
() => {}
)
await simpleBitmexWSClient.closed()
const elapsedSeconds = (new Date() - startTime) / 1000
const messagesPerSecond = Math.round(messagesCount / elapsedSeconds)
console.log('WS /ws-replay-normalized finished', {
computeTBTBookSnapshots,
messagesPerSecond,
messagesCount,
elapsedSeconds
})
}
async function runBenchmarks() {
console.log(`tardis-machine benchmark for ${EXCHANGE} from ${FROM_DATE} to ${TO_DATE}`)
console.log('\n')
await httpReplayBenchmark({ JSONParseResponse: false })
await httpReplayBenchmark({ JSONParseResponse: true })
await wsReplayBenchmark({ JSONParseResponse: true })
await httpReplayNormalizedBenchmark({ computeTBTBookSnapshots: false })
await httpReplayNormalizedBenchmark({ computeTBTBookSnapshots: true })
await wsReplayNormalizedBenchmark({ computeTBTBookSnapshots: false })
await wsReplayNormalizedBenchmark({ computeTBTBookSnapshots: true })
}
// assumes tardis-machine server is running
runBenchmarks()