Slide 1

Slide 1 text

Executing scripts in milliseconds with MicroPython Marwan AlSabbagh

Slide 2

Slide 2 text

In 2006 I fell in love

Slide 3

Slide 3 text

With Python

Slide 4

Slide 4 text

We did everything together... Command line scripting Web applications Graphical applications Network programing Coroutines

Slide 5

Slide 5 text

Until one fateful day. Something horrible happened.

Slide 6

Slide 6 text

CONKY

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

● Over 300 built-in widgets ● Supports custom scripts ● I wanted 5 of them ● Running each second ● 60*60*24*5 = 432,000 Conky Setup

Slide 9

Slide 9 text

Python 15% Bash 0.5% vs CPU Usage

Slide 10

Slide 10 text

● Text editor lint check on save Flake8 takes >300ms ● Bash completion pip completion can take >1s Beyond Conky

Slide 11

Slide 11 text

● Elapsed time ● Clock cycles Measure Script Execution

Slide 12

Slide 12 text

add.py print(1 + 1) add.sh echo $((1 + 1)) add.pl print 1 + 1 . "\n"; add.lua print(1 + 1) add.awk BEGIN {print 1+1; exit} Test scripts

Slide 13

Slide 13 text

$ time python3.7 add.py 2 real 0m0.010s user 0m0.004s sys 0m0.007s Bash time command

Slide 14

Slide 14 text

$ perf stat -r 10 /usr/bin/python3.7 /opt/py/add.py Performance counter stats (10 runs): 9.146528 task-clock (msec) # 0.981 CPUs utilized 0 context-switches # 0.000 K/sec 0 cpu-migrations # 0.000 K/sec 804 page-faults # 0.088 M/sec 31,926,587 cycles # 3.491 GHz 39,586,183 instructions # 1.24 insn per cycle 8,926,391 branches # 975.932 M/sec 325,273 branch-misses # 3.64% of all branches 0.009324090 seconds time elapsed ( +- 0.25% ) Linux Performance Tool

Slide 15

Slide 15 text

January 15, 2017 $9 USD ESP8266 80 MHz CPU MicroPython

Slide 16

Slide 16 text

MicroPython ● Lean implementation of Python 3 ● Includes selection of core Python libraries ● Supports Unix platforms ● usocket (e.g. HTTP client/server) ● uos (has uos.system) ● ujson, ure, utime, uzlib

Slide 17

Slide 17 text

Hardware ● Model: system76 - Galago Pro ● OS: Ubuntu Linux 16.04 ● CPU: 3.5 GHz i7-7500U ● RAM: 16 GB ● DISK: 250 GB NVMe SSD ● Shipped: July-2017 Software ● Python 3.7.0 ● MicroPython v1.9.4 (2018-10-04) Hardware/Software Used

Slide 18

Slide 18 text

Execution Time

Slide 19

Slide 19 text

CPU Cycles

Slide 20

Slide 20 text

Part 2 pip bash completion

Slide 21

Slide 21 text

$ time python -c 'import pip' real 0m0.743s user 0m0.696s sys 0m0.048s pip built-in completion ● Usability needs < 100ms ● Does not complete package names

Slide 22

Slide 22 text

export PIP_NO_INDEX=1 export PIP_FIND_LINKS="http://localhost/wheels/" export PIP_TRUSTED_HOST='localhost' python3 -m virtualenv -p python3 /opt/py/venv/main Demo Details ● Nginx hosting over 400 wheels ● Auto complete sub-commands and packages ● live list of packages on every complete (over http) ● Completion includes package name and versions

Slide 23

Slide 23 text

_pip_completion() { compopt -o filenames COMPREPLY=($(compgen -W "$(pipcomp.py $COMP_CWORD)" -- \ "${COMP_WORDS[COMP_CWORD]}")) } complete -F _pip_completion pip bashrc

Slide 24

Slide 24 text

#!/usr/bin/env micropython import urequests, uos, sys def get_packages(url): html = urequests.get(url).text wheels = [i for i in html.split('"') if i.endswith('.whl')] return ['{0}=={1}'.format(*i.split('-')) for i in wheels] def main(): if sys.argv[1] == '1': print('install uninstall freeze list show search help') if sys.argv[1] == '2': url = uos.getenv('PIP_FIND_LINKS') packages = get_packages(url) print('\n'.join(packages)) if __name__ == "__main__": main() pipcomp.py

Slide 25

Slide 25 text

$ perf stat -r 10 /bin/micropython /opt/py/pipcomp.py 1 0.001258725 seconds time elapsed pipcomp Performance Sub-command completion: 1.3 ms $ perf stat -r 10 /bin/micropython /opt/py/pipcomp.py 2 0.009136741 seconds time elapsed Package name completion: 9.1 ms

Slide 26

Slide 26 text

Live Demo!

Slide 27

Slide 27 text

The End https://marwano.com