forked from pyodide/pyodide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_usage.py
87 lines (78 loc) · 2.35 KB
/
stack_usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import pytest
@pytest.fixture(scope="session")
def print_info():
headings = [
"browser",
"py_limit",
"py_usage",
"js_depth",
"py_depth",
"js_depth/py_usage",
"js_depth/py_depth",
]
fmt = "## {{:{:d}s}} {{:{:d}g}} {{:{: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(
"""
self.measure_available_js_stack_depth = () => {
let depth = 0;
function recurse() { depth += 1; recurse(); }
try { recurse(); } catch (err) { }
return depth;
};
let py_limit = pyodide.runPython("import sys; sys.getrecursionlimit()");
self.jsrecurse = function(f, g) {
return (n) => (n > 0) ? f(n-1) : g();
}
let py_usage = pyodide.runPython(`
from js import measure_available_js_stack_depth, jsrecurse
from pyodide.ffi import create_proxy
recurse_proxy = None
def recurse(n):
return jsrecurse(recurse_proxy, measure_available_js_stack_depth)(n)
recurse_proxy = create_proxy(recurse)
(recurse(0)-recurse(100))/100
`);
let js_depth = measure_available_js_stack_depth();
self.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_limit,
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()