-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathlatency_tracking.py
More file actions
61 lines (45 loc) · 1.56 KB
/
latency_tracking.py
File metadata and controls
61 lines (45 loc) · 1.56 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
# !/usr/bin/env python
# coding: utf-8
"""
Example of how to track WebSocket latency with local timestamps.
"""
import time
import logging
import asyncio
from gate_ws import Configuration, Connection, WebSocketResponse
from gate_ws.spot import SpotPublicTradeChannel
logger = logging.getLogger(__name__)
async def on_trade(conn: Connection, response: WebSocketResponse):
# Local timestamp is injected into the 'result' dictionary
data = response.result
assert '_local_ts' in data, "Local timestamp not found in data"
local_ts = data['_local_ts']
# Immitation of local processing
await asyncio.sleep(0.05)
now_ns = int(time.time() * 1_000_000_000)
latency_ns = now_ns - local_ts
latency_ms = latency_ns / 1_000_000
logger.info(f"[TRADE] Price: {data.get('price')}, Latency: {latency_ms:.3f} ms")
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s: %(message)s")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
cfg = Configuration(
event_loop=loop,
add_local_ts=True # Enable local timestamp feature
)
conn = Connection(cfg)
channel = SpotPublicTradeChannel(conn, on_trade)
channel.subscribe(["BTC_USDT"])
tasks: set[asyncio.Task] = {
loop.create_task(conn.run()),
}
try:
loop.run_forever()
except KeyboardInterrupt:
for task in tasks:
task.cancel()
group = asyncio.gather(*tasks, return_exceptions=True)
loop.run_until_complete(group)
finally:
loop.close()