From 3e1b7751c8b56e0d96f2d4cea511736f2aa6d00d Mon Sep 17 00:00:00 2001 From: bd Date: Tue, 5 Aug 2025 00:07:35 -0400 Subject: Add jupyter notebook comparing Aergia and Yappi --- jupyter/yappi_aergia_in_depth.ipynb | 2731 +++++++++++++++++++++++++++++++++++ 1 file changed, 2731 insertions(+) create mode 100644 jupyter/yappi_aergia_in_depth.ipynb (limited to 'jupyter/yappi_aergia_in_depth.ipynb') diff --git a/jupyter/yappi_aergia_in_depth.ipynb b/jupyter/yappi_aergia_in_depth.ipynb new file mode 100644 index 0000000..5be3d5d --- /dev/null +++ b/jupyter/yappi_aergia_in_depth.ipynb @@ -0,0 +1,2731 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "56841af7", + "metadata": {}, + "source": [ + "Instrumentation of code often results in what is known as the 'probing' effect. Collecting data about function runtime with instrumentation, as Yappi does, may cause the profiled code to not run correctly or run more slowly, distorting the measured results. The SCALENE paper shows that sampling-based profilers are capable of outperforming instrumentation in this regard.\n", + "\n", + "Yet yappi supports asyncio, while no sampling based profiler does so far. This is where the prototype profiler Aergia comes in---it has basic support for all sort of programs, but really prioritizes assigning time to suspended asyncio tasks, allowing developers to access whether lingering asyncio tasks cause slowdown in their programs.\n", + "\n", + "This notebook attempts to demonstrate that Aergia not only works, but provides undistorted and more useful results than Yappi." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "054eab59", + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "import asyncio" + ] + }, + { + "cell_type": "markdown", + "id": "317ba5a2", + "metadata": {}, + "source": [ + "Adding the Aergia folder to my modules path is required:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "33fb8e3a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['/gnu/store/c5qarpvwzcz1idryvvpiqd7738jn4rs1-python-3.11.11/lib/python311.zip', '/gnu/store/c5qarpvwzcz1idryvvpiqd7738jn4rs1-python-3.11.11/lib/python3.11', '/gnu/store/c5qarpvwzcz1idryvvpiqd7738jn4rs1-python-3.11.11/lib/python3.11/lib-dynload', '', '/home/bdunahu/.local/lib/python3.11/site-packages', '/gnu/store/xxbyri1xnragvkcrfnzsw5akp3c1hwfh-python-notebook-6.5.7/lib/python3.11/site-packages', '/gnu/store/x8l5b47rka6ydfh0vfscr3i8gxw781zc-python-coverage-7.6.8/lib/python3.11/site-packages', '/gnu/store/v6vh0jxzd595b2gwj6h18slyz5ip6gkd-python-jupyter-server-2.14.0/lib/python3.11/site-packages', '/gnu/store/xzmds8gm2hqs1nv71s1bx87lb1pm1rc3-python-nbval-0.11.0/lib/python3.11/site-packages', '/gnu/store/ihha82cm6xh1g7692k8wlf7fcp24j0kv-python-pytest-8.3.3/lib/python3.11/site-packages', '/gnu/store/21qdhfn1s445ci3jmpr1dbc8kpjhzhci-python-pytest-cov-6.0.0/lib/python3.11/site-packages', '/gnu/store/dq5vkwzc8xvl4q3hww16ic3j5q1hfjc4-python-requests-2.31.0/lib/python3.11/site-packages', '/gnu/store/avv06v3cfanxwi3c7y39f56k8g5nx6ab-python-requests-unixsocket2-0.4.2/lib/python3.11/site-packages', '/gnu/store/d7c41kl3qkynmiwcyj26fppwckifd3hq-python-setuptools-67.6.1/lib/python3.11/site-packages', '/gnu/store/8af8k4p93zahcfndz160m20bfrlf5h4l-python-wheel-0.40.0/lib/python3.11/site-packages', '/gnu/store/ycqwsapbkpmwnfinfdfyc30jsadp6drc-python-argon2-cffi-21.1.0/lib/python3.11/site-packages', '/gnu/store/mar5wcakna02zdmn0izwdl19krm3grj5-python-ipykernel-6.29.4/lib/python3.11/site-packages', '/gnu/store/z9yyzbh2kfd19yysnk7iyhasfs8wgwi2-python-ipython-genutils-0.1.0/lib/python3.11/site-packages', '/gnu/store/bk2bgyy9nwl5x55dvfsfysi34l2jwwf5-python-jinja2-3.1.2/lib/python3.11/site-packages', '/gnu/store/001ii1n5b4cvbi46hqpdas1286lczxv3-python-jupyter-client-7.4.4/lib/python3.11/site-packages', '/gnu/store/aki05mqn67aw0yyy3pjcsgwwqprr24cw-python-jupyter-core-5.7.2/lib/python3.11/site-packages', '/gnu/store/gwbjk09d3g7lw636zk8dfvlfkyisrrzp-python-nest-asyncio-1.5.5/lib/python3.11/site-packages', '/gnu/store/vvancsvy0qp471jcab6a5blmmp9vbk4i-python-nbclassic-1.0.0/lib/python3.11/site-packages', '/gnu/store/l97g5r6mcqr1s4z7v3cyxs71z3y10g0x-python-nbconvert-7.16.4/lib/python3.11/site-packages', '/gnu/store/1l417pwxsggks86x9fimnfav7b86cjfn-python-nbformat-5.10.4/lib/python3.11/site-packages', '/gnu/store/pi5bvn8iyv098ap6nx50j3sgkwcy43m4-python-prometheus-client-0.20.0/lib/python3.11/site-packages', '/gnu/store/vy0dc7a2fm7ry9y4dcmfhc9wdpd5zi8y-python-pyzmq-25.1.0/lib/python3.11/site-packages', '/gnu/store/h2517sx6jg6a3vvkrmvm2v9b4q1x6lkw-python-send2trash-1.8.2/lib/python3.11/site-packages', '/gnu/store/7m1wxcm5ly0lg1jj099ggw7jfs3sfc46-python-terminado-0.10.0/lib/python3.11/site-packages', '/gnu/store/simm56r07r2g6hc4qkz8vvp4zdwc51ps-python-tornado-6.4.2/lib/python3.11/site-packages', '/gnu/store/rnhjwjwfgjm511bdsi638b3bi90h4cj1-python-traitlets-5.14.1/lib/python3.11/site-packages', '/gnu/store/3nnkbl90qk9l7a757ldxjdxyz73p0b9c-python-3.11.11/lib/python3.11/site-packages', '/gnu/store/50w8avb4mdmfqddpkjydiiv95v3xjds5-python-tomli-2.1.0/lib/python3.11/site-packages', '/gnu/store/g8ycyd5jvjw6g2h3sppj2ac2y86kyhbg-python-websocket-client-1.8.0/lib/python3.11/site-packages', '/gnu/store/0m54sa414acswx0i119dnim4hsdamcnx-python-packaging-24.2/lib/python3.11/site-packages', '/gnu/store/r95sk8cjyz7w40dhl1qdcqvfdm41d6mf-python-overrides-7.7.0/lib/python3.11/site-packages', '/gnu/store/npszb6i1683ya9idsgdcjibkxxpjzsnj-python-jupyter-server-terminals-0.5.3/lib/python3.11/site-packages', '/gnu/store/adsbnzagyxsvabr3kzjhnanmmz85i2ir-python-jupyter-events-0.10.0/lib/python3.11/site-packages', '/gnu/store/libv00bm613n92mhmyy95sh70aw0q7gb-python-anyio-4.6.2/lib/python3.11/site-packages', '/gnu/store/j3vk0v15cvmn3qvgm4mpyxik82h0rdlr-python-six-1.16.0/lib/python3.11/site-packages', '/gnu/store/l7mf7i7r2w18xsb2b3r6gk2674nn4a7m-python-py-1.11.0/lib/python3.11/site-packages', '/gnu/store/m7yld8jfs03s5dhlgp292p7ifwp43j17-python-pluggy-1.5.0/lib/python3.11/site-packages', '/gnu/store/djlwr8zkx7bmviypfbh7nwkg57ysdmp6-python-exceptiongroup-1.1.1/lib/python3.11/site-packages', '/gnu/store/3xlb5f0cvhrl2202frr3hqavfdjwdzx4-python-packaging-bootstrap-24.2/lib/python3.11/site-packages', '/gnu/store/cqw0fmfhyqxlvyizv2cqc5y3qh1ywa9s-python-iniconfig-1.1.1/lib/python3.11/site-packages', '/gnu/store/1d7kn9ldv5idlh3bb8a7yajryc8f3ppk-python-attrs-bootstrap-24.2.0/lib/python3.11/site-packages', '/gnu/store/rbj89mvacjvqfsj0bkapafc0zfqcjm4q-python-urllib3-2.2.1/lib/python3.11/site-packages', '/gnu/store/1kmaa5nl0ih4nqbqcif8rgv17iq7dvms-python-idna-3.3/lib/python3.11/site-packages', '/gnu/store/2b7ry0xs541z0ibl9rf5h0jnzvn1pmd0-python-charset-normalizer-2.1.0/lib/python3.11/site-packages', '/gnu/store/vdvcyy897wjk6s39ji3rahaga7v8ki7h-python-certifi-2022.6.15/lib/python3.11/site-packages', '/gnu/store/s4a0g9bjivgyw4sy7zyy5qbswaykaria-python-pbr-5.5.1/lib/python3.11/site-packages', '/gnu/store/hpajypigh3iaq0zw6vcw1jdzmdq1j962-python-typing-extensions-4.12.2/lib/python3.11/site-packages', '/gnu/store/jm1r2v84pfp28njl3agxaj4nmnwpzjs0-python-cffi-1.17.1/lib/python3.11/site-packages', '/gnu/store/qmgmik8rc44f8v5d33smzg6cwgrbvip4-python-psutil-5.9.2/lib/python3.11/site-packages', '/gnu/store/cbhkc48vmbam1bli524ycmia6v55piba-python-matplotlib-inline-0.1.3/lib/python3.11/site-packages', '/gnu/store/f3p7w5aics740njc5w2z9f4b6vi2w6s4-python-ipython-8.29.0/lib/python3.11/site-packages', '/gnu/store/1hza9x63v2ji75kb8kdha78zqkh5ny34-python-debugpy-1.8.9/lib/python3.11/site-packages', '/gnu/store/0ry8mwms6sxy94kcy4v1hzrxd6j45yyc-python-comm-0.2.2/lib/python3.11/site-packages', '/gnu/store/yzhc3lsqn8gwjs5vyvxk551s0fkly664-python-markupsafe-2.1.1/lib/python3.11/site-packages', '/gnu/store/gjhl4ggs0m9hggna0xy47c03mc88f2rd-python-entrypoints-0.3/lib/python3.11/site-packages', '/gnu/store/fazc70c4gxqgk7fh0gyd4n8i9p3x2y56-python-dateutil-2.8.2/lib/python3.11/site-packages', '/gnu/store/1ii8v3vbzlxk9ngdrawmckscfa731rl4-python-platformdirs-4.3.6/lib/python3.11/site-packages', '/gnu/store/bznlrpf2y32dx17067ybda2qs2px9p42-python-notebook-shim-0.2.4/lib/python3.11/site-packages', '/gnu/store/3il19ff54zw2pds9a9w2cv14l90hjrnh-python-tinycss2-1.4.0/lib/python3.11/site-packages', '/gnu/store/fqf4najfm6yp6baml031f48dbvali63f-python-pygments-2.15.1/lib/python3.11/site-packages', '/gnu/store/k1aq0v4fhd4gv5q30kv6ivjn33rn2hv7-python-pandocfilters-1.5.0/lib/python3.11/site-packages', '/gnu/store/p0287p1jclml4sqhqbzghrj7fr5yl2cp-python-nbclient-0.10.0/lib/python3.11/site-packages', '/gnu/store/67pfbf7dkw8l7qy9gj9c2crw5kwwhk3y-python-mistune-3.0.2/lib/python3.11/site-packages', '/gnu/store/zkx6skh4zgnw69jfbdwv7038mhjar3wi-python-jupyterlab-pygments-0.1.2/lib/python3.11/site-packages', '/gnu/store/s3w3q7m4xgvvc1fycakcb2853i4vlgpp-python-importlib-metadata-5.2.0/lib/python3.11/site-packages', '/gnu/store/4wlr8i0ha615n6x2399d8nx3wi8nhfgs-python-defusedxml-0.7.1/lib/python3.11/site-packages', '/gnu/store/5lpdkbk4imssqxis6m3nr21a4l25zcfr-python-bleach-6.0.0/lib/python3.11/site-packages', '/gnu/store/28kii7vzz482p40pb24ympk2sfkpmj9n-python-beautifulsoup4-4.12.3/lib/python3.11/site-packages', '/gnu/store/s4a48y50yl4fifs1ss7haih74idkgjzh-python-jsonschema-4.23.0/lib/python3.11/site-packages', '/gnu/store/y0mbna4yqxjlrx925p6gzmc3ii1f91d7-python-fastjsonschema-2.20.0/lib/python3.11/site-packages', '/gnu/store/0gjwdiaxwzzgyvd0z4mxhzg154bfsz33-python-ptyprocess-0.7.0/lib/python3.11/site-packages', '/gnu/store/7wgwzr1i93kyvy8i35lwm08ijics096l-python-pyparsing-3.2.1/lib/python3.11/site-packages', '/gnu/store/b3i5mf6v75zp4qdsf8psi5vdzmhkzla3-python-rfc3986-validator-0.1.1/lib/python3.11/site-packages', '/gnu/store/pffrql3h4wl693xhc69090h5fndil9dz-python-rfc3339-validator-0.1.4/lib/python3.11/site-packages', '/gnu/store/dx72kp642qfx96b917svywlbc6flmigc-python-rich-13.7.1/lib/python3.11/site-packages', '/gnu/store/z6ygq5raj9qkrykm3ypiimj3f536wz07-python-referencing-0.35.1/lib/python3.11/site-packages', '/gnu/store/b79n68m9pa68rs07ngra1c3a9b3xi1qv-python-pyyaml-6.0.1/lib/python3.11/site-packages', '/gnu/store/c29z1izy27z0p0msq190csdm8i2as5l8-python-json-logger-2.0.7/lib/python3.11/site-packages', '/gnu/store/ys4rj84grbhahdmgvdyh6rzxkj74g39m-python-sniffio-1.3.1/lib/python3.11/site-packages', '/gnu/store/xqlf1fslnysndampyfqkkhy5db6dx16m-python-contextvars-2.4/lib/python3.11/site-packages', '/gnu/store/iz01ddzyva0qf1iyrzdxaldprrd072pi-python-six-bootstrap-1.16.0/lib/python3.11/site-packages', '/gnu/store/diwaq9r0acrxiigwhjbrwhqlhyzlky4p-python-pysocks-1.7.1/lib/python3.11/site-packages', '/gnu/store/cfyyq8d8549ska921irv13gih0mfv5p0-python-pyopenssl-24.3.0/lib/python3.11/site-packages', '/gnu/store/gg9bqfa5gyxjy94iznsnc3mmivm89l7q-python-cryptography-44.0.0/lib/python3.11/site-packages', '/gnu/store/7qbsfm3pzcc73cvqdj2755d8wkqhjmil-python-pycparser-2.21/lib/python3.11/site-packages', '/gnu/store/smb71frir4ijdpwpk85rckcd3n4asr5y-python-matplotlib-3.8.2/lib/python3.11/site-packages', '/gnu/store/1fa60irxxdidvh685m4fpyaqxcnxh1sp-python-stack-data-0.6.3/lib/python3.11/site-packages', '/gnu/store/wlgbwr1wxi0fn8kk4qj1j2yncwdgj8cq-python-prompt-toolkit-3.0.43/lib/python3.11/site-packages', '/gnu/store/aacdlvzdnv3bv8jnkmss9b5v148rzl92-python-pexpect-4.9.0/lib/python3.11/site-packages', '/gnu/store/c5dr3hm1nv43gpyh13z97fkb13sy35gm-python-jedi-0.19.2/lib/python3.11/site-packages', '/gnu/store/ky32ynvbqxwyqakwyk0hj2nl60ahi157-python-decorator-5.0.9/lib/python3.11/site-packages', '/gnu/store/sx5g7545dvffcai9wvmj1jd3vzk69nbn-python-colorama-0.4.6/lib/python3.11/site-packages', '/gnu/store/szxayp9cjs68f2mmrym67r5r1hhf53rc-python-pydevd-3.2.3/lib/python3.11/site-packages', '/gnu/store/3r4vfjk4d1w6q37s8kpj1jll45gjdhlb-python-webencodings-0.5.1/lib/python3.11/site-packages', '/gnu/store/i3sfl4fjan7f7ixg7rbm99wqvmbhdbjj-python-zipp-1.0.0/lib/python3.11/site-packages', '/gnu/store/f245zik0hby777kikcrcdfycd196dlb6-python-html5lib-1.1/lib/python3.11/site-packages', '/gnu/store/j7cxak4y2a62k2i8ikq0msy87rk1arsh-python-lxml-5.2.2/lib/python3.11/site-packages', '/gnu/store/r73gfkjy67zhyh8p2gpbr3x3b03nmr08-python-soupsieve-2.2.1/lib/python3.11/site-packages', '/gnu/store/pabwbhbx55jp0s1nq18ha5px0i1qc0d6-python-webcolors-24.11.1/lib/python3.11/site-packages', '/gnu/store/7sim1sl86wlzlzl64mia57vdg2g13dfx-python-uri-template-1.3.0/lib/python3.11/site-packages', '/gnu/store/8w31cq1km280z7ihgs2y1gjnw8a9kn5q-python-rpds-py-0.10.6/lib/python3.11/site-packages', '/gnu/store/q3dnvg841zcc42h17lkkzdk1db8ydj7y-python-referencing-bootstrap-0.35.1/lib/python3.11/site-packages', '/gnu/store/zvxli58ld1nr01pnllvlkl5gmyzabr4s-python-jsonschema-specifications-2024.10.1/lib/python3.11/site-packages', '/gnu/store/gsw4ykif32dvi6qkp2gz9xvpjc0s81gc-python-jsonpointer-1.14/lib/python3.11/site-packages', '/gnu/store/7acqbdccam1kmbhj534zw4fqdspc948h-python-isoduration-20.11.0/lib/python3.11/site-packages', '/gnu/store/yk41d5vgxkf8j8g7xadldrnfs0chrwqf-python-importlib-resources-5.12.0/lib/python3.11/site-packages', '/gnu/store/2mvwx9qjiwqxb6akzjiv6lz13ayg57a8-python-fqdn-1.5.1/lib/python3.11/site-packages', '/gnu/store/hkf1b0xdiwrxwfvs1a066fl4j8mzckfb-python-attrs-24.2.0/lib/python3.11/site-packages', '/gnu/store/lqim3jja3zyqavc5rkl5n0qxyvl082jd-python-markdown-it-py-3.0.0/lib/python3.11/site-packages', '/gnu/store/mns86p157znl6ld1wx93wqfrg109q0yi-python-immutables-0.21/lib/python3.11/site-packages', '/gnu/store/yv4n6ng492crwj9nq6bn0kjpzrix7vjp-python-3.11.11-tk/lib/python3.11/site-packages', '/gnu/store/786yxjm4wq3wff8xjkfm6wl946q4a9dc-python-pillow-11.1.0/lib/python3.11/site-packages', '/gnu/store/bx80nwddb0827ryrca0dx85nh4fn2b2x-python-numpy-1.26.4/lib/python3.11/site-packages', '/gnu/store/34y2dizg06p667xmjjcq47n84bimg74l-python-kiwisolver-1.4.6/lib/python3.11/site-packages', '/gnu/store/7r0bfbq1l90qsihhsqad4s5f3w63dhkv-python-fonttools-minimal-4.39.3/lib/python3.11/site-packages', '/gnu/store/6q1s56a6qnxh9b704b9y95jyxi15z3wy-python-cycler-0.10.0/lib/python3.11/site-packages', '/gnu/store/mng7minvpnn7fmyczjlwx3caq0lij5il-python-contourpy-1.3.2/lib/python3.11/site-packages', '/gnu/store/v3dvpj36qih2p1ipvzf0dh4flswswhb4-python-pure-eval-0.2.3/lib/python3.11/site-packages', '/gnu/store/z855h6njq66g87vxxi25rblky0yxnwd5-python-executing-2.1.0/lib/python3.11/site-packages', '/gnu/store/9qx4gnv4hz9gpq7q4zzlkhks3nx0p5w2-python-asttokens-2.4.1/lib/python3.11/site-packages', '/gnu/store/z710c9ibc3fnvc6wqys9jdz0al8w9dh7-python-wcwidth-0.2.13/lib/python3.11/site-packages', '/gnu/store/8rjg30isj2f3l0x6ypdg4nj3j1vzb2b1-python-parso-0.8.4/lib/python3.11/site-packages', '/gnu/store/2d7jpdwjjs8sqwck7cal3sd5p3mxc716-python-more-itertools-10.6.0/lib/python3.11/site-packages', '/gnu/store/m0mbw0x6265ydqpksdfws5qiaj83vlci-python-chardet-5.2.0/lib/python3.11/site-packages', '/gnu/store/w0hl7d5867napkafsp85qz15xnswslgz-python-arrow-1.2.3/lib/python3.11/site-packages', '/gnu/store/izkankkiazd6b1xbi1sfshbv0yss7nxz-python-cached-property-2.0.1/lib/python3.11/site-packages', '/gnu/store/ff9af39qhmapj6b2ppkgs1rkgvadyiq4-python-panflute-2.3.0/lib/python3.11/site-packages', '/gnu/store/9dvbnq602glhvrbk6lqbaxd4ifn8grvh-python-mistletoe-1.3.0/lib/python3.11/site-packages', '/gnu/store/nzj6nf797zsh1az7i85g1f8h6yfd85jm-python-mdurl-0.1.2/lib/python3.11/site-packages', '/gnu/store/fbb83fjfkqgjl66qjpm019ars526fj8w-python-markdown-3.3.4/lib/python3.11/site-packages', '/gnu/store/hnj9qg4ybrqd4rj406psf8mzc7rpiwn3-python-linkify-it-py-2.0.0/lib/python3.11/site-packages', '/gnu/store/y8caisgjifxa4aqpiaxdwjhy449f2l1p-python-commonmark-0.9.1/lib/python3.11/site-packages', '/gnu/store/vrsxnwwmbslhlv4500whcdg6wfkbqb53-python-click-8.1.7/lib/python3.11/site-packages', '/gnu/store/7h80aql8fkvlnq5ywwpvil5yjldfv40l-python-uc-micro-py-1.0.1/lib/python3.11/site-packages', '/gnu/store/9sscnmx9s0b66chv2nd3wc110fqmk01c-python-hatchling-1.26.1/lib/python3.11/site-packages', '/gnu/store/i11m2aa9nks0mxn4gdy6xk1bgsxa60n5-python-pip-23.1/lib/python3.11/site-packages', '/gnu/store/cl35ary861wgpcml6ivrvdcl30g2kzhy-python-pre-commit-3.7.1/lib/python3.11/site-packages', '/gnu/store/7f7an2qrzlv3hbl93qsgblpw4ajy4ymh-python-pytest-timeout-2.3.1/lib/python3.11/site-packages', '/gnu/store/r3fyqnr3a4laq2hzlx1jvfgn2a0cnig6-python-trove-classifiers-2024.10.21.16/lib/python3.11/site-packages', '/gnu/store/5li4bcywi3r9vas9pny8r3s1clwiz3g1-python-pathspec-0.11.1/lib/python3.11/site-packages', '/gnu/store/ff3x2cl0linkxwi5nd073yl1r239m2wr-python-editables-0.5/lib/python3.11/site-packages', '/gnu/store/ksl6l8kg6zpcy58bxic0v6hkrp3gvqa3-python-virtualenv-20.29.1/lib/python3.11/site-packages', '/gnu/store/lll8f9srw4qpkn8f1d8jjwmrbcd2jd72-python-nodeenv-1.8.0/lib/python3.11/site-packages', '/gnu/store/ar9fvhmbmdfyvnaa84mn0pr9qn05wr9y-python-identify-2.5.36/lib/python3.11/site-packages', '/gnu/store/8mdgjh6w4160ra15nmwgbins0k6by0mk-python-cfgv-3.4.0/lib/python3.11/site-packages', '/gnu/store/8md4l2hkn46b213sbkpxprin8aa2g03h-python-filelock-3.16.1/lib/python3.11/site-packages', '/gnu/store/fg0wxqdwvd86xa8cmbcqbiqa756lakff-python-distlib-0.3.7/lib/python3.11/site-packages', '/gnu/store/61sxpc8fx54qq7syhrfg444mx61gnyaa-python-editdistance-0.3.1-1.3ea84a7/lib/python3.11/site-packages', '/gnu/store/6xi74zr92ljnw3n0agasasp46gqikrda-profile/lib/python3.11/site-packages', '/home/bdunahu/.guix-home/profile/lib/python3.11/site-packages', '/gnu/store/c5qarpvwzcz1idryvvpiqd7738jn4rs1-python-3.11.11/lib/python3.11/site-packages', '../aergia/']\n" + ] + } + ], + "source": [ + "sys.path.append('../aergia/')\n", + "print(sys.path)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "00b36557", + "metadata": {}, + "outputs": [], + "source": [ + "from aergia import Aergia\n", + "import yappi" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "3eb2281d", + "metadata": {}, + "outputs": [], + "source": [ + "?Aergia" + ] + }, + { + "cell_type": "markdown", + "id": "ca110c31", + "metadata": {}, + "source": [ + "# Yappi and Aergia" + ] + }, + { + "cell_type": "markdown", + "id": "4aa51b85", + "metadata": {}, + "source": [ + "Aergia reports profiling information on the line level like SCALENE, where yappi reports timing information on the function level.\n", + "\n", + "There are a couple other minor differences too:\n", + "\n", + "- yappi reports CPU time and wall time; Aergia only does wall time.\n", + "- yappi reports information for every function (default), Aergia only reports the the suspended line in each task." + ] + }, + { + "cell_type": "markdown", + "id": "aa685355", + "metadata": {}, + "source": [ + "Since Yappi keeps track of all functions, I'll want our print statement to filter out only what I care about (stealing a function from Aergia):" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "591908f7", + "metadata": {}, + "outputs": [], + "source": [ + "def yappi_print_traceable_results(stats):\n", + " '''Filters the list of yappi stats, printing information for only the same\n", + " functions as Aergia. This is for manual testing.'''\n", + " print(f\"{'FILE':<30} {'FUNC':<30} {'SEC':<10}\")\n", + " for s in stats:\n", + " fname = s.module\n", + " if Aergia._should_trace(fname):\n", + " fname = \\\n", + " (fname if len(fname) <= 25 else fname[-25:])\n", + " print(f\"{fname}:{s.lineno}\".ljust(30) +\n", + " f\"{s.name:<30} {s.ttot:<10.4f}\")" + ] + }, + { + "cell_type": "markdown", + "id": "efd1804d", + "metadata": {}, + "source": [ + "# Setting up yappi and Aergia" + ] + }, + { + "cell_type": "markdown", + "id": "af4924a8", + "metadata": {}, + "source": [ + "Yappi is a singleton class, which means it doesn't require much setup. However, it must be set to profile wall clock time rather than the default of cpu time, as Aergia cannot do CPU time:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1bd50c9e", + "metadata": {}, + "outputs": [], + "source": [ + "def reset_yappi():\n", + " yappi.stop()\n", + " yappi.clear_stats()\n", + " yappi.set_clock_type('wall')\n", + " yappi.set_context_id_callback(None)\n", + " yappi.set_context_name_callback(None)\n", + " yappi.set_tag_callback(None)" + ] + }, + { + "cell_type": "markdown", + "id": "a48930f9", + "metadata": {}, + "source": [ + "The rest is so I may reset the profiler inbetween executions. Aergia also requires this, and must be instantiated one time:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "ad7a7bd6", + "metadata": {}, + "outputs": [], + "source": [ + "aergia_interval = 0.01 # standard profiling interval\n", + "profiles_everything = True\n", + "aergia = Aergia(aergia_interval, profiles_everything)\n", + "\n", + "def reset_aergia():\n", + " aergia.clear()" + ] + }, + { + "cell_type": "markdown", + "id": "d50ff88f", + "metadata": {}, + "source": [ + "I found that results are slightly more consistent when I run the event loop in a separate thread. Jupyter is running its own event loop on the main thread, which may cause extra delays.\n", + "\n", + "I'll further bundle this with the actual await call:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "2b8b09ae", + "metadata": {}, + "outputs": [], + "source": [ + "from threading import Thread" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "f94e192d", + "metadata": {}, + "outputs": [], + "source": [ + "def run_coro(coro):\n", + " thread = Thread(target=lambda: asyncio.run(coro))\n", + " thread.start()\n", + " thread.join()\n", + "\n", + "def run_with_yappi(coro, should_print=False):\n", + " reset_yappi()\n", + " yappi.start()\n", + " run_coro(coro)\n", + " yappi.stop()\n", + " r = yappi.get_func_stats()\n", + " if should_print:\n", + " yappi_print_traceable_results(r)\n", + " return r\n", + "\n", + " \n", + "def run_with_aergia(coro, should_print=False):\n", + " reset_aergia()\n", + " aergia.start()\n", + " run_coro(coro)\n", + " if should_print:\n", + " aergia.print_samples()\n", + " r = aergia.get_samples()\n", + " aergia.stop()\n", + " return r" + ] + }, + { + "cell_type": "markdown", + "id": "fd7812a6", + "metadata": {}, + "source": [ + "# asyncio.sleep (sequential)" + ] + }, + { + "cell_type": "markdown", + "id": "18401e85", + "metadata": {}, + "source": [ + "The sleep function will allow me to compare yappi and aergia while minimizing non-determinism. Note that it, in fact, runs sequentially." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "638bdada", + "metadata": {}, + "outputs": [], + "source": [ + "async def snooze():\n", + " await asyncio.sleep(0.025)\n", + "\n", + "async def alarm(times):\n", + " for i in range(times):\n", + " await snooze()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "d30993d9", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "FILE FUNC PERC (ACTUAL -> SEC)\n", + "ernel_32439/1510412079.py:2 snooze 33.333% (63.000 -> 0.630000)\n", + "kernel_32439/510785827.py:4 run_coro 33.333% (63.000 -> 0.630000)\n", + "kernel_32439/510785827.py:2 33.333% (63.000 -> 0.630000)\n", + "FILE FUNC SEC \n", + "kernel_32439/510785827.py:1 run_coro 0.6429 \n", + "kernel_32439/510785827.py:2 0.6420 \n", + "ernel_32439/1510412079.py:4 alarm 0.6386 \n", + "ernel_32439/1510412079.py:1 snooze 0.6384 \n" + ] + } + ], + "source": [ + "run_with_aergia(alarm(25), True)\n", + "run_with_yappi(alarm(25), True)\n", + "None" + ] + }, + { + "cell_type": "markdown", + "id": "436ab8d8", + "metadata": {}, + "source": [ + "Note that when sampling, Aergia only assigns time to the current suspended task, so the `await snooze()` line does not have time attributed to it." + ] + }, + { + "cell_type": "markdown", + "id": "1414a394", + "metadata": {}, + "source": [ + "Also, remember how yappi has to run instrumented profiling code on every function exit and exit? Given the above `snooze` function contains no logic, calling it for a greater number of repetitions might accumulate the overhead." + ] + }, + { + "cell_type": "markdown", + "id": "103e2dbc", + "metadata": {}, + "source": [ + "To do this experiment, I'll first create a function that puts Aergia and yappi on more even footing: one to sum up all the time spent in the lines of a function under one number.\n", + "\n", + "I'll have to remember that this number can be misleading, percisely due to reporting inconsistences between the two profilers as shown above." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "8a15b19b", + "metadata": {}, + "outputs": [], + "source": [ + "def yappi_extract_values_by_func(stats, func_name):\n", + " '''Returns the total number of samples for function name, given a\n", + " yappi stats object.'''\n", + " for s in stats:\n", + " if s.name == func_name:\n", + " return s.ttot\n", + "\n", + "def aergia_expected_time(total_samples):\n", + " '''Given TOTAL_SAMPLES, returns the total time, using the\n", + " sampling interval.'''\n", + " return total_samples * aergia_interval\n", + "\n", + "def aergia_extract_values_by_func(samples, func_name):\n", + " '''Returns the total number of samples for function name, given an\n", + " Aergia SAMPLES object.'''\n", + " return aergia_expected_time(\n", + " sum(\n", + " value for key, value in samples.items()\n", + " if key.func == func_name\n", + " )\n", + " )" + ] + }, + { + "cell_type": "markdown", + "id": "25d725bd", + "metadata": {}, + "source": [ + "Next, a small function which actually runs the iterations and does the plotting:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "09420196", + "metadata": {}, + "outputs": [], + "source": [ + "def compare_coro(call, func_to_profile, iterations):\n", + " '''Calls CALL with each element of ITERATIONS under both\n", + " yappi and Aergia. Maintains the profiling information for\n", + " FUNC_TO_PROFILE and returns the results.'''\n", + " y_res = []\n", + " a_res = []\n", + " for n, i in enumerate(iterations):\n", + " res = run_with_aergia(call(i), False)\n", + " a_res.append(aergia_extract_values_by_func(res, func_to_profile))\n", + "\n", + " res = run_with_yappi(call(i), False)\n", + " y_res.append(yappi_extract_values_by_func(res, func_to_profile))\n", + "\n", + " if ((n + 1) % 5 == 0):\n", + " print(f'Completed iteration {n + 1}.')\n", + " \n", + " return y_res, a_res" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "7141ba1c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Completed iteration 5.\n" + ] + } + ], + "source": [ + "iterations = list(range(100, 1000, 200))\n", + "\n", + "yres, ares = compare_coro(alarm, 'snooze', iterations)" + ] + }, + { + "cell_type": "markdown", + "id": "0c962138", + "metadata": {}, + "source": [ + "To help visualize, I'll use plotly:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "cb5936dc", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "import plotly.graph_objects as go" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "bca40c72", + "metadata": {}, + "outputs": [], + "source": [ + "def plot_comp(func_name, iterations, aergia, yappi, ideal=None, color1='blue', color2='red', color3='green'):\n", + " fig = go.Figure()\n", + "\n", + " fig.add_trace(go.Scatter(x=iterations, y=aergia, mode='lines', name='aergia', line=dict(color=color1)))\n", + " fig.add_trace(go.Scatter(x=iterations, y=yappi, mode='lines', name='yappi', line=dict(color=color2)))\n", + " if ideal:\n", + " fig.add_trace(go.Scatter(x=iterations, y=ideal, mode='lines', name='ideal', line=dict(color=color3)))\n", + "\n", + "\n", + " fig.update_layout(\n", + " title=f'Reported Seconds for Coroutine {func_name}',\n", + " xaxis_title='tasks run simultaniously',\n", + " yaxis_title='reported seconds',\n", + " legend_title='Legend',\n", + " )\n", + "\n", + "\n", + " fig.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "ced7211f", + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "data": { + "text/html": [ + " \n", + " " + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.plotly.v1+json": { + "config": { + "plotlyServerURL": "https://plot.ly" + }, + "data": [ + { + "line": { + "color": "blue" + }, + "mode": "lines", + "name": "aergia", + "type": "scatter", + "x": [ + 100, + 300, + 500, + 700, + 900 + ], + "y": [ + 2.5300000000000002, + 7.61, + 12.66, + 17.76, + 22.85 + ] + }, + { + "line": { + "color": "red" + }, + "mode": "lines", + "name": "yappi", + "type": "scatter", + "x": [ + 100, + 300, + 500, + 700, + 900 + ], + "y": [ + 2.5606519999999997, + 7.633044, + 12.775767, + 17.922031, + 22.972393999999998 + ] + }, + { + "line": { + "color": "green" + }, + "mode": "lines", + "name": "ideal", + "type": "scatter", + "x": [ + 100, + 300, + 500, + 700, + 900 + ], + "y": [ + 2.5, + 7.5, + 12.5, + 17.5, + 22.5 + ] + } + ], + "layout": { + "legend": { + "title": { + "text": "Legend" + } + }, + "template": { + "data": { + "bar": [ + { + "error_x": { + "color": "#2a3f5f" + }, + "error_y": { + "color": "#2a3f5f" + }, + "marker": { + "line": { + "color": "#E5ECF6", + "width": 0.5 + }, + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "bar" + } + ], + "barpolar": [ + { + "marker": { + "line": { + "color": "#E5ECF6", + "width": 0.5 + }, + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "barpolar" + } + ], + "carpet": [ + { + "aaxis": { + "endlinecolor": "#2a3f5f", + "gridcolor": "white", + "linecolor": "white", + "minorgridcolor": "white", + "startlinecolor": "#2a3f5f" + }, + "baxis": { + "endlinecolor": "#2a3f5f", + "gridcolor": "white", + "linecolor": "white", + "minorgridcolor": "white", + "startlinecolor": "#2a3f5f" + }, + "type": "carpet" + } + ], + "choropleth": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "choropleth" + } + ], + "contour": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "contour" + } + ], + "contourcarpet": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "contourcarpet" + } + ], + "heatmap": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "heatmap" + } + ], + "heatmapgl": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "heatmapgl" + } + ], + "histogram": [ + { + "marker": { + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "histogram" + } + ], + "histogram2d": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "histogram2d" + } + ], + "histogram2dcontour": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "histogram2dcontour" + } + ], + "mesh3d": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "mesh3d" + } + ], + "parcoords": [ + { + "line": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "parcoords" + } + ], + "pie": [ + { + "automargin": true, + "type": "pie" + } + ], + "scatter": [ + { + "fillpattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + }, + "type": "scatter" + } + ], + "scatter3d": [ + { + "line": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatter3d" + } + ], + "scattercarpet": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattercarpet" + } + ], + "scattergeo": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattergeo" + } + ], + "scattergl": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattergl" + } + ], + "scattermapbox": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattermapbox" + } + ], + "scatterpolar": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterpolar" + } + ], + "scatterpolargl": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterpolargl" + } + ], + "scatterternary": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterternary" + } + ], + "surface": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "surface" + } + ], + "table": [ + { + "cells": { + "fill": { + "color": "#EBF0F8" + }, + "line": { + "color": "white" + } + }, + "header": { + "fill": { + "color": "#C8D4E3" + }, + "line": { + "color": "white" + } + }, + "type": "table" + } + ] + }, + "layout": { + "annotationdefaults": { + "arrowcolor": "#2a3f5f", + "arrowhead": 0, + "arrowwidth": 1 + }, + "autotypenumbers": "strict", + "coloraxis": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "colorscale": { + "diverging": [ + [ + 0, + "#8e0152" + ], + [ + 0.1, + "#c51b7d" + ], + [ + 0.2, + "#de77ae" + ], + [ + 0.3, + "#f1b6da" + ], + [ + 0.4, + "#fde0ef" + ], + [ + 0.5, + "#f7f7f7" + ], + [ + 0.6, + "#e6f5d0" + ], + [ + 0.7, + "#b8e186" + ], + [ + 0.8, + "#7fbc41" + ], + [ + 0.9, + "#4d9221" + ], + [ + 1, + "#276419" + ] + ], + "sequential": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "sequentialminus": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ] + }, + "colorway": [ + "#636efa", + "#EF553B", + "#00cc96", + "#ab63fa", + "#FFA15A", + "#19d3f3", + "#FF6692", + "#B6E880", + "#FF97FF", + "#FECB52" + ], + "font": { + "color": "#2a3f5f" + }, + "geo": { + "bgcolor": "white", + "lakecolor": "white", + "landcolor": "#E5ECF6", + "showlakes": true, + "showland": true, + "subunitcolor": "white" + }, + "hoverlabel": { + "align": "left" + }, + "hovermode": "closest", + "mapbox": { + "style": "light" + }, + "paper_bgcolor": "white", + "plot_bgcolor": "#E5ECF6", + "polar": { + "angularaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "bgcolor": "#E5ECF6", + "radialaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + } + }, + "scene": { + "xaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + }, + "yaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + }, + "zaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + } + }, + "shapedefaults": { + "line": { + "color": "#2a3f5f" + } + }, + "ternary": { + "aaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "baxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "bgcolor": "#E5ECF6", + "caxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + } + }, + "title": { + "x": 0.05 + }, + "xaxis": { + "automargin": true, + "gridcolor": "white", + "linecolor": "white", + "ticks": "", + "title": { + "standoff": 15 + }, + "zerolinecolor": "white", + "zerolinewidth": 2 + }, + "yaxis": { + "automargin": true, + "gridcolor": "white", + "linecolor": "white", + "ticks": "", + "title": { + "standoff": 15 + }, + "zerolinecolor": "white", + "zerolinewidth": 2 + } + } + }, + "title": { + "text": "Reported Seconds for Coroutine snooze" + }, + "xaxis": { + "title": { + "text": "tasks run simultaniously" + } + }, + "yaxis": { + "title": { + "text": "reported seconds" + } + } + } + }, + "text/html": [ + "
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "ideal = [x * 0.025 for x in iterations]\n", + "plot_comp('snooze', iterations, ares, yres, ideal)" + ] + }, + { + "cell_type": "markdown", + "id": "e4c56ca9", + "metadata": {}, + "source": [ + "# asyncio.sleep (asynchronous)" + ] + }, + { + "cell_type": "markdown", + "id": "fe542ae5", + "metadata": {}, + "source": [ + "This code does the same as above, synchronously." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "d5ae80e1", + "metadata": {}, + "outputs": [], + "source": [ + "async def alarm(times):\n", + " tasks = [snooze() for _ in range(times)]\n", + " await asyncio.gather(*tasks)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "45e9b10f", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Completed iteration 5.\n", + "Completed iteration 10.\n", + "Completed iteration 15.\n" + ] + } + ], + "source": [ + "iterations = list(range(100, 4500, 250))\n", + "yres, ares = compare_coro(alarm, 'snooze', iterations)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "e33c1263", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.plotly.v1+json": { + "config": { + "plotlyServerURL": "https://plot.ly" + }, + "data": [ + { + "line": { + "color": "blue" + }, + "mode": "lines", + "name": "aergia", + "type": "scatter", + "x": [ + 100, + 350, + 600, + 850, + 1100, + 1350, + 1600, + 1850, + 2100, + 2350, + 2600, + 2850, + 3100, + 3350, + 3600, + 3850, + 4100, + 4350 + ], + "y": [ + 2, + 7, + 16.240000000000002, + 25.5, + 33, + 40.5, + 48, + 22.66, + 42, + 70.5, + 10.28, + 57, + 93.01, + 106.23, + 36.980000000000004, + 75.56, + 102.08, + 66.26 + ] + }, + { + "line": { + "color": "red" + }, + "mode": "lines", + "name": "yappi", + "type": "scatter", + "x": [ + 100, + 350, + 600, + 850, + 1100, + 1350, + 1600, + 1850, + 2100, + 2350, + 2600, + 2850, + 3100, + 3350, + 3600, + 3850, + 4100, + 4350 + ], + "y": [ + 2.624851, + 10.093847, + 20.335304, + 27.180349999999997, + 43.009395999999995, + 68.302106, + 69.776913, + 97.082583, + 132.065781, + 184.641413, + 226.00855199999998, + 300.63442399999997, + 383.388624, + 486.177013, + 598.7597589999999, + 585.833658, + 858.729848, + 829.778103 + ] + }, + { + "line": { + "color": "green" + }, + "mode": "lines", + "name": "ideal", + "type": "scatter", + "x": [ + 100, + 350, + 600, + 850, + 1100, + 1350, + 1600, + 1850, + 2100, + 2350, + 2600, + 2850, + 3100, + 3350, + 3600, + 3850, + 4100, + 4350 + ], + "y": [ + 2.5, + 8.75, + 15, + 21.25, + 27.5, + 33.75, + 40, + 46.25, + 52.5, + 58.75, + 65, + 71.25, + 77.5, + 83.75, + 90, + 96.25, + 102.5, + 108.75 + ] + } + ], + "layout": { + "legend": { + "title": { + "text": "Legend" + } + }, + "template": { + "data": { + "bar": [ + { + "error_x": { + "color": "#2a3f5f" + }, + "error_y": { + "color": "#2a3f5f" + }, + "marker": { + "line": { + "color": "#E5ECF6", + "width": 0.5 + }, + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "bar" + } + ], + "barpolar": [ + { + "marker": { + "line": { + "color": "#E5ECF6", + "width": 0.5 + }, + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "barpolar" + } + ], + "carpet": [ + { + "aaxis": { + "endlinecolor": "#2a3f5f", + "gridcolor": "white", + "linecolor": "white", + "minorgridcolor": "white", + "startlinecolor": "#2a3f5f" + }, + "baxis": { + "endlinecolor": "#2a3f5f", + "gridcolor": "white", + "linecolor": "white", + "minorgridcolor": "white", + "startlinecolor": "#2a3f5f" + }, + "type": "carpet" + } + ], + "choropleth": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "choropleth" + } + ], + "contour": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "contour" + } + ], + "contourcarpet": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "contourcarpet" + } + ], + "heatmap": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "heatmap" + } + ], + "heatmapgl": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "heatmapgl" + } + ], + "histogram": [ + { + "marker": { + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "histogram" + } + ], + "histogram2d": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "histogram2d" + } + ], + "histogram2dcontour": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "histogram2dcontour" + } + ], + "mesh3d": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "mesh3d" + } + ], + "parcoords": [ + { + "line": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "parcoords" + } + ], + "pie": [ + { + "automargin": true, + "type": "pie" + } + ], + "scatter": [ + { + "fillpattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + }, + "type": "scatter" + } + ], + "scatter3d": [ + { + "line": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatter3d" + } + ], + "scattercarpet": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattercarpet" + } + ], + "scattergeo": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattergeo" + } + ], + "scattergl": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattergl" + } + ], + "scattermapbox": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattermapbox" + } + ], + "scatterpolar": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterpolar" + } + ], + "scatterpolargl": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterpolargl" + } + ], + "scatterternary": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterternary" + } + ], + "surface": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "surface" + } + ], + "table": [ + { + "cells": { + "fill": { + "color": "#EBF0F8" + }, + "line": { + "color": "white" + } + }, + "header": { + "fill": { + "color": "#C8D4E3" + }, + "line": { + "color": "white" + } + }, + "type": "table" + } + ] + }, + "layout": { + "annotationdefaults": { + "arrowcolor": "#2a3f5f", + "arrowhead": 0, + "arrowwidth": 1 + }, + "autotypenumbers": "strict", + "coloraxis": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "colorscale": { + "diverging": [ + [ + 0, + "#8e0152" + ], + [ + 0.1, + "#c51b7d" + ], + [ + 0.2, + "#de77ae" + ], + [ + 0.3, + "#f1b6da" + ], + [ + 0.4, + "#fde0ef" + ], + [ + 0.5, + "#f7f7f7" + ], + [ + 0.6, + "#e6f5d0" + ], + [ + 0.7, + "#b8e186" + ], + [ + 0.8, + "#7fbc41" + ], + [ + 0.9, + "#4d9221" + ], + [ + 1, + "#276419" + ] + ], + "sequential": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "sequentialminus": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ] + }, + "colorway": [ + "#636efa", + "#EF553B", + "#00cc96", + "#ab63fa", + "#FFA15A", + "#19d3f3", + "#FF6692", + "#B6E880", + "#FF97FF", + "#FECB52" + ], + "font": { + "color": "#2a3f5f" + }, + "geo": { + "bgcolor": "white", + "lakecolor": "white", + "landcolor": "#E5ECF6", + "showlakes": true, + "showland": true, + "subunitcolor": "white" + }, + "hoverlabel": { + "align": "left" + }, + "hovermode": "closest", + "mapbox": { + "style": "light" + }, + "paper_bgcolor": "white", + "plot_bgcolor": "#E5ECF6", + "polar": { + "angularaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "bgcolor": "#E5ECF6", + "radialaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + } + }, + "scene": { + "xaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + }, + "yaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + }, + "zaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + } + }, + "shapedefaults": { + "line": { + "color": "#2a3f5f" + } + }, + "ternary": { + "aaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "baxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "bgcolor": "#E5ECF6", + "caxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + } + }, + "title": { + "x": 0.05 + }, + "xaxis": { + "automargin": true, + "gridcolor": "white", + "linecolor": "white", + "ticks": "", + "title": { + "standoff": 15 + }, + "zerolinecolor": "white", + "zerolinewidth": 2 + }, + "yaxis": { + "automargin": true, + "gridcolor": "white", + "linecolor": "white", + "ticks": "", + "title": { + "standoff": 15 + }, + "zerolinecolor": "white", + "zerolinewidth": 2 + } + } + }, + "title": { + "text": "Reported Seconds for Coroutine snooze" + }, + "xaxis": { + "title": { + "text": "tasks run simultaniously" + } + }, + "yaxis": { + "title": { + "text": "reported seconds" + } + } + } + }, + "text/html": [ + "
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "ideal = [x * 0.025 for x in iterations]\n", + "plot_comp('snooze', iterations, ares, yres, ideal)" + ] + }, + { + "cell_type": "markdown", + "id": "cf618932", + "metadata": {}, + "source": [ + "# [aiofile](https://pypi.org/project/aiofiles/)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "aed763cb", + "metadata": {}, + "outputs": [], + "source": [ + "import aiofiles\n", + "import os" + ] + }, + { + "cell_type": "markdown", + "id": "65c94910", + "metadata": {}, + "source": [ + "aiofiles is a rewrite of the file library, capable of accessing the user's file system asynchronously." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "8061a64f", + "metadata": {}, + "outputs": [], + "source": [ + "async def read_file(filename):\n", + " async with aiofiles.open(filename, mode='r') as f:\n", + " content = await f.read()\n", + " return content\n", + "\n", + "async def read_lots(iterations):\n", + " filename = os.path.join(os.getcwd(), 'test.txt')\n", + " tasks = [read_file(filename) for _ in range(iterations)]\n", + " await asyncio.gather(*tasks)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4af6cc84", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Completed iteration 5.\n" + ] + } + ], + "source": [ + "iterations = list(range(50, 4000, 400))\n", + "func_name = 'read_file'\n", + "\n", + "yres, ares = compare_coro(read_lots, func_name, iterations)\n", + "plot_comp(func_name, iterations, ares, yres)" + ] + }, + { + "cell_type": "markdown", + "id": "2949ce6c", + "metadata": {}, + "source": [ + "# [aiohttp](https://docs.aiohttp.org/)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "38a455f5", + "metadata": {}, + "outputs": [], + "source": [ + "import aiohttp\n", + "from aiohttp import web" + ] + }, + { + "cell_type": "markdown", + "id": "b314e4a3", + "metadata": {}, + "source": [ + "aiohttp is a rewrite of http with asyncio. It can run both http servers and clients: both will be useful---since we especially want to remove non-determinism from the results of running both yappi and Aergia." + ] + }, + { + "cell_type": "markdown", + "id": "612a6bef", + "metadata": {}, + "source": [ + "Starting with the server logic:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0057245a", + "metadata": {}, + "outputs": [], + "source": [ + "runner = None\n", + "\n", + "async def handle(request):\n", + " print('foo')\n", + " return web.Response(text=\"Hello, world!\")\n", + "\n", + "app = web.Application()\n", + "app.router.add_get('/', handle)\n", + "runner = web.AppRunner(app)\n", + "\n", + "async def start_server():\n", + " await runner.setup()\n", + " site = web.TCPSite(runner, 'localhost', 5678)\n", + " await site.start()\n", + " \n", + "async def stop_server():\n", + " await runner.cleanup()" + ] + }, + { + "cell_type": "markdown", + "id": "ca9ede81", + "metadata": {}, + "source": [ + "And the client:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "942afe5d", + "metadata": {}, + "outputs": [], + "source": [ + "async def fetch(url):\n", + " async with aiohttp.ClientSession() as session:\n", + " async with session.get(url) as response:\n", + " return await response.text()\n", + "\n", + "async def query(request_num):\n", + " urls = ['http://localhost:5678'] * request_num\n", + " tasks = [fetch(url) for url in urls]\n", + " print(tasks)\n", + " return await asyncio.gather(*tasks)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "17c54d45", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n" + ] + } + ], + "source": [ + "iterations = list(range(10, 11, 10))\n", + "func_name = 'fetch'\n", + "\n", + "await start_server()\n", + "# wait for server to start\n", + "await asyncio.sleep(1)\n", + "\n", + "yres, ares = compare_coro(query, func_name, iterations)\n", + "\n", + "await stop_server()\n", + "plot_comp(func_name, iterations, ares, yres)" + ] + }, + { + "cell_type": "markdown", + "id": "915b9cd6", + "metadata": {}, + "source": [ + "## " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "/gnu/store/c5qarpvwzcz1idryvvpiqd7738jn4rs1-python-3.11.11/bin/python3", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} -- cgit v1.2.3