-->
Page 1 of 1

client.print is slow?

PostPosted: Sun Sep 13, 2015 12:57 am
by aussieshibe
I've noticed in testing ESP8266 Arduino that the WiFiClient.print and WiFiClient.println functions seem to be quite slow to return?

Currently all of my code including comparisons of a few reasonably sized strings all executes in less than 2ms, but print seems to reliably take 50-100ms.

I tested this with the following code provided by martinayotte in this post: http://www.esp8266.com/viewtopic.php?p=26030#p26030
In total, it took 104756ms to call print 1000 times, for an average of 100ms per call.

I'm using the most recent stable version (1.6.5-947-g39819f0).

I believe the problem may be that the print command blocks while waiting for the TCP ACK packet (Wireshark shows roughly a 50ms delay from PSH to ACK). Am I right to think this? How can I send data without blocking in this way? Do I need to use the native SDK?

EDIT: On further testing, I have no idea what's going on, as the below code prints response times of ~2/3ms when sending data to an echo server. To add to the confusion, if I disable echo on the server and just have it accept the incoming data, the time-per-print shoots back up to 100ms again.
Code: Select allvoid loop() {
  Serial.println("************");
  long startTime = millis();
    String str = ">>>>>>>>>>>>>>>";
    client.print(str);
    while(!client.available()){yield();};
    while(client.available()){
      client.read();
    }
   
  Serial.println((String)(millis() - startTime));
}

Re: client.print is slow?

PostPosted: Sat Jan 09, 2016 1:18 pm
by Chris2016
Hi, I'm experiancing a similar issue, but with 2.0.0 on arduino IDE 1.6.7; did you ever find a solution/reliable work-around?

Re: client.print is slow?

PostPosted: Sat Jan 09, 2016 5:49 pm
by WereCatf
I'll copy myself from the other thread:

I've been digging to this issue and while I don't understand all the details it seems it mostly comes down to the fact that the library can only handle one data-packet on the wire at a time and thus has to keep it in the buffer in case it has to be retransmitted, ergo it has to wait for an ACK from the other party. https://github.com/esp8266/Arduino/issues/922 talks about this issue and in https://github.com/esp8266/Arduino/pull/1323 Links2004 claims he will take a look at it later, but does not say when.

In the meantime you can try the modified non-block library mentioned on the last post in the first link I gave. And hope that Links2004 or some other code-wizard does manage to come up with a proper solution to this, eh? :)

Re: client.print is slow?

PostPosted: Wed Jan 13, 2016 3:52 am
by InnerException
I am experiencing the same thing. Each packet takes about 50ms to transmit. Each print or write call results in at least one packet (if the data to be written is more than 1460B, multiple packets are sent). New packets are sent only after ACK.
Does the lower level API (espressif sdk) provide more control?
I could imagine one theoretical solution: the device should not buffer outgoing packages until the ACK is received, but if ack is not received in a certain time period (is it 200ms now?), let the user know by a callback, to regenerate and resend the package(s). Would this be possible?