Repository navigation
Expand file tree
/
Copy pathbenchmark_send_receive.js
More file actions
127 lines (98 loc) · 2.78 KB
/
Copy pathbenchmark_send_receive.js
File metadata and controls
127 lines (98 loc) · 2.78 KB
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
'use strict'
const benchmark = require('benchmark')
const proto = require('../proto/proto')
class MySender1 extends proto.Sender {
constructor () {
super()
this._size = 0
this._log_size = 0
}
get size () {
return this._size
}
get logSize () {
return this._log_size
}
// noinspection JSUnusedLocalSymbols
onSend (buffer, offset, size) {
this._size += size
return size
}
onSendLog (message) {
this._log_size += message.length
}
}
class MySender2 extends proto.Sender {
constructor () {
super()
this._size = 0
this._log_size = 0
}
get size () {
return this._size
}
get logSize () {
return this._log_size
}
// noinspection JSUnusedLocalSymbols
onSend (buffer, offset, size) {
this._size += size
return 0
}
onSendLog (message) {
this._log_size += message.length
}
}
class MyReceiver extends proto.Receiver {
constructor () {
super()
this._log_size = 0
}
get logSize () {
return this._log_size
}
onReceive_OrderMessage (value) {} // eslint-disable-line
onReceive_BalanceMessage (value) {} // eslint-disable-line
onReceive_AccountMessage (value) {} // eslint-disable-line
onReceiveLog (message) {
this._log_size += message.length
}
}
const suite = new benchmark.Suite('Send & Receive')
suite.on('start', function () {
// Create a new account with some orders
this.account = new proto.AccountMessage(new proto.Account(1, 'Test', proto.State.good, new proto.Balance('USD', 1000.0), new proto.Balance('EUR', 100.0)))
this.account.body.orders.push(new proto.Order(1, 'EURUSD', proto.OrderSide.buy, proto.OrderType.market, 1.23456, 1000.0))
this.account.body.orders.push(new proto.Order(2, 'EURUSD', proto.OrderSide.sell, proto.OrderType.limit, 1.0, 100.0))
this.account.body.orders.push(new proto.Order(3, 'EURUSD', proto.OrderSide.buy, proto.OrderType.stop, 1.5, 10.0))
this.sender1 = new MySender1()
this.sender1.send(this.account)
this.sender2 = new MySender2()
this.sender2.send(this.account)
this.receiver = new MyReceiver()
this.receiver.receive(this.sender2.buffer)
})
suite.on('cycle', function (event) {
console.log(String(event.target))
})
suite.add('Send', function () {
// Serialize and send the account
suite.sender1.send(suite.account)
})
suite.add('Send with logs', function () {
// Enable logging
suite.sender1.logging = true
// Serialize and send the account
suite.sender1.send(suite.account)
})
suite.add('Receive', function () {
// Receive the account from the sender
suite.receiver.receive(suite.sender2.buffer)
})
suite.add('Receive with logs', function () {
// Enable logging
suite.receiver.logging = true
// Receive the account from the sender
suite.receiver.receive(suite.sender2.buffer)
})
suite.run({ async: true })