Skip to content

Commit e74c47e

Browse files
committed
Update tests
1 parent 677fe9f commit e74c47e

File tree

4 files changed

+63
-33
lines changed

4 files changed

+63
-33
lines changed
File renamed without changes.

tests/test_block.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import pytest
22
from textwrap import dedent
3-
from utils import compile_source
3+
from conftest import compile_source
44
from web.core.app import WebApplication
55

66

7-
@pytest.fixture
8-
def app():
9-
app = WebApplication.instance() or WebApplication()
10-
yield app
11-
12-
137
def test_block(app):
148
# Test that a block's content is replaced
159
Page = compile_source(

tests/test_html.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import sys
21
import inspect
32
import pytest
43
from textwrap import dedent
54
from lxml import html
6-
from utils import compile_source, app
5+
from conftest import compile_source
76

87
try:
98
import nbformat # noqa: F401
@@ -19,6 +18,7 @@
1918
except ImportError:
2019
MARKDOWN_UNAVAILABLE = True
2120

21+
2222
def test_hello_world(app):
2323
Page = compile_source(
2424
dedent(

tests/test_mem.py

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import sys
22
import time
3-
import pytest
43
import tracemalloc
54
import linecache
5+
import pytest
66
from subprocess import Popen, PIPE, STDOUT
77
from textwrap import dedent
8-
from utils import compile_source, app
8+
from conftest import compile_source
99

1010
try:
1111
import requests
@@ -54,29 +54,33 @@ def test_mem_usage():
5454
print(proc.stdout.read())
5555

5656

57-
def diff_stats(snapshot1, snapshot2, label: str, limit: int = 10):
58-
top_stats = snapshot2.compare_to(snapshot1, 'lineno')
57+
def diff_stats(snapshot1, snapshot2, label: str, limit: int = 20):
58+
top_stats = snapshot2.compare_to(snapshot1, "lineno")
5959

6060
print(f"------- Top {limit} differences {label} -------")
6161
for stat in top_stats[:limit]:
6262
print(stat)
6363

6464

65-
def display_top(snapshot, key_type='lineno', limit=10):
66-
snapshot = snapshot.filter_traces((
67-
tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
68-
tracemalloc.Filter(False, "<unknown>"),
69-
))
65+
def display_top(snapshot, key_type="lineno", limit=20):
66+
snapshot = snapshot.filter_traces(
67+
(
68+
tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
69+
tracemalloc.Filter(False, "<unknown>"),
70+
)
71+
)
7072
top_stats = snapshot.statistics(key_type)
7173

7274
print("Top %s lines" % limit)
7375
for index, stat in enumerate(top_stats[:limit], 1):
7476
frame = stat.traceback[0]
75-
print("#%s: %s:%s: %.1f KiB (%i)"
76-
% (index, frame.filename, frame.lineno, stat.size / 1024, stat.count))
77+
print(
78+
"#%s: %s:%s: %.1f KiB (%i)"
79+
% (index, frame.filename, frame.lineno, stat.size / 1024, stat.count)
80+
)
7781
line = linecache.getline(frame.filename, frame.lineno).strip()
7882
if line:
79-
print(' %s' % line)
83+
print(" %s" % line)
8084

8185
other = top_stats[limit:]
8286
if other:
@@ -86,9 +90,25 @@ def display_top(snapshot, key_type='lineno', limit=10):
8690
print("Total allocated size: %.1f KiB" % (total / 1024))
8791

8892

89-
def test_html_size(app):
93+
def snap_and_show_stats(snapshots, label):
94+
snapshots.append(tracemalloc.take_snapshot())
95+
display_top(snapshots[-1])
96+
diff_stats(snapshots[-2], snapshots[-1], "after construct")
97+
98+
99+
@pytest.fixture
100+
def tmalloc():
90101
tracemalloc.start()
91-
Page = compile_source(dedent("""
102+
try:
103+
yield
104+
finally:
105+
tracemalloc.stop()
106+
107+
108+
def test_html_size(tmalloc, app):
109+
Page = compile_source(
110+
dedent(
111+
"""
92112
from web.components.api import *
93113
from web.core.api import *
94114
enamldef Page(Html): view:
@@ -108,27 +128,43 @@ def test_html_size(app):
108128
Td:
109129
text = f"{loop.item}"
110130
111-
"""), "Page")
131+
"""
132+
),
133+
"Page",
134+
)
135+
snapshots = []
136+
snapshots.append(tracemalloc.take_snapshot())
137+
display_top(snapshots[-1])
112138

113-
snapshot1 = tracemalloc.take_snapshot()
114-
display_top(snapshot1)
139+
t0 = time.time()
115140
view = Page()
116-
snapshot2 = tracemalloc.take_snapshot()
117-
display_top(snapshot2)
118-
diff_stats(snapshot1, snapshot2, "after construct", 20)
119141

142+
print(f"\n\n\nConstruct took {time.time()-t0}")
143+
snap_and_show_stats(snapshots, "after construct")
144+
145+
t0 = time.time()
146+
view.initialize()
147+
print(f"\n\n\nInit took {time.time()-t0}")
148+
snap_and_show_stats(snapshots, "after init")
149+
150+
t0 = time.time()
151+
view.activate_proxy()
152+
print(f"\n\n\nActivate took {time.time()-t0}")
153+
snap_and_show_stats(snapshots, "after activate")
154+
155+
t0 = time.time()
120156
content = view.render()
121-
snapshot3 = tracemalloc.take_snapshot()
122-
display_top(snapshot3)
123-
diff_stats(snapshot2, snapshot3, "after render", 20)
157+
print(f"\n\n\nRender took {time.time()-t0}")
158+
snap_and_show_stats(snapshots, "after render")
124159

125-
assert len(content) < 2_500_000
126160
total_size = 0
127161
num_nodes = 0
128162
for node in view.traverse():
129163
num_nodes += 1
130164
total_size += sys.getsizeof(node)
131165
assert total_size < 5_000_000
166+
assert len(content) < 2_500_000
167+
assert False
132168

133169

134170
if __name__ == "__main__":

0 commit comments

Comments
 (0)