How to fast the data transfer rate with WebSocket module 'ws' in Espruino? #6887
Replies: 1 comment
-
Posted at 2017-03-20 by Aifer I have wrote some test codes:
the result:
per 230 bytes packet needs 850ms to be transfered to websocket server(nodejs). Posted at 2017-03-20 by @gfwilliams The data rate depends on loads of things so I can't give you a sensible value, but it might be slowed down by the need to 'unmask' the received data - which would be very dependent on JS execution speed. Posted at 2017-03-20 by Aifer I run the test code on ESP32 module with 16M falsh. In the test code, I store data just in a String var. And with no btoa(), set time interval to zero in setInterval, I get the same result. Posted at 2017-03-20 by Aifer test code with no btoa(), and interval = 0
result:
with no btoa(), about 200ms is saved per 230 bytes packet. Posted at 2017-03-20 by Aifer I try to transfer 230 bytes packet per 100ms with a C++ websocket lib - arduinoWebSockets on esp8266, it's ok. The data rate is at least 6 times faster then 'ws' on Espruino. Even esp8266 is slower then Esp32 module. Is there some method to enchance the performance of 'ws' ? Posted at 2017-03-20 by Aifer The test codes using 'net' module on Esp32. very fast.
result:
Posted at 2017-03-20 by @gfwilliams
Chances are it's this code that is slowing things down: https://github.com/espruino/EspruinoDocs/blob/master/modules/ws.js#L230 You could probably do something like this to make Espruino head through it 32 bits at a time instead of 8:
But you'd have to have a play around yourself - I haven't tested that. Posted at 2017-03-20 by @gfwilliams Just to add - if your server supports it then you may be able to turn masking off, which would help things a huge amount. Posted at 2017-03-21 by Aifer @gfwilliams in the 'ws' codes:
result:
What strange is that after 60 packets are transfered, the program throws " out of memory " error, every time. AND , when the server receives the second packet, it crashed every time.
The first two bytes of the first packet is correct, and that of the second packet is wrong. The first tow bytes of the second(and every frame/packet) should be <Buffer 81 fe>, but they are <Buffer 35 38> now. The error is throwed by the codes in Receiver.js of ws module on nodejs
I'll try to find the problem. Posted at 2017-03-21 by @gfwilliams The out of memory could be that you're now sending data faster than the ESP8266 can send it down WiFi! The 'send' function will return control to your code before the data packets have actually left the ESP8266. I'd compare the data you get from the 2 bits of code - maybe there is something wrong with my code. It could also be that the wrong amount of data is getting sent somehow? That might explain the 'invalid opcode' Posted at 2017-03-21 by Aifer I write some test codes.
result:
It seems that your codes works well. Posted at 2017-03-21 by Aifer @gfwilliams As in the 'net' codes, the ESP32 module can transfer 230bytres in 4~6ms with WiFi.
Posted at 2017-03-21 by Aifer @gfwilliams
to
Then , the server will never crashed, and the ESP32 with no out of memroy all the time. the m8 stores the char code, so we need to convert them to char by fromCharCode(); But, the 'ws' is slowed down, per 230 bytes packet needs 600ms to be transfered , and all the 23KB datas need 66 seconds. Posted at 2017-03-21 by @gfwilliams Ok, thanks - and it's still sending the correct data? That line might be sending the correct length, but I'm not sure if the data will be correct.
Posted at 2017-03-21 by Aifer Yes, it's sending the correct data by your code
or the code below:
They have the same performance. The m8 stores the char code, so we need to convert them to char by fromCharCode(); But, the 'ws' is slowed down, per 230 bytes packet needs 600ms to be transfered , and all the 23KB datas need 66 seconds. Before we use UintArray, the total time is 85 seconds. Do you think we can find some way to enhance the 'ws' performance to transfer 23KB datas within 10 seconds? Posted at 2017-03-23 by Aifer Hi, @gfwilliams You are right! Follow your suggestion, I write the "C" native Mask library for websocket module on Espruino. All codes are here: jswrap_mask.h
jswrap_mask.c
Change in ws.js
Espruino is flexible, Cool! Posted at 2017-03-23 by @gfwilliams That's great - thanks for posting it up! If you were using Espruino on a Pico/Wifi/etc then you could actually use the I think eventually I will implement the |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-03-20 by Aifer
Esprusino on ESP32, I write some codes to transfer data from serial port to a websocket server(nodejs). I use 'ws' module to transfer the data. The Serial2 is set to baudrate 115200, In ten seconds, about 23KB data will be received by Serial2 port. The Serial2.on('data', ...) returns about 230 bytes datas each time. In Serial2.on('data', ...) , I want to transfer the received data (about 230 bytes) to the server by ws.send(), but this will halt the program(the program no responsing). If I cache the 230 bytes to a string or a array until all the 23KB data are received, then send the data by ws.send(), it will take about 120 seconds to trasnsfer them to the server.
I try the work with 'net' module, I can transfer 230bytes data in Serial2.on('data', ...) directly, without cacheing.
Then I try the wok with a websocket module written in c++ in arduino-esp32, I can transfer 230bytes data in Serial2.on('data', ...) directly(without cacheing) too.
How to fast the data transfer rate with 'ws' module in Esrpuino?
Beta Was this translation helpful? Give feedback.
All reactions