Wokwi simulator slower than real ESP32

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
V!nce
Posts: 20
Joined: Sun May 22, 2022 4:35 pm

Wokwi simulator slower than real ESP32

Post by V!nce » Mon May 30, 2022 7:20 pm

Hi everyone,

For the past 2 weeks, I have been using the Wokwi simulator to learn micropython and preparing some code before I get my real esp32s. I have been suspecting a bug in the simulator with uasyncio for quite a while. Indeed, I felt like the uasyncio.sleep(n) and uasyncio.sleep_ms(n) where quite slow and were actually causing problems with timing (as if there was a lot of overhead) and therefore, the sleep time given was not really respected.

I tested the same function, with and without the uasyncio sleep functions and noticed quite a big difference between it and the synchronous sleep in the simulator. Today, I finally got my Esp32s devkit and tested it with the code below:

Code: Select all

#necessary imports 

async def benchmark(n):
    l1 = Pin(12)
    on = l1.on
    off = l1.off

    for i in range(n):
        on()
        off()
        await asyncio.sleep_ms(5)

n = 500
s = ticks_ms()
asyncio.run(benchmark(n))
print(f"{n} loops ran in {ticks_diff(ticks_ms(), s)}ms")
and the results are quite impressive:
  • Wokwi printed: 500 loops ran in 3126ms
  • Real ESP32 printed: 500 loops ran in 2501ms
If we compute how how long the code should take to complete : T = 5ms * 500cycles = 2500ms.
This is exactly what we get from the real ESP32 + a few ms taken to blink the led.
However, with Wikwo we find a 3126/2500 = 1,25 <=> 25% increase in the expected run time!

If someone knows why there are such discrepancies, I would be glad to hear it. Otherwise I hope this remark was constructive and can help people understand why they may have timing issues with asyncio :)

PS: this may have been causing issues observed in this thread where it seems I could never reach the sampling rate i wanted, whatever the number of sensors/rate I set! viewtopic.php?f=18&t=12452

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Wokwi simulator slower than real ESP32

Post by scruss » Mon May 30, 2022 8:34 pm

I don't see any guarantees of timing accuracy on the Wokwi site. The Arduino simulator runs at around 80% true speed.

You're also expecting precise timing in a process running on a browser. It's almost impossible to get real-time accuracy in a user app, so it's pretty amazing the thing works at all.

V!nce
Posts: 20
Joined: Sun May 22, 2022 4:35 pm

Re: Wokwi simulator slower than real ESP32

Post by V!nce » Mon May 30, 2022 9:20 pm

Yep I completely agree, it's an amazing tool! I learned everything on it. It's just that I lost some time trying to figure out if my code was broken or not, or if micropython was just unable to perform efficiently enough... I don't think I red anything on this topic online so I thought it would be worth asking. Especially that I could actually be be the dumdum doing things wrong :D

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Wokwi simulator slower than real ESP32

Post by scruss » Tue May 31, 2022 2:17 pm

V!nce wrote:
Mon May 30, 2022 9:20 pm
... Especially that I could actually be be the dumdum doing things wrong :D
Hey, it's better to think of yourself as learning. None of this stuff is obvious, and your assumption that a simulator would be the same speed as real hardware is a reasonable one. And it looks like you got some coding done in it before your real hardware arrived, so it wasn't a total waste of time. I wouldn't have learned about the Wokwi MicroPython Simulator without your post, either.

V!nce
Posts: 20
Joined: Sun May 22, 2022 4:35 pm

Re: Wokwi simulator slower than real ESP32

Post by V!nce » Tue May 31, 2022 5:29 pm

I am glad that my post and observations were useful to someone.
I think Wokwi is perfect to test logic and to learn basic micropython programming with a few sensors. But it is important to be aware of its flaws in term of speed and maybe weird overheads 😊
I did not pretend to have made a complete benchmark, I just noticed that the asyncio sleep functions were not behaving properly.
I tested the same kind of async sampling loop today while writing in a file on the real esp and got around 1.75% values than I should have had so that's ok with me 👌
Maybe I could push the cpu frequency higher but not sure it would change much because I did nothing in the simulator...

[EDIT]: The delta between the number of awaited values and the real one seems to be 0.8%! This could just be milliseconds approximations in the delay i give to sleep_ms func aswell...

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Wokwi simulator slower than real ESP32

Post by mattyt » Fri Jun 03, 2022 11:12 am

Note that, when the simulation is running, in the top-right of the Wokwi display there's a dial showing up to 100%; I believe this is an estimate as to the relative speed of the micro simulation. You'll see that it's often below 100%. :)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Wokwi simulator slower than real ESP32

Post by pythoncoder » Mon Jun 06, 2022 8:21 am

That simulator is rather impressive. One thing that had me foxed - how can you see the output of print statements?
Peter Hinch
Index to my micropython libraries.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Wokwi simulator slower than real ESP32

Post by mattyt » Tue Jun 07, 2022 1:50 pm

It's visible in the REPL window in the lower right after you start running the simulation, before the REPL appears.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Wokwi simulator bug?

Post by pythoncoder » Wed Jun 08, 2022 8:06 am

Ah! I started out by running and modifying the 7-segment demo. Because this runs forever (until you stop the simulation) you never get to see the REPL window. Nor do you see the outcome of print statements I put in the code. I think this is a bug. You really do need to see the output of print statements in real time.
Peter Hinch
Index to my micropython libraries.

Post Reply