chrisys / train-departure-display

A replica, near real-time, miniature UK railway station train departure sign based upon a Raspberry Pi Zero and 256x64 SPI OLED display
243 stars 74 forks source link

Large performance improvements and a couple of nice to have new features #96

Closed CalamityJames closed 1 year ago

CalamityJames commented 1 year ago

Obsoletes #95 (included in this PR)

Massive thanks to @cr3ative for his stellar work with optimising the scrolling performance, he is the real star of this PR!

Base OS and Python Upgrade

We've moved the base operating system to Alpine, cleaned up the build chain, and simplified the Dockerfile. The resulting image on Balena is about 220MB, about the same as before, but hopefully easier to approach.

This PR has now been tested in Balena and deploys as expected.

Python Profiling

The most computationally expensive thing to do on any computer is render a font. It's what Babbage fought with for years.

I profiled the main loop of this code by vendoring in Luma, removing the threadpool, and running cProfile over it. That revealed two things:

So, main fixes were to put the seconds as an interval-updated zone with 0.1 second resolution, and to pre-render all other commonly used TTF operations in the main loop.

Results

On a Pi Zero (the oldest possible device!), here's the performance on "main" running Raspbian:

353895428_652705433582787_8305305711463697195_n

And here's the performance on "performance", with targetFPS set to 0:

image

Using BalenaOS, the Pi Zero can manage about 33fps when targeting 45fps, at about 70% CPU; this leaves some headroom for the supervisor services.

image

Changelog

We edited the changelog that shouldn't have been edited, sorry! Changes pasted in from changelog:

CalamityJames commented 1 year ago

I don't have two screens so couldn't test the multi-screen performance. Sorry if that totally breaks it! Will be ordering a second screen but my previous ebay supplier is now away till August!

cr3ative commented 1 year ago

Just a note here to say that I reformatted my Pi Zero and installed Python 3.11 (rather than 3.7 which the image comes with), which has many CPU optimisations, and the result is significant:

image

95fps on a Pi Zero is a big leap!

It looks like the image here: https://hub.docker.com/layers/balenalib/raspberry-pi-debian-python/3.11-buster-run/images/sha256-d74b72c912b9f0d019308d0995e50c82b54c106466656d094ebdd30d831e72f7?context=explore

Could be used, but I haven't run this project through Balena yet, so I'm not going to fiddle with Balena-specific settings.

cr3ative commented 1 year ago

I've proceeded with the move to Python 3.11 and put both the build and the run Docker containers on Alpine.

Also got this all working with Balena, which is frustrating to start with, but quite neato when you get used to it.

Updated the PR to note performance running via BalenaOS is acceptable, but understandably lower.

chrisys commented 1 year ago

Wow guys! I'll take a look at this immediately!

@CalamityJames I've got a spare screen I can send you if you need it for testing purposes - email me your address chris@thefnet.co.uk

CalamityJames commented 1 year ago

@chrisys thanks for the offer - I've dropped you an email :) I should have a Pi Zero 1 (and 2) with me in the next couple of days too so I can test on more realistic devices than my OP Pi3!

chrisys commented 1 year ago

@CalamityJames @cr3ative I've updated my sign (running on balena) with this and it worked first time and is just beautiful, it's the point I always dreamed we could get to, the scrolling is just ๐Ÿ˜

@cr3ative I'm shipping James a display to help with testing but I'm happy to do the same for you too if it helps! I have a couple of spare white ones as I was planning on working on #62 but realistically I'm not going to get to it any time soon.

cr3ative commented 1 year ago

@CalamityJames @cr3ative I've updated my sign (running on balena) with this and it worked first time and is just beautiful, it's the point I always dreamed we could get to, the scrolling is just ๐Ÿ˜

That's really kind of you to say! I hope we got the Balena bits right - hopefully you could tweak them for us anyway. I'll take a display, especially if it saves me taking the dang headers off it! Will drop you an email.

chrisys commented 1 year ago

Yep as far as the balena side is concerned it all looks great! The resultant container is a bit larger than it was previously (225MB vs 89MB) but I agree it makes the maintenance (and development) easier.

GOTO-GOSUB commented 1 year ago

I would just like to say a huge thank you to everyone who contributed to this update. The Pi Zero display under my monitor has gone up from approx 0.9fps easily into the 40's. A truly huge improvement and I'm not seeing the clock sticking as it used to either.

Trains 0 5 0

One silly question though, my TZ is set to the default of "Europe/London" but the clock is an hour behind and making a change to it is not making any difference. Has anyone else encountered this, and if so what have I forgotten to change ?

The departure times are correct, it's the real time clock at the bottom that is an hour behind.

CalamityJames commented 1 year ago

One silly question though, my TZ is set to the default of "Europe/London" but the clock is an hour behind and making a change to it is not making any difference. Has anyone else encountered this, and if so what have I forgotten to change ?

Hah, just glanced behind me and noticed mine is also reporting as 16:27 currently! Will have a look and see if it's something we've broken!

CalamityJames commented 1 year ago

Fix identified I believe, just testing and will submit a new PR!

cr3ative commented 1 year ago

See https://github.com/chrisys/train-departure-display/pull/97 for Time Zone fix.