Page 1 of 1

Performance issue with streams and uasyncio on ESP32

Posted: Wed Aug 03, 2022 2:59 pm
by samosa
Hi guys,

I am testing the server code by Peter from here on my ESP32 dev board: micropython-async/v3/as_drivers/client_server/userver.py

If I change the way I read from the stream on line 34 by calling sreader.read() rather than readline(), then there is around 3x to 4x performance boost. I am attempting to read a small HTTP header from a client, so readline() is quite useful in parsing the header as each header is separated line by line but if I read the whole thing in one go, it is way faster. If I have to use the read() function, then I have to manage the buffer myself in Python.

Does anyone have any ideas what could be the reason and what is the best way to approach this ?

Many thanks

Re: Performance issue with streams and uasyncio on ESP32

Posted: Thu Aug 04, 2022 9:58 am
by karfas
I assume the readline version in the code below (taken from https://github.com/peterhinch/micropyth ... userver.py) will happily read the last line and then wait for another one, until the timeout is reached.
The additional overhead caused by readline() and a task switch from the await is negligible, I think.

Code: Select all

        try:
            while True:
                try:
                    res = await asyncio.wait_for(sreader.readline(), self.timeout)
                except asyncio.TimeoutError:
                    res = b''
                if res == b'':
                    raise OSError
                print('Received {} from client {}'.format(ujson.loads(res.rstrip()), self.cid))
                swriter.write(res)
                await swriter.drain()  # Echo back
        except OSError:
            pass

Re: Performance issue with streams and uasyncio on ESP32

Posted: Fri Aug 05, 2022 10:10 am
by samosa
That is what I expect behvaiour-wise but I still do not understand the large difference in performance. If I read the whole request in one go (if bytes are available) and process it myself line by line, it is much faster

Re: Performance issue with streams and uasyncio on ESP32

Posted: Fri Aug 05, 2022 11:33 am
by karfas
What is the absolute difference in performance you observe ?
And where do you measure ?
For the echo reply readline() might be a few milliseconds slower.
For interpretation of the whole request you send (several lines) the last readline() will wait for the specified timeout (as described above).