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
|
import cProfile
import pstats
import sys
import unittest
from pyprof2calltree import CalltreeConverter
from .profile_code import expected_output_py2, expected_output_py3, top
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
if sys.version_info < (3, 0):
expected_output = expected_output_py2
else:
expected_output = expected_output_py3
class MockTimeProfile(cProfile.Profile):
def __init__(self):
self._mock_time = 0
super(MockTimeProfile, self).__init__(self._timer, 1e-9)
def _timer(self):
now = self._mock_time
self._mock_time += 1000
return now
class TestIntegration(unittest.TestCase):
def setUp(self):
self.profile = MockTimeProfile()
self.profile.enable()
top()
self.profile.disable()
def test_direct_entries(self):
entries = self.profile.getstats()
converter = CalltreeConverter(entries)
out_file = StringIO()
converter.output(out_file)
self.assertEqual(out_file.getvalue(), expected_output)
def test_pstats_data(self):
stats = pstats.Stats(self.profile)
converter = CalltreeConverter(stats)
out_file = StringIO()
converter.output(out_file)
self.assertEqual(out_file.getvalue(), expected_output)
|