Last active
September 30, 2018 00:15
-
-
Save nagadomi/bbf4df93a4ac2fce10d89e4206e4cb7a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip3 install python-dateutil websocket-client | |
import sys | |
import time | |
import json | |
import websocket | |
from dateutil.parser import parse as date_parse | |
from collections import deque | |
K=100 | |
pack_buf = deque(maxlen=K) | |
pack_all_buf = deque(maxlen=K) | |
pack_rate_buf = deque(maxlen=K) | |
lat_buf = deque(maxlen=K) | |
epsilon=1e-7 | |
def mean(q): | |
return sum(q) / (len(q) + epsilon) | |
def on_message(ws, message): | |
message = json.loads(message)["params"]["message"] | |
times = [date_parse(m["exec_date"]) for m in message] | |
lat_buf.extend([time.time() - times[-1].timestamp()]) | |
pack_all_buf.extend([(times[-1] - times[0]).total_seconds()]) | |
if not all([t == times[0] for t in times]): | |
pack_buf.extend([(times[-1] - times[0]).total_seconds()]) | |
pack_rate_buf.extend([1]) | |
else: | |
pack_rate_buf.extend([0]) | |
print("latency: %.4fs, packed(all): %.4fs, packed: %.4fs, packed rate: %d%%, K=%d" % (mean(lat_buf), mean(pack_all_buf), mean(pack_buf), mean(pack_rate_buf) * 100, K)) | |
def on_open(ws): | |
ws.send(json.dumps({"method": "subscribe", "params": {"channel": "lightning_executions_FX_BTC_JPY"}})) | |
if __name__ == "__main__": | |
ws = websocket.WebSocketApp("wss://ws.lightstream.bitflyer.com/json-rpc", | |
on_message = on_message, | |
on_open = on_open) | |
ws.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment