Debian Codegraph @

benchmarks/enaml_like_benchmark.py

raw
1 # --------------------------------------------------------------------------------------
2 # Copyright (c) 2019-2024, Nucleic Development Team.
3 #
4 # Distributed under the terms of the Modified BSD License.
5 #
6 # The full license is in the file LICENSE, distributed with this software.
7 # --------------------------------------------------------------------------------------
8 """Time updating an EditVariable in a set of constraints typical of enaml use."""
9
10 import perf
11
12 from kiwisolver import Solver, Variable, strength
13
14 solver = Solver()
15
16 # Create custom strength
17 mmedium = strength.create(0, 1, 0, 1.25)
18 smedium = strength.create(0, 100, 0)
19
20 # Create the variable
21 left = Variable("left")
22 height = Variable("height")
23 top = Variable("top")
24 width = Variable("width")
25 contents_top = Variable("contents_top")
26 contents_bottom = Variable("contents_bottom")
27 contents_left = Variable("contents_left")
28 contents_right = Variable("contents_right")
29 midline = Variable("midline")
30 ctleft = Variable("ctleft")
31 ctheight = Variable("ctheight")
32 cttop = Variable("cttop")
33 ctwidth = Variable("ctwidth")
34 lb1left = Variable("lb1left")
35 lb1height = Variable("lb1height")
36 lb1top = Variable("lb1top")
37 lb1width = Variable("lb1width")
38 lb2left = Variable("lb2left")
39 lb2height = Variable("lb2height")
40 lb2top = Variable("lb2top")
41 lb2width = Variable("lb2width")
42 lb3left = Variable("lb3left")
43 lb3height = Variable("lb3height")
44 lb3top = Variable("lb3top")
45 lb3width = Variable("lb3width")
46 fl1left = Variable("fl1left")
47 fl1height = Variable("fl1height")
48 fl1top = Variable("fl1top")
49 fl1width = Variable("fl1width")
50 fl2left = Variable("fl2left")
51 fl2height = Variable("fl2height")
52 fl2top = Variable("fl2top")
53 fl2width = Variable("fl2width")
54 fl3left = Variable("fl3left")
55 fl3height = Variable("fl3height")
56 fl3top = Variable("fl3top")
57 fl3width = Variable("fl3width")
58
59 # Add the edit variables
60 solver.addEditVariable(width, "strong")
61 solver.addEditVariable(height, "strong")
62
63 # Add the constraints
64 for c in [
65 (left + -0 >= 0) | "required",
66 (height + 0 == 0) | "medium",
67 (top + -0 >= 0) | "required",
68 (width + -0 >= 0) | "required",
69 (height + -0 >= 0) | "required",
70 (-top + contents_top + -10 == 0) | "required",
71 (lb3height + -16 == 0) | "strong",
72 (lb3height + -16 >= 0) | "strong",
73 (ctleft + -0 >= 0) | "required",
74 (cttop + -0 >= 0) | "required",
75 (ctwidth + -0 >= 0) | "required",
76 (ctheight + -0 >= 0) | "required",
77 (fl3left + -0 >= 0) | "required",
78 (ctheight + -24 >= 0) | smedium,
79 (ctwidth + -1.67772e07 <= 0) | smedium,
80 (ctheight + -24 <= 0) | smedium,
81 (fl3top + -0 >= 0) | "required",
82 (fl3width + -0 >= 0) | "required",
83 (fl3height + -0 >= 0) | "required",
84 (lb1width + -67 == 0) | "weak",
85 (lb2width + -0 >= 0) | "required",
86 (lb2height + -0 >= 0) | "required",
87 (fl2height + -0 >= 0) | "required",
88 (lb3left + -0 >= 0) | "required",
89 (fl2width + -125 >= 0) | "strong",
90 (fl2height + -21 == 0) | "strong",
91 (fl2height + -21 >= 0) | "strong",
92 (lb3top + -0 >= 0) | "required",
93 (lb3width + -0 >= 0) | "required",
94 (fl1left + -0 >= 0) | "required",
95 (fl1width + -0 >= 0) | "required",
96 (lb1width + -67 >= 0) | "strong",
97 (fl2left + -0 >= 0) | "required",
98 (lb2width + -66 == 0) | "weak",
99 (lb2width + -66 >= 0) | "strong",
100 (lb2height + -16 == 0) | "strong",
101 (fl1height + -0 >= 0) | "required",
102 (fl1top + -0 >= 0) | "required",
103 (lb2top + -0 >= 0) | "required",
104 (-lb2top + lb3top + -lb2height + -10 == 0) | mmedium,
105 (-lb3top + -lb3height + fl3top + -10 >= 0) | "required",
106 (-lb3top + -lb3height + fl3top + -10 == 0) | mmedium,
107 (contents_bottom + -fl3height + -fl3top + -0 == 0) | mmedium,
108 (fl1top + -contents_top + 0 >= 0) | "required",
109 (fl1top + -contents_top + 0 == 0) | mmedium,
110 (contents_bottom + -fl3height + -fl3top + -0 >= 0) | "required",
111 (-left + -width + contents_right + 10 == 0) | "required",
112 (-top + -height + contents_bottom + 10 == 0) | "required",
113 (-left + contents_left + -10 == 0) | "required",
114 (lb3left + -contents_left + 0 == 0) | mmedium,
115 (fl1left + -midline + 0 == 0) | "strong",
116 (fl2left + -midline + 0 == 0) | "strong",
117 (ctleft + -midline + 0 == 0) | "strong",
118 (fl1top + 0.5 * fl1height + -lb1top + -0.5 * lb1height + 0 == 0) | "strong",
119 (lb1left + -contents_left + 0 >= 0) | "required",
120 (lb1left + -contents_left + 0 == 0) | mmedium,
121 (-lb1left + fl1left + -lb1width + -10 >= 0) | "required",
122 (-lb1left + fl1left + -lb1width + -10 == 0) | mmedium,
123 (-fl1left + contents_right + -fl1width + -0 >= 0) | "required",
124 (width + 0 == 0) | "medium",
125 (-fl1top + fl2top + -fl1height + -10 >= 0) | "required",
126 (-fl1top + fl2top + -fl1height + -10 == 0) | mmedium,
127 (cttop + -fl2top + -fl2height + -10 >= 0) | "required",
128 (-ctheight + -cttop + fl3top + -10 >= 0) | "required",
129 (contents_bottom + -fl3height + -fl3top + -0 >= 0) | "required",
130 (cttop + -fl2top + -fl2height + -10 == 0) | mmedium,
131 (-fl1left + contents_right + -fl1width + -0 == 0) | mmedium,
132 (-lb2top + -0.5 * lb2height + fl2top + 0.5 * fl2height + 0 == 0) | "strong",
133 (-contents_left + lb2left + 0 >= 0) | "required",
134 (-contents_left + lb2left + 0 == 0) | mmedium,
135 (fl2left + -lb2width + -lb2left + -10 >= 0) | "required",
136 (-ctheight + -cttop + fl3top + -10 == 0) | mmedium,
137 (contents_bottom + -fl3height + -fl3top + -0 == 0) | mmedium,
138 (lb1top + -0 >= 0) | "required",
139 (lb1width + -0 >= 0) | "required",
140 (lb1height + -0 >= 0) | "required",
141 (fl2left + -lb2width + -lb2left + -10 == 0) | mmedium,
142 (-fl2left + -fl2width + contents_right + -0 == 0) | mmedium,
143 (-fl2left + -fl2width + contents_right + -0 >= 0) | "required",
144 (lb3left + -contents_left + 0 >= 0) | "required",
145 (lb1left + -0 >= 0) | "required",
146 (0.5 * ctheight + cttop + -lb3top + -0.5 * lb3height + 0 == 0) | "strong",
147 (ctleft + -lb3left + -lb3width + -10 >= 0) | "required",
148 (-ctwidth + -ctleft + contents_right + -0 >= 0) | "required",
149 (ctleft + -lb3left + -lb3width + -10 == 0) | mmedium,
150 (fl3left + -contents_left + 0 >= 0) | "required",
151 (fl3left + -contents_left + 0 == 0) | mmedium,
152 (-ctwidth + -ctleft + contents_right + -0 == 0) | mmedium,
153 (-fl3left + contents_right + -fl3width + -0 == 0) | mmedium,
154 (-contents_top + lb1top + 0 >= 0) | "required",
155 (-contents_top + lb1top + 0 == 0) | mmedium,
156 (-fl3left + contents_right + -fl3width + -0 >= 0) | "required",
157 (lb2top + -lb1top + -lb1height + -10 >= 0) | "required",
158 (-lb2top + lb3top + -lb2height + -10 >= 0) | "required",
159 (lb2top + -lb1top + -lb1height + -10 == 0) | mmedium,
160 (fl1height + -21 == 0) | "strong",
161 (fl1height + -21 >= 0) | "strong",
162 (lb2left + -0 >= 0) | "required",
163 (lb2height + -16 >= 0) | "strong",
164 (fl2top + -0 >= 0) | "required",
165 (fl2width + -0 >= 0) | "required",
166 (lb1height + -16 >= 0) | "strong",
167 (lb1height + -16 == 0) | "strong",
168 (fl3width + -125 >= 0) | "strong",
169 (fl3height + -21 == 0) | "strong",
170 (fl3height + -21 >= 0) | "strong",
171 (lb3height + -0 >= 0) | "required",
172 (ctwidth + -119 >= 0) | smedium,
173 (lb3width + -24 == 0) | "weak",
174 (lb3width + -24 >= 0) | "strong",
175 (fl1width + -125 >= 0) | "strong",
176 ]:
177 solver.addConstraint(c)
178
179
180 def bench_update_variables(loops, solver):
181 """Suggest new values and update variables.
182
183 This mimic the use of kiwi in enaml in the case of a resizing.
184
185 """
186 t0 = perf.perf_counter()
187 for w, h in [
188 (400, 600),
189 (600, 400),
190 (800, 1200),
191 (1200, 800),
192 (400, 800),
193 (800, 400),
194 ] * loops:
195 solver.suggestValue(width, w)
196 solver.suggestValue(height, h)
197 solver.updateVariables()
198
199 return perf.perf_counter() - t0
200
201
202 runner = perf.Runner()
203 runner.bench_time_func(
204 "kiwi.suggestValue", bench_update_variables, solver, inner_loops=1
205 )