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.5 add.py 2 real 0m0.048s user 0m0.044s sys 0m0.000s Bash time command

Slide 14

Slide 14 text

$ perf stat -r 10 /usr/bin/python3.5 /opt/py/add.py Performance counter stats (10 runs): 17.590596 task-clock (msec)# 0.987 CPUs utilized 0 context-switches # 0.017 K/sec 0 cpu-migrations # 0.000 K/sec 916 page-faults # 0.052 M/sec 50,158,374 cycles # 2.851 GHz 11,392,485 branches # 647.646 M/sec 519,939 branch-misses # 4.56% of all branches 0.017830390 seconds time elapsed ( +- 0.97% ) Linux Performance Tool

Slide 15

Slide 15 text

January 15, 2017 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

Execution Time

Slide 18

Slide 18 text

CPU Cycles

Slide 19

Slide 19 text

Part 2 pip bash completion

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

export PIP_NO_INDEX=1 export PIP_FIND_LINKS="http://localhost/wheels/" export PIP_TRUSTED_HOST='localhost' python3 -m virtualenv -p python3 /opt/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 22

Slide 22 text

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

Slide 23

Slide 23 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 24

Slide 24 text

$ perf stat -r 10 /bin/micropython /opt/py/pipcomp.py 1 0.003858786 seconds time elapsed ( +- 5.86% ) pipcomp Performance Sub-command completion: 3.9 ms $ perf stat -r 10 /bin/micropython /opt/py/pipcomp.py 2 0.011441601 seconds time elapsed ( +- 8.77% ) Package name completion: 11.4 ms

Slide 25

Slide 25 text

Live Demo!

Slide 26

Slide 26 text

The End