Performance issue with streams and uasyncio on ESP32

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
samosa
Posts: 2
Joined: Wed Aug 03, 2022 2:47 pm

Performance issue with streams and uasyncio on ESP32

Post by samosa » Wed Aug 03, 2022 2:59 pm

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

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Performance issue with streams and uasyncio on ESP32

Post by karfas » Thu Aug 04, 2022 9:58 am

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
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

samosa
Posts: 2
Joined: Wed Aug 03, 2022 2:47 pm

Re: Performance issue with streams and uasyncio on ESP32

Post by samosa » Fri Aug 05, 2022 10:10 am

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

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Performance issue with streams and uasyncio on ESP32

Post by karfas » Fri Aug 05, 2022 11:33 am

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).
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

Post Reply