Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_version():
vcall = sp.Popen(
['snakeviz', '--version'], stdout=sp.PIPE, stderr=sp.PIPE)
out, err = vcall.communicate()
# in Python <= 3.3 this comes out on stderr, otherwise on stdout
assert version.version in out.decode('utf-8') or \
version.version in err.decode('utf-8')
def test_version():
vcall = sp.Popen(
['snakeviz', '--version'], stdout=sp.PIPE, stderr=sp.PIPE)
out, err = vcall.communicate()
# in Python <= 3.3 this comes out on stderr, otherwise on stdout
assert version.version in out.decode('utf-8') or \
version.version in err.decode('utf-8')
self.location_rows = self.rows.copy()
for child in self.rows.values():
current = directories.get( child.directory )
directory, filename = child.directory, child.filename
if current is None:
if directory == '':
current = root
else:
current = PStatLocation( directory, '' )
self.location_rows[ current.key ] = current
directories[ directory ] = current
if filename == '~':
filename = ''
file_current = files.get( (directory,filename) )
if file_current is None:
file_current = PStatLocation( directory, filename )
self.location_rows[ file_current.key ] = file_current
files[ (directory,filename) ] = file_current
current.children.append( file_current )
file_current.children.append( child )
# now link the directories...
for key,value in directories.items():
if value is root:
continue
found = False
while key:
new_key,rest = os.path.split( key )
if new_key == key:
break
key = new_key
parent = directories.get( key )
if parent:
benchmark(metadata.main)(args)
elif args.func == 'export':
benchmark(export.main)(args)
elif args.func == 'export-html':
benchmark(export_html.main)(args)
elif args.func == 'import':
benchmark(tezimport.main)(args)
if args.profile:
try:
import snakeviz.cli
print("\n\n")
snakeviz.cli.main([args.profile])
except ImportError:
print("""\nSnakeviz is not installed. Install with `pip install snakeviz`,
then run `snakeviz {}`.""".format(args.profile))
if platform.system() == 'Windows':
win_unicode_console.disable()
import sys
from .cli import main
if __name__ == "__main__":
# __main__.py is ugly and confusing, monkey patch executable to say snakeviz
sys.argv[0] = "snakeviz"
sys.exit(main())
def load_location( self ):
"""Build a squaremap-compatible model for location-based hierarchy"""
directories = {}
files = {}
root = PStatLocation( '/', 'PYTHONPATH' )
self.location_rows = self.rows.copy()
for child in self.rows.values():
current = directories.get( child.directory )
directory, filename = child.directory, child.filename
if current is None:
if directory == '':
current = root
else:
current = PStatLocation( directory, '' )
self.location_rows[ current.key ] = current
directories[ directory ] = current
if filename == '~':
filename = ''
file_current = files.get( (directory,filename) )
if file_current is None:
file_current = PStatLocation( directory, filename )
def load_location( self ):
"""Build a squaremap-compatible model for location-based hierarchy"""
directories = {}
files = {}
root = PStatLocation( '/', 'PYTHONPATH' )
self.location_rows = self.rows.copy()
for child in self.rows.values():
current = directories.get( child.directory )
directory, filename = child.directory, child.filename
if current is None:
if directory == '':
current = root
else:
current = PStatLocation( directory, '' )
self.location_rows[ current.key ] = current
directories[ directory ] = current
if filename == '~':
filename = ''
file_current = files.get( (directory,filename) )
if file_current is None:
file_current = PStatLocation( directory, filename )
self.location_rows[ file_current.key ] = file_current
files[ (directory,filename) ] = file_current
current.children.append( file_current )
file_current.children.append( child )
# now link the directories...
for key,value in directories.items():
if value is root:
continue
found = False
def __init__( self, directory, filename, tree=TREE_FILES):
super( PStatLocation, self ).__init__( directory=directory, filename=filename, name='package', tree=tree )
def filter_children( self ):
tree_dict : dict
Tree of nested dictionaries representing the profile call tree.
"""
# recursive_seen prevents us from repeatedly traversing
# recursive structures. only want to show the first set.
if recursive_seen is None:
recursive_seen = set()
d = {}
d['name'] = node.name
d['filename'] = node.filename
d['directory'] = node.directory
if isinstance(node, pstatsloader.PStatRow):
d['calls'] = node.calls
d['recursive'] = node.recursive
d['local'] = node.local
d['localPer'] = node.localPer
d['cumulative'] = node.cummulative
d['cumulativePer'] = node.cummulativePer
d['line_number'] = node.lineno
recursive_seen.add(node)
if parent:
# figure out the size of this node. This is an arbitrary value
# but it's important that the child size is no larger
# than the parent size.
if isinstance(parent, pstatsloader.PStatGroup):
if parent.cummulative:
def prof_to_json(prof_name):
"""
Convert profiles stats in a `pstats` compatible file to a JSON string.
Parameters
----------
prof_name : str
Path to to a `pstats` compatible profile.
Returns
-------
json_stats : str
Profile as a JSON string.
"""
loader = pstatsloader.PStatsLoader(prof_name)
d = _stats_to_tree_dict(loader.tree.children[0])
return json.dumps(d, indent=1)