this repo has no description
1# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2import os
3import sys
4import unittest
5import warnings
6from tempfile import TemporaryDirectory
7
8from test_support import pyro_only
9
10try:
11 import _profiler
12except ImportError:
13 pass
14
15
16@pyro_only
17@unittest.skipIf(
18 "PYRO_CPP_INTERPRETER" in os.environ,
19 "opcode counting/profiling not support in C++ interpreter",
20)
21class ProfilerTest(unittest.TestCase):
22 def test_dump_callgrind_writes_file(self):
23 def bar():
24 pass
25
26 def foo():
27 for _ in range(5):
28 bar()
29
30 with TemporaryDirectory() as temp_dir:
31 warnings.filterwarnings(
32 action="ignore",
33 category=RuntimeWarning,
34 message="Interpreter switching .*",
35 module="_builtins",
36 )
37 _profiler.install()
38 foo()
39 _profiler.dump_callgrind(f"{temp_dir}/profile.cg")
40 _profiler.uninstall()
41
42 with open(f"{temp_dir}/profile.cg") as fp:
43 contents = fp.read()
44
45 this_module = sys.modules[__name__]
46
47 expected = f""".*\
48# callgrind format
49version: 1
50creator: _profiler
51positions: line
52events: Op
53
54fl=\\(0\\) <empty>
55fn=\\(0\\) _builtins._profiler_exclude
560 1
57
58fn=\\(1\\) _builtins._range_ctor_stop
590 1
60
61fl=\\(1\\) {this_module.__file__}
62fn=\\(2\\) __main__.ProfilerTest.test_dump_callgrind_writes_file.<locals>.bar
63[0-9]+ [0-9]+
64
65fl=\\(2\\) {_profiler.__file__}
66fn=\\(3\\) _profiler.dump_callgrind
67[0-9]+ 0
68cfi=\\(0\\)
69cfn=\\(0\\)
70calls=1 0
71[0-9]+ 1
72
73fl=\\(1\\)
74fn=\\(4\\) __main__.ProfilerTest.test_dump_callgrind_writes_file.<locals>.foo
75[0-9]+ [0-9]+
76cfn=\\(1\\)
77calls=1 0
78[0-9]+ 1
79cfi=\\(1\\)
80cfn=\\(2\\)
81calls=5 [0-9]+
82[0-9]+ [0-9]+
83
84fn=\\(5\\) test_dump_callgrind_writes_file
85[0-9]+ 0
86cfn=\\(4\\)
87calls=1 [0-9]+
88[0-9]+ [0-9]+
89cfi=\\(2\\)
90cfn=\\(3\\)
91calls=1 [0-9]+
92[0-9]+ 0.*"""
93 self.assertRegex(contents, expected)
94
95
96if __name__ == "__main__":
97 unittest.main()