1d4afb5ceSopenharmony_ci# lws minimal ws client binance
2d4afb5ceSopenharmony_ci
3d4afb5ceSopenharmony_ciThis connects to the binance ws server and monitors transactions with
4d4afb5ceSopenharmony_cian eye on low latency.
5d4afb5ceSopenharmony_ci
6d4afb5ceSopenharmony_ciLatency seems to be associated with server-side coalescing at tls
7d4afb5ceSopenharmony_cilayer, and the coalescing at server side seems somewhat correlated to number
8d4afb5ceSopenharmony_ciof transactions per second, which seems to cause increased packet sizes from the
9d4afb5ceSopenharmony_ciserver as a reaction.  The relationship is more complex probably according to what
10d4afb5ceSopenharmony_ciactually happens at the server backend, but it seems to be broadly related
11d4afb5ceSopenharmony_cireliably.
12d4afb5ceSopenharmony_ci
13d4afb5ceSopenharmony_ciTypically when showing low latency at ~70msg/s, the messages on the wire are
14d4afb5ceSopenharmony_cieg, ~70 byte packets containing small tls records
15d4afb5ceSopenharmony_ci
16d4afb5ceSopenharmony_ci10:14:40.682293 IP ec2-54-249-113-172.ap-northeast-1.compute.amazonaws.com.https > constance.42952: Flags [P.], seq 50846:50927, ack 1, win 11, options [nop,nop,TS val 366445630 ecr 3893437035], length 81
17d4afb5ceSopenharmony_ci
18d4afb5ceSopenharmony_ciunder pressure from increased messages per second, the tls records increase above 2KB
19d4afb5ceSopenharmony_ci
20d4afb5ceSopenharmony_ci08:06:02.825160 IP ec2-54-249-113-172.ap-northeast-1.compute.amazonaws.com.https > constance.42688: Flags [.], seq 512319:513643, ack 1, win 11, options [nop,nop,TS val 3990208942 ecr 3885719233], length 1324
21d4afb5ceSopenharmony_ci08:06:02.825290 IP constance.42688 > ec2-54-249-113-172.ap-northeast-1.compute.amazonaws.com.https: Flags [.], ack 513643, win 14248, options [nop,nop,TS val 3885719479 ecr 3990208942], length 0
22d4afb5ceSopenharmony_ci08:06:02.891646 IP ec2-54-249-113-172.ap-northeast-1.compute.amazonaws.com.https > constance.42688: Flags [.], seq 513643:516291, ack 1, win 11, options [nop,nop,TS val 3990209006 ecr 3885719296], length 2648
23d4afb5ceSopenharmony_ci
24d4afb5ceSopenharmony_ciThe larger the packets, the longer the first item in the packet had to
25d4afb5ceSopenharmony_ciwait before it was sent, and a tls record cannot be authenticated until
26d4afb5ceSopenharmony_ciall of it has been received.
27d4afb5ceSopenharmony_ci
28d4afb5ceSopenharmony_ciThe example circumvents this somewhat by using `permessage_deflate`, which reduces
29d4afb5ceSopenharmony_cithe packet size before tls by applying compression, making even coalesced packets
30d4afb5ceSopenharmony_cismaller, and a new option for adjusting how lws manages conflicting requirements to
31d4afb5ceSopenharmony_ciclear pending rx and allow interleaved tx, `LCCSCF_PRIORITIZE_READS` that causes the
32d4afb5ceSopenharmony_cistream to prioritize handling any pending rx, not just pending at ssl layer, in one
33d4afb5ceSopenharmony_cievent loop trip.
34d4afb5ceSopenharmony_ci
35d4afb5ceSopenharmony_ci## build
36d4afb5ceSopenharmony_ci
37d4afb5ceSopenharmony_ciLws must have been built with `LWS_ROLE_WS=1` and `LWS_WITHOUT_EXTENSIONS=0`
38d4afb5ceSopenharmony_ci
39d4afb5ceSopenharmony_ci```
40d4afb5ceSopenharmony_ci $ cmake . && make
41d4afb5ceSopenharmony_ci```
42d4afb5ceSopenharmony_ci
43d4afb5ceSopenharmony_ci## Commandline Options
44d4afb5ceSopenharmony_ci
45d4afb5ceSopenharmony_ciOption|Meaning
46d4afb5ceSopenharmony_ci---|---
47d4afb5ceSopenharmony_ci-d|Set logging verbosity
48d4afb5ceSopenharmony_ci
49d4afb5ceSopenharmony_ci## usage
50d4afb5ceSopenharmony_ci
51d4afb5ceSopenharmony_ci```
52d4afb5ceSopenharmony_ci$ ./bin/lws-minimal-ws-client-binance 
53d4afb5ceSopenharmony_ci[2020/08/23 10:22:49:3003] U: LWS minimal binance client
54d4afb5ceSopenharmony_ci[2020/08/23 10:22:49:3005] N: LWS: 4.0.99-v4.1.0-rc2-4-g3cf133aef, loglevel 1031
55d4afb5ceSopenharmony_ci[2020/08/23 10:22:49:3005] N: NET CLI SRV H1 H2 WS MQTT SS-JSON-POL SSPROX ASYNC_DNS IPv6-absent
56d4afb5ceSopenharmony_ci[2020/08/23 10:22:50:8243] N: checking client ext permessage-deflate
57d4afb5ceSopenharmony_ci[2020/08/23 10:22:50:8244] N: instantiating client ext permessage-deflate
58d4afb5ceSopenharmony_ci[2020/08/23 10:22:50:8244] U: callback_minimal: established
59d4afb5ceSopenharmony_ci[2020/08/23 10:22:51:8244] N: sul_hz_cb: price: min: 1160284¢, max: 1163794¢, avg: 1160516¢, (150 prices/s)
60d4afb5ceSopenharmony_ci[2020/08/23 10:22:51:8245] N: sul_hz_cb: elatency: min: 112ms, max: 547ms, avg: 259ms, (155 msg/s)
61d4afb5ceSopenharmony_ci[2020/08/23 10:22:52:8244] N: sul_hz_cb: price: min: 1160287¢, max: 1178845¢, avg: 1160897¢, (112 prices/s)
62d4afb5ceSopenharmony_ci[2020/08/23 10:22:52:8245] N: sul_hz_cb: elatency: min: 111ms, max: 226ms, avg: 152ms, (134 msg/s)
63d4afb5ceSopenharmony_ci[2020/08/23 10:22:53:8247] N: sul_hz_cb: price: min: 1160287¢, max: 1168005¢, avg: 1160806¢, (86 prices/s)
64d4afb5ceSopenharmony_ci[2020/08/23 10:22:53:8248] N: sul_hz_cb: elatency: min: 112ms, max: 476ms, avg: 287ms, (101 msg/s)
65d4afb5ceSopenharmony_ci[2020/08/23 10:22:54:8247] N: sul_hz_cb: price: min: 1160284¢, max: 1162780¢, avg: 1160698¢, (71 prices/s)
66d4afb5ceSopenharmony_ci...
67d4afb5ceSopenharmony_ci```
68