-
Notifications
You must be signed in to change notification settings - Fork 2
/
h4asyncCOMPARISON.ino
165 lines (144 loc) · 4.11 KB
/
h4asyncCOMPARISON.ino
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
/*
Yes, I know: this code is a filthy hack! It doesn't cleanup the connection, uses statics for
cheap-n-dirty record counting, is full of unused / redundant sh*t and commits a dozen other sins...
I chopped it from another example when I was tired in 5 minutes flat to perform a one-time task
...so live with it!
*/
#include<H4.h>
H4 h4(115200);
#define ECHO_SERVER "192.168.1.20"
#define ECHO_PORT 7
#define YOUR_SSID "XXXXXXXX"
#define YOUR_PWD "XXXXXXXX"
#define USE_ASYNC_CLIENT 0
H4_TIMER dataLoss;
H4_TIMER stagger;
#define USE_NAGLE false
#define DEFAULT_TIMEOUT 10000
#define STAGGER 0
#define BURST_SIZE 20
#ifdef ARDUINO_ARCH_ESP32
#include<WiFi.h>
#else
#include<ESP8266WiFi.h>
#endif
#if USE_ASYNC_CLIENT
#ifdef ARDUINO_ARCH_ESP32
#define LIB_NAME "AsyncTCP"
#include<AsyncTCP.h>
#else
#define LIB_NAME "ESPAsyncTCP"
#include<ESPAsyncTCP.h>
#endif
#else
#include<H4AsyncTCP.h>
#define LIB_NAME "H4AsyncTCP"
#endif
#define BUFSIZE_PACKET TCP_SND_BUF
#define SPEED_PACKET TCP_SND_BUF / BURST_SIZE
#define BUNKER_BUSTER (TCP_WND * 11) / 10
#if USE_ASYNC_CLIENT
char bigbuf[BUNKER_BUSTER]; // WRONG!!!
#else
uint8_t bigbuf[BUNKER_BUSTER];
#endif
struct test{
size_t N;
size_t size;
size_t Tstart;
size_t dataTX;
size_t dataRX;
#if USE_ASYNC_CLIENT
AsyncClient* a;
#else
H4AsyncClient* a;
#endif
};
using VT = std::vector<test>;
VT tests={
{1,BUFSIZE_PACKET},
{2,BUFSIZE_PACKET},
{1,BUNKER_BUSTER},
{BURST_SIZE,SPEED_PACKET}
};
size_t I=0;
void handleData(size_t i,void* data,size_t len){
VT::value_type* T=&tests[i];
size_t N=T->N;
size_t size=T->size;
T->dataRX+=len;
// Serial.printf("HD%d T=%u RCV %d/%d bytes [%d] from 0x%08x\n",i+1,millis(),len,T->dataRX,T->dataTX,data);
if(T->dataRX==T->dataTX){
h4.cancel(dataLoss);
auto Ttotal=millis()-T->Tstart;
Serial.printf("T=%u TEST %d: PASS: %d bytes received. T=%u kBPSeff=%d\n",millis(),i+1,T->dataRX,Ttotal,(T->dataRX/Ttotal));
h4.cancel(stagger);
T->a->close();
}
}
void sendPackets(size_t i){
VT::value_type* T=&tests[i];
size_t N=T->N;
size_t size=T->size;
Serial.printf("T=%u TEST %d: Send %d packet(s) @ %d (=%d) STAGGER=%d\n",millis(),i+1,N,size,N*size,STAGGER);
T->dataRX=0;
T->dataTX=N*size;
dataLoss=h4.once(DEFAULT_TIMEOUT,[=]{
Serial.printf("TEST %d: FAIL: DATA LOSS - RCVD %u\n",i+1,T->dataRX);
dataLoss=h4.cancel(stagger);
T->a->close(); /// ...and kill?
});
T->Tstart=millis();
stagger=h4.nTimes(N,STAGGER,
[=]{
#if USE_ASYNC_CLIENT
T->a->write(bigbuf,size);
#else
T->a->TX(bigbuf,size,false); // data is static - no copy
#endif
}
);
}
void runTest(size_t i){
#if USE_ASYNC_CLIENT
AsyncClient* a=new AsyncClient();
tests[i].a=a;
a->onData([=](void*, AsyncClient*, void* data,size_t len){
handleData(i,(void*) data,len);
});
a->onConnect([=](void*, AsyncClient*){
a->setNoDelay(!USE_NAGLE);
sendPackets(i);
});
#else
H4AsyncClient* a=new H4AsyncClient();
tests[i].a=a;
a->onRX([=](const uint8_t* data, size_t len){ handleData(i,(void*) data,len); });
a->onConnect([=]{
a->nagle(USE_NAGLE);
sendPackets(i);
});
#endif
a->connect(ECHO_SERVER,ECHO_PORT);
}
#define TEST_LAUNCH DEFAULT_TIMEOUT + 1000
void runTests(){
I=0;
h4Chunker<VT>(tests,[=](VT::iterator i){ runTest(I++); },TEST_LAUNCH,TEST_LAUNCH);
}
void h4setup(){
WiFi.mode(WIFI_STA);
WiFi.begin(YOUR_SSID, YOUR_PWD);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
Serial.print(".");
delay(5000);
}
Serial.printf("\nIP: %s\n",WiFi.localIP().toString().c_str());
Serial.printf("LIB %s: NAGLE=%s MSS=%d SND_BUF=%d WND=%d\n",LIB_NAME,USE_NAGLE ? "true":"false",TCP_MSS,TCP_SND_BUF,TCP_WND);
Serial.printf("Runing on %s\n",ARDUINO_BOARD);
auto i=1;
for(auto const& t:tests) Serial.printf(" Test %d: %d pkts %d\n",i++,t.N,t.size);
Serial.printf("Cry 'Havoc!' and let slip the packets of War!\n\n");
runTests();
}