Last active
December 30, 2015 11:39
-
-
Save blink1073/7824342 to your computer and use it in GitHub Desktop.
Test Jedi Memory Usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
@author Steven Silvester <steven.silvester@ieee.org> | |
@license MIT | |
""" | |
import pkgutil | |
import os | |
from wmi import WMI # pip install wmi (windows only) | |
# see http://stackoverflow.com/questions/938733/total-memory-used-by-python-process | |
import jedi | |
mods = ['numpy', 'scipy', 'sympy', 'pandas', 'networkx', 'statsmodels', | |
'matplotlib.pyplot', 'sklearn', 'skimage', 'mpmath', 'os', 'PIL', | |
'OpenGL', 'array', 'audioop', 'binascii', 'cPickle', 'cStringIO', | |
'cmath', 'collections', 'datetime', 'errno', 'exceptions', 'gc', | |
'imageop', 'imp', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', | |
'nt', 'operator', 'parser', 'rgbimg', 'signal', 'strop', 'sys', | |
'thread', 'time', 'wx', 'xxsubtype', 'zipimport', 'zlib', 'nose', | |
'PyQt4', 'PySide', 'os.path'] | |
def get_submodules(mod): | |
"""Get all submodules of a given module""" | |
def catch_exceptions(module): | |
pass | |
try: | |
m = __import__(mod) | |
submodules = [mod] | |
submods = pkgutil.walk_packages(m.__path__, m.__name__ + '.', | |
catch_exceptions) | |
for sm in submods: | |
sm_name = sm[1] | |
submodules.append(sm_name) | |
except ImportError: | |
return [] | |
except: | |
return [mod] | |
return submodules | |
submodules = [] | |
for m in mods: | |
submods = get_submodules(m) | |
submodules += submods | |
index = 1 | |
submodules = get_submodules(mods[index]) | |
def memory_mb(): | |
w = WMI('.') | |
result = w.query("SELECT WorkingSet FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess=%d" % os.getpid()) | |
return int(result[0].WorkingSet) / 2**20 | |
baseline = memory_mb() | |
print 'MB | Import' | |
print '----------------------' | |
for mod in submodules: | |
code = 'import %s' % mod | |
script = jedi.Script(code, 1, len(code)) | |
script.goto_definitions() | |
new = memory_mb() | |
if new - baseline > 10: | |
print '%3s | %s' % (new - baseline, mod) | |
baseline = new | |
print '----------------------' | |
print 'Total MB: %s' % memory_mb() | |
print 'Total files: %s' % len(submodules) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment