forked from mottosso/cmdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_performance.py
137 lines (107 loc) · 3.35 KB
/
test_performance.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
import os
import sys
import timeit
import contextlib
from maya import cmds, mel, OpenMaya as om1
from maya.api import OpenMaya as om2
import cmdx
from flaky import flaky
from nose.tools import (
assert_greater,
)
timings = {}
def Compare(method,
task,
func,
setup=None,
number=200,
repeat=4,
precision=1,
quiet=True):
setup = setup or (lambda: None)
text = "%s %s: %.1f ms (%.2f {precision}/call)".format(
precision="μs" if precision else "ms"
)
results = timeit.Timer(func, setup=setup).repeat(
repeat=repeat, number=number)
if not quiet:
print(text % (
task,
method,
10 ** 3 * sum(results),
10 ** (6 if precision else 3) * min(results) / number
))
if task not in timings:
timings[task] = {}
# Store for comparison
timings[task][method] = {
"func": func,
"number": number,
"results": results,
"min": min(results),
"percall": min(results) / number
}
@contextlib.contextmanager
def environment(key, value=None):
env = os.environ.copy()
os.environ[key] = value or "1"
try:
sys.modules.pop("cmdx")
__import__("cmdx")
yield
finally:
os.environ.update(env)
@contextlib.contextmanager
def pop_environment(key):
env = os.environ.copy()
os.environ.pop(key, None)
try:
sys.modules.pop("cmdx")
__import__("cmdx")
yield
finally:
os.environ.update(env)
def New(setup=None):
cmds.file(new=True, force=True)
(setup or (lambda: None))()
@flaky(max_runs=20, min_passes=3)
def test_createNode_performance():
"""createNode cmdx vs cmds > 2x"""
versions = (
("mel", lambda: mel.eval("createNode \"transform\"")),
("cmds", lambda: cmds.createNode("transform")),
("cmdx", lambda: cmdx.createNode(cmdx.tTransform)),
# ("PyMEL", lambda: pm.createNode("transform")),
("API 1.0", lambda: om1.MFnDagNode().create("transform")),
("API 2.0", lambda: om2.MFnDagNode().create("transform")),
)
for contender, test in versions:
Compare(contender, "createNode", test, setup=New)
cmdx_vs_cmds = (
timings["createNode"]["cmds"]["percall"] /
timings["createNode"]["cmdx"]["percall"]
)
cmdx_vs_api = (
timings["createNode"]["API 2.0"]["percall"] /
timings["createNode"]["cmdx"]["percall"]
)
if cmdx.__maya_version__ == 2017:
assert_greater(cmdx_vs_cmds, 0.4545) # at most 2.2x slower than cmds
assert_greater(cmdx_vs_api, 0.1818) # at most 5.5x slower than API 2.0
else:
assert_greater(cmdx_vs_cmds, 0.5) # at most 2x slower than cmds
assert_greater(cmdx_vs_api, 0.20) # at most 5x slower than API 2.0
@flaky(max_runs=20, min_passes=3)
def test_rouge_mode():
"""CMDX_ROGUE_MODE is faster"""
node = cmdx.createNode("transform")
Compare("norogue", "createNode", node.name)
with environment("CMDX_ROGUE_MODE"):
node = cmdx.createNode("transform")
Compare("rogue", "createNode", node.name)
rogue_vs_norogue = (
timings["createNode"]["norogue"]["percall"] /
timings["createNode"]["rogue"]["percall"]
)
assert_greater(rogue_vs_norogue, 0.9)