+++ /dev/null
-"""Test suite for the profile module."""\r
-\r
-import sys\r
-import pstats\r
-import unittest\r
-from StringIO import StringIO\r
-from test.test_support import run_unittest\r
-\r
-import profile\r
-from test.profilee import testfunc, timer\r
-\r
-\r
-class ProfileTest(unittest.TestCase):\r
-\r
- profilerclass = profile.Profile\r
- methodnames = ['print_stats', 'print_callers', 'print_callees']\r
- expected_output = {}\r
- expected_list_sort_output = ':0(sort)'\r
-\r
- @classmethod\r
- def do_profiling(cls):\r
- results = []\r
- prof = cls.profilerclass(timer, 0.001)\r
- start_timer = timer()\r
- prof.runctx("testfunc()", globals(), locals())\r
- results.append(timer() - start_timer)\r
- for methodname in cls.methodnames:\r
- s = StringIO()\r
- stats = pstats.Stats(prof, stream=s)\r
- stats.strip_dirs().sort_stats("stdname")\r
- getattr(stats, methodname)()\r
- results.append(s.getvalue())\r
- return results\r
-\r
- def test_cprofile(self):\r
- results = self.do_profiling()\r
- self.assertEqual(results[0], 1000)\r
- for i, method in enumerate(self.methodnames):\r
- self.assertEqual(results[i+1], self.expected_output[method],\r
- "Stats.%s output for %s doesn't fit expectation!" %\r
- (method, self.profilerclass.__name__))\r
-\r
- def test_calling_conventions(self):\r
- # Issue #5330: profile and cProfile wouldn't report C functions called\r
- # with keyword arguments. We test all calling conventions.\r
- stmts = [\r
- "[].sort()",\r
- "[].sort(reverse=True)",\r
- "[].sort(*(None, None, True))",\r
- "[].sort(**dict(reverse=True))",\r
- ]\r
- for stmt in stmts:\r
- s = StringIO()\r
- prof = self.profilerclass(timer, 0.001)\r
- prof.runctx(stmt, globals(), locals())\r
- stats = pstats.Stats(prof, stream=s)\r
- stats.print_stats()\r
- res = s.getvalue()\r
- self.assertIn(self.expected_list_sort_output, res,\r
- "Profiling {0!r} didn't report list.sort:\n{1}".format(stmt, res))\r
-\r
-\r
-def regenerate_expected_output(filename, cls):\r
- filename = filename.rstrip('co')\r
- print 'Regenerating %s...' % filename\r
- results = cls.do_profiling()\r
-\r
- newfile = []\r
- with open(filename, 'r') as f:\r
- for line in f:\r
- newfile.append(line)\r
- if line[:6] == '#--cut':\r
- break\r
-\r
- with open(filename, 'w') as f:\r
- f.writelines(newfile)\r
- for i, method in enumerate(cls.methodnames):\r
- f.write('%s.expected_output[%r] = """\\\n%s"""\n' % (\r
- cls.__name__, method, results[i+1]))\r
- f.write('\nif __name__ == "__main__":\n main()\n')\r
-\r
-\r
-def test_main():\r
- run_unittest(ProfileTest)\r
-\r
-def main():\r
- if '-r' not in sys.argv:\r
- test_main()\r
- else:\r
- regenerate_expected_output(__file__, ProfileTest)\r
-\r
-\r
-# Don't remove this comment. Everything below it is auto-generated.\r
-#--cut--------------------------------------------------------------------------\r
-ProfileTest.expected_output['print_stats'] = """\\r
- 127 function calls (107 primitive calls) in 999.749 seconds\r
-\r
- Ordered by: standard name\r
-\r
- ncalls tottime percall cumtime percall filename:lineno(function)\r
- 4 -0.004 -0.001 -0.004 -0.001 :0(append)\r
- 4 -0.004 -0.001 -0.004 -0.001 :0(exc_info)\r
- 12 -0.024 -0.002 11.964 0.997 :0(hasattr)\r
- 8 -0.008 -0.001 -0.008 -0.001 :0(range)\r
- 1 0.000 0.000 0.000 0.000 :0(setprofile)\r
- 1 -0.002 -0.002 999.751 999.751 <string>:1(<module>)\r
- 0 0.000 0.000 profile:0(profiler)\r
- 1 -0.002 -0.002 999.749 999.749 profile:0(testfunc())\r
- 28 27.972 0.999 27.972 0.999 profilee.py:110(__getattr__)\r
- 1 269.996 269.996 999.753 999.753 profilee.py:25(testfunc)\r
- 23/3 149.937 6.519 169.917 56.639 profilee.py:35(factorial)\r
- 20 19.980 0.999 19.980 0.999 profilee.py:48(mul)\r
- 2 39.986 19.993 599.814 299.907 profilee.py:55(helper)\r
- 4 115.984 28.996 119.964 29.991 profilee.py:73(helper1)\r
- 2 -0.006 -0.003 139.942 69.971 profilee.py:84(helper2_indirect)\r
- 8 311.976 38.997 399.896 49.987 profilee.py:88(helper2)\r
- 8 63.968 7.996 79.944 9.993 profilee.py:98(subhelper)\r
-\r
-\r
-"""\r
-ProfileTest.expected_output['print_callers'] = """\\r
- Ordered by: standard name\r
-\r
-Function was called by...\r
-:0(append) <- profilee.py:73(helper1)(4) 119.964\r
-:0(exc_info) <- profilee.py:73(helper1)(4) 119.964\r
-:0(hasattr) <- profilee.py:73(helper1)(4) 119.964\r
- profilee.py:88(helper2)(8) 399.896\r
-:0(range) <- profilee.py:98(subhelper)(8) 79.944\r
-:0(setprofile) <- profile:0(testfunc())(1) 999.749\r
-<string>:1(<module>) <- profile:0(testfunc())(1) 999.749\r
-profile:0(profiler) <-\r
-profile:0(testfunc()) <- profile:0(profiler)(1) 0.000\r
-profilee.py:110(__getattr__) <- :0(hasattr)(12) 11.964\r
- profilee.py:98(subhelper)(16) 79.944\r
-profilee.py:25(testfunc) <- <string>:1(<module>)(1) 999.751\r
-profilee.py:35(factorial) <- profilee.py:25(testfunc)(1) 999.753\r
- profilee.py:35(factorial)(20) 169.917\r
- profilee.py:84(helper2_indirect)(2) 139.942\r
-profilee.py:48(mul) <- profilee.py:35(factorial)(20) 169.917\r
-profilee.py:55(helper) <- profilee.py:25(testfunc)(2) 999.753\r
-profilee.py:73(helper1) <- profilee.py:55(helper)(4) 599.814\r
-profilee.py:84(helper2_indirect) <- profilee.py:55(helper)(2) 599.814\r
-profilee.py:88(helper2) <- profilee.py:55(helper)(6) 599.814\r
- profilee.py:84(helper2_indirect)(2) 139.942\r
-profilee.py:98(subhelper) <- profilee.py:88(helper2)(8) 399.896\r
-\r
-\r
-"""\r
-ProfileTest.expected_output['print_callees'] = """\\r
- Ordered by: standard name\r
-\r
-Function called...\r
-:0(append) ->\r
-:0(exc_info) ->\r
-:0(hasattr) -> profilee.py:110(__getattr__)(12) 27.972\r
-:0(range) ->\r
-:0(setprofile) ->\r
-<string>:1(<module>) -> profilee.py:25(testfunc)(1) 999.753\r
-profile:0(profiler) -> profile:0(testfunc())(1) 999.749\r
-profile:0(testfunc()) -> :0(setprofile)(1) 0.000\r
- <string>:1(<module>)(1) 999.751\r
-profilee.py:110(__getattr__) ->\r
-profilee.py:25(testfunc) -> profilee.py:35(factorial)(1) 169.917\r
- profilee.py:55(helper)(2) 599.814\r
-profilee.py:35(factorial) -> profilee.py:35(factorial)(20) 169.917\r
- profilee.py:48(mul)(20) 19.980\r
-profilee.py:48(mul) ->\r
-profilee.py:55(helper) -> profilee.py:73(helper1)(4) 119.964\r
- profilee.py:84(helper2_indirect)(2) 139.942\r
- profilee.py:88(helper2)(6) 399.896\r
-profilee.py:73(helper1) -> :0(append)(4) -0.004\r
- :0(exc_info)(4) -0.004\r
- :0(hasattr)(4) 11.964\r
-profilee.py:84(helper2_indirect) -> profilee.py:35(factorial)(2) 169.917\r
- profilee.py:88(helper2)(2) 399.896\r
-profilee.py:88(helper2) -> :0(hasattr)(8) 11.964\r
- profilee.py:98(subhelper)(8) 79.944\r
-profilee.py:98(subhelper) -> :0(range)(8) -0.008\r
- profilee.py:110(__getattr__)(16) 27.972\r
-\r
-\r
-"""\r
-\r
-if __name__ == "__main__":\r
- main()\r