-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_metrics.py
286 lines (224 loc) · 10.5 KB
/
test_metrics.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import tempfile
from unittest.mock import Mock
import pytest
from hathor.p2p.manager import PeerConnectionsMetrics
from hathor.p2p.peer_id import PeerId
from hathor.p2p.protocol import HathorProtocol
from hathor.pubsub import HathorEvents
from hathor.transaction.storage import TransactionCacheStorage, TransactionMemoryStorage
from hathor.wallet import Wallet
from tests import unittest
from tests.utils import HAS_ROCKSDB, add_new_blocks
class BaseMetricsTest(unittest.TestCase):
__test__ = False
def test_p2p_network_events(self):
"""Simulates publishing an event to pubsub the same way as done
by the ConnectionsManager class.
The expected result is that the Metrics class will use the info provided with
the event to set its own fields related to the network peers
"""
# Preparation
self.use_memory_storage = True
manager = self.create_peer('testnet')
self.assertIsInstance(manager.tx_storage, TransactionMemoryStorage)
pubsub = manager.pubsub
# Execution
pubsub.publish(
HathorEvents.NETWORK_PEER_CONNECTED,
protocol=Mock(),
peers_count=PeerConnectionsMetrics(3, 4, 5, 6)
)
self.run_to_completion()
# Assertion
self.assertEquals(manager.metrics.connecting_peers, 3)
self.assertEquals(manager.metrics.handshaking_peers, 4)
self.assertEquals(manager.metrics.connected_peers, 5)
self.assertEquals(manager.metrics.known_peers, 6)
manager.metrics.stop()
def test_connections_manager_integration(self):
"""Tests the integration with the ConnectionsManager class
The expected result is that the both classes communicate through pubsub
to update the Metrics class with info from ConnectionsManager class
"""
# Preparation
tx_storage = TransactionMemoryStorage()
tmpdir = tempfile.mkdtemp()
self.tmpdirs.append(tmpdir)
wallet = Wallet(directory=tmpdir)
wallet.unlock(b'teste')
manager = self.create_peer('testnet', tx_storage=tx_storage, wallet=wallet)
manager.connections.peer_storage.update({"1": PeerId(), "2": PeerId(), "3": PeerId()})
manager.connections.connected_peers.update({"1": Mock(), "2": Mock()})
manager.connections.handshaking_peers.update({Mock()})
# Execution
endpoint = 'tcp://127.0.0.1:8005'
# This will trigger sending to the pubsub one of the network events
manager.connections.connect_to(endpoint, use_ssl=True)
self.run_to_completion()
# Assertion
self.assertEquals(manager.metrics.known_peers, 3)
self.assertEquals(manager.metrics.connected_peers, 2)
self.assertEquals(manager.metrics.handshaking_peers, 1)
self.assertEquals(manager.metrics.connecting_peers, 0)
manager.metrics.stop()
@pytest.mark.skipif(not HAS_ROCKSDB, reason='requires python-rocksdb')
def test_tx_storage_data_collection_with_rocksdb_storage_and_no_cache(self):
"""Tests storage data collection when using RocksDB Storage
with cache disabled.
The expected result is that it will successfully collect
the RocksDB metrics.
"""
path = tempfile.mkdtemp()
self.tmpdirs.append(path)
def _init_manager():
builder = self.get_builder('testnet') \
.use_rocksdb(path, cache_capacity=100) \
.force_memory_index() \
.set_wallet(self._create_test_wallet(unlocked=True))
manager = self.create_peer_from_builder(builder, start_manager=False)
return manager
manager = _init_manager()
manager.metrics._collect_data()
self.assertEqual(manager.metrics.rocksdb_cfs_sizes, {
b'default': 0.0,
b'tx': 0.0,
b'meta': 0.0,
b'attr': 0.0,
b'migrations': 0.0,
b'event': 0.0,
b'event-metadata': 0.0,
})
manager.tx_storage.pre_init()
manager.tx_storage.indexes._manually_initialize(manager.tx_storage)
manager.tx_storage.update_best_block_tips_cache(None)
add_new_blocks(manager, 10)
# XXX: I had to close the DB and reinitialize the classes to force a flush of RocksDB memtables to disk
# But I think we could do this in a better way if we had a python-binding for this Flush method in
# https://github.com/facebook/rocksdb/blob/v7.5.3/include/rocksdb/db.h#L1396
manager.tx_storage._db.close()
manager = _init_manager()
manager.metrics._collect_data()
# We don't know exactly the sizes of each column family,
# but we know empirically that they should be higher than these values
self.assertGreater(manager.metrics.rocksdb_cfs_sizes[b'tx'], 500)
self.assertGreater(manager.metrics.rocksdb_cfs_sizes[b'meta'], 1000)
@pytest.mark.skipif(not HAS_ROCKSDB, reason='requires python-rocksdb')
def test_tx_storage_data_collection_with_rocksdb_storage_and_cache(self):
"""Tests storage data collection when using RocksDB Storage
with cache enabled.
The expected result is that it will successfully collect
the RocksDB metrics.
"""
path = tempfile.mkdtemp()
self.tmpdirs.append(path)
def _init_manager():
builder = self.get_builder('testnet') \
.use_rocksdb(path, cache_capacity=100) \
.force_memory_index() \
.set_wallet(self._create_test_wallet(unlocked=True)) \
.use_tx_storage_cache()
manager = self.create_peer_from_builder(builder, start_manager=False)
return manager
manager = _init_manager()
manager.metrics._collect_data()
# Assert that the metrics are really zero
self.assertEqual(manager.metrics.rocksdb_cfs_sizes, {
b'default': 0.0,
b'tx': 0.0,
b'meta': 0.0,
b'attr': 0.0,
b'migrations': 0.0,
b'event': 0.0,
b'event-metadata': 0.0,
})
manager.tx_storage.pre_init()
manager.tx_storage.indexes._manually_initialize(manager.tx_storage)
manager.tx_storage.update_best_block_tips_cache(None)
add_new_blocks(manager, 10)
# XXX: I had to close the DB and reinitialize the classes to force a flush of RocksDB memtables to disk
# But I think we could do this in a better way if we had a python-binding for this Flush method in
# https://github.com/facebook/rocksdb/blob/v7.5.3/include/rocksdb/db.h#L1396
manager.tx_storage.store._db.close()
manager = _init_manager()
manager.metrics._collect_data()
# We don't know exactly the sizes of each column family,
# but we know empirically that they should be higher than these values
self.assertTrue(manager.metrics.rocksdb_cfs_sizes[b'tx'] > 500)
self.assertTrue(manager.metrics.rocksdb_cfs_sizes[b'meta'] > 1000)
def test_tx_storage_data_collection_with_memory_storage(self):
"""Tests storage data collection when using Memory Storage using no cache
We don't allow using it with cache, so this is the only case
The expected result is that nothing is done, because we currently only collect
data for RocksDB storage
"""
tx_storage = TransactionMemoryStorage()
# All
manager = self.create_peer('testnet', tx_storage=tx_storage)
manager.metrics._collect_data()
self.assertEqual(manager.metrics.rocksdb_cfs_sizes, {})
def test_peer_connections_data_collection(self):
"""Test if peer connections data is correctly being collected from the
ConnectionsManager
"""
# Preparation
self.use_memory_storage = True
manager = self.create_peer('testnet')
self.assertIsInstance(manager.tx_storage, TransactionMemoryStorage)
my_peer = manager.my_peer
def build_hathor_protocol():
protocol = HathorProtocol(
network="testnet",
my_peer=my_peer,
p2p_manager=manager.connections,
use_ssl=False,
inbound=False
)
protocol.peer = PeerId()
return protocol
fake_peers = [
build_hathor_protocol(),
build_hathor_protocol(),
build_hathor_protocol(),
]
fake_peers[0].metrics.received_messages = 1
fake_peers[0].metrics.received_bytes = 100
fake_peers[0].metrics.received_txs = 1
fake_peers[1].metrics.sent_messages = 2
fake_peers[1].metrics.sent_bytes = 200
fake_peers[2].metrics.discarded_blocks = 3
fake_peers[2].metrics.discarded_txs = 3
manager.connections.connections.add(fake_peers[0])
manager.connections.connections.add(fake_peers[1])
manager.connections.connections.add(fake_peers[2])
# Execution
manager.metrics._collect_data()
# Assertion
manager.metrics.peer_connection_metrics[0].received_messages = 1
manager.metrics.peer_connection_metrics[0].received_bytes = 100
manager.metrics.peer_connection_metrics[0].received_txs = 1
manager.metrics.peer_connection_metrics[1].sent_messages = 2
manager.metrics.peer_connection_metrics[1].sent_bytes = 200
manager.metrics.peer_connection_metrics[2].discarded_blocks = 3
manager.metrics.peer_connection_metrics[2].discarded_txs = 3
def test_cache_data_collection(self):
"""Test if cache-related data is correctly being collected from the
TransactionCacheStorage
"""
# Preparation
base_storage = TransactionMemoryStorage()
tx_storage = TransactionCacheStorage(base_storage, self.clock, indexes=None)
manager = self.create_peer('testnet', tx_storage=tx_storage)
tx_storage.stats["hit"] = 10
tx_storage.stats["miss"] = 20
# Execution
manager.metrics._collect_data()
# Assertion
self.assertEquals(manager.metrics.transaction_cache_hits, 10)
self.assertEquals(manager.metrics.transaction_cache_misses, 20)
class SyncV1MetricsTest(unittest.SyncV1Params, BaseMetricsTest):
__test__ = True
class SyncV2MetricsTest(unittest.SyncV2Params, BaseMetricsTest):
__test__ = True
# sync-bridge should behave like sync-v2
class SyncBridgeMetricsTest(unittest.SyncBridgeParams, SyncV2MetricsTest):
pass