1
1
import sys
2
2
import time
3
- import pytest
4
3
import tracemalloc
5
4
import linecache
5
+ import pytest
6
6
from subprocess import Popen , PIPE , STDOUT
7
7
from textwrap import dedent
8
- from utils import compile_source , app
8
+ from conftest import compile_source
9
9
10
10
try :
11
11
import requests
@@ -54,29 +54,33 @@ def test_mem_usage():
54
54
print (proc .stdout .read ())
55
55
56
56
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" )
59
59
60
60
print (f"------- Top { limit } differences { label } -------" )
61
61
for stat in top_stats [:limit ]:
62
62
print (stat )
63
63
64
64
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
+ )
70
72
top_stats = snapshot .statistics (key_type )
71
73
72
74
print ("Top %s lines" % limit )
73
75
for index , stat in enumerate (top_stats [:limit ], 1 ):
74
76
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
+ )
77
81
line = linecache .getline (frame .filename , frame .lineno ).strip ()
78
82
if line :
79
- print (' %s' % line )
83
+ print (" %s" % line )
80
84
81
85
other = top_stats [limit :]
82
86
if other :
@@ -86,9 +90,25 @@ def display_top(snapshot, key_type='lineno', limit=10):
86
90
print ("Total allocated size: %.1f KiB" % (total / 1024 ))
87
91
88
92
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 ():
90
101
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
+ """
92
112
from web.components.api import *
93
113
from web.core.api import *
94
114
enamldef Page(Html): view:
@@ -108,27 +128,43 @@ def test_html_size(app):
108
128
Td:
109
129
text = f"{loop.item}"
110
130
111
- """ ), "Page" )
131
+ """
132
+ ),
133
+ "Page" ,
134
+ )
135
+ snapshots = []
136
+ snapshots .append (tracemalloc .take_snapshot ())
137
+ display_top (snapshots [- 1 ])
112
138
113
- snapshot1 = tracemalloc .take_snapshot ()
114
- display_top (snapshot1 )
139
+ t0 = time .time ()
115
140
view = Page ()
116
- snapshot2 = tracemalloc .take_snapshot ()
117
- display_top (snapshot2 )
118
- diff_stats (snapshot1 , snapshot2 , "after construct" , 20 )
119
141
142
+ print (f"\n \n \n Construct 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 \n Init 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 \n Activate took { time .time ()- t0 } " )
153
+ snap_and_show_stats (snapshots , "after activate" )
154
+
155
+ t0 = time .time ()
120
156
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 \n Render took { time .time ()- t0 } " )
158
+ snap_and_show_stats (snapshots , "after render" )
124
159
125
- assert len (content ) < 2_500_000
126
160
total_size = 0
127
161
num_nodes = 0
128
162
for node in view .traverse ():
129
163
num_nodes += 1
130
164
total_size += sys .getsizeof (node )
131
165
assert total_size < 5_000_000
166
+ assert len (content ) < 2_500_000
167
+ assert False
132
168
133
169
134
170
if __name__ == "__main__" :
0 commit comments