Add stack usage benchmark (#1698)

This commit is contained in:
Hood Chatham 2021-07-09 13:37:28 -07:00 committed by GitHub
parent e97d3bb2cf
commit 7e180cc8a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 0 deletions

View File

@ -186,6 +186,16 @@ jobs:
- store_test_results:
path: test-results
benchmark-stack-size:
<<: *defaults
steps:
- attach_workspace:
at: .
- run:
name: stack-size
command: |
pytest -s benchmark/stack_usage.py | sed -n 's/## //pg'
test-emsdk:
<<: *defaults
resource_class: small
@ -331,6 +341,9 @@ workflows:
- test-js:
requires:
- build-core
- benchmark-stack-size:
requires:
- build-core
- benchmark:
requires:
- build-packages

78
benchmark/stack_usage.py Normal file
View File

@ -0,0 +1,78 @@
import pytest
@pytest.fixture(scope="session")
def print_info():
headings = [
"browser",
"py_usage",
"js_depth",
"py_depth",
"js_depth/py_usage",
"js_depth/py_depth",
]
fmt = "## {{:{:d}s}} {{:{:d}.2f}} {{:{:d}g}} {{:{:d}g}} {{:{:d}g}} {{:{:d}g}}".format(
*map(len, headings)
)
printed_heading = False
def print_info(*args):
nonlocal printed_heading
if not printed_heading:
printed_heading = True
print("## " + " ".join(headings))
print(fmt.format(*args))
yield print_info
@pytest.mark.skip_refcount_check
@pytest.mark.skip_pyproxy_check
def test_stack_usage(selenium, print_info):
res = selenium.run_js(
"""
window.measure_available_js_stack_depth = () => {
let depth = 0;
function recurse() { depth += 1; recurse(); }
try { recurse(); } catch (err) { }
return depth;
};
let py_usage = pyodide.runPython(`
from js import measure_available_js_stack_depth
def recurse(n):
return measure_available_js_stack_depth() if n==0 else recurse(n-1)
(recurse(0)-recurse(100))/100
`);
let js_depth = measure_available_js_stack_depth();
window.py_depth = [0];
try {
pyodide.runPython(`
import sys
from js import py_depth
sys.setrecursionlimit(2000)
def infiniterecurse():
py_depth[0] += 1
infiniterecurse()
infiniterecurse()
`);
} catch {}
py_depth = py_depth[0];
return [
py_usage,
js_depth,
py_depth,
Math.floor(js_depth/py_usage),
Math.floor(js_depth/py_depth),
]
"""
)
# "py_usage",
# "js_depth",
# "py_depth",
# "js_depth/py_usage",
# "js_depth/py_depth",
print_info(selenium.browser, *res)
selenium.clean_logs()