adding new stuff

This commit is contained in:
ViktorBarzin 2017-07-09 00:22:01 +03:00
parent f84d7183aa
commit 9ef8a96f9a
1580 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,58 @@
#! /usr/bin/env python
"""
This is a convenience script to test the speed and memory usage of Jedi with
large libraries.
Each library is preloaded by jedi, recording the time and memory consumed by
each operation.
You can provide additional libraries via command line arguments.
Note: This requires the psutil library, available on PyPI.
"""
import time
import sys
import os
import psutil
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/..'))
import jedi
def used_memory():
"""Return the total MB of System Memory in use."""
return psutil.virtual_memory().used / 2 ** 20
def profile_preload(mod):
"""Preload a module into Jedi, recording time and memory used."""
base = used_memory()
t0 = time.time()
jedi.preload_module(mod)
elapsed = time.time() - t0
used = used_memory() - base
return elapsed, used
def main(mods):
"""Preload the modules, and print the time and memory used."""
t0 = time.time()
baseline = used_memory()
print('Time (s) | Mem (MB) | Package')
print('------------------------------')
for mod in mods:
elapsed, used = profile_preload(mod)
if used > 0:
print('%8.2f | %8d | %s' % (elapsed, used, mod))
print('------------------------------')
elapsed = time.time() - t0
used = used_memory() - baseline
print('%8.2f | %8d | %s' % (elapsed, used, 'Total'))
if __name__ == '__main__':
if sys.argv[1:]:
mods = sys.argv[1:]
else:
mods = ['re', 'numpy', 'scipy', 'scipy.sparse', 'scipy.stats',
'wx', 'decimal', 'PyQt4.QtGui', 'PySide.QtGui', 'Tkinter']
main(mods)

View file

@ -0,0 +1,49 @@
#!/usr/bin/env python
"""
Profile a piece of Python code with ``cProfile``. Tries a completion on a
certain piece of code.
Usage:
profile.py [<code>] [-n <number>] [-d] [-o] [-s <sort>]
profile.py -h | --help
Options:
-h --help Show this screen.
-n <number> Number of passes before profiling [default: 1].
-d --debug Enable Jedi internal debugging.
-o --omit Omit profiler, just do a normal run.
-s <sort> Sort the profile results, e.g. cum, name [default: time].
"""
import time
import cProfile
from docopt import docopt
import jedi
def run(code, index):
start = time.time()
result = jedi.Script(code).completions()
print('Used %ss for the %sth run.' % (time.time() - start, index + 1))
return result
def main(args):
code = args['<code>']
n = int(args['-n'])
for i in range(n):
run(code, i)
jedi.set_debug_function(notices=args['--debug'])
if args['--omit']:
run(code, n)
else:
cProfile.runctx('run(code, n)', globals(), locals(), sort=args['-s'])
if __name__ == '__main__':
args = docopt(__doc__)
if args['<code>'] is None:
args['<code>'] = 'import numpy; numpy.array([0])'
main(args)

View file

@ -0,0 +1,61 @@
#! /usr/bin/env python
"""
Depends: ``objgraph`` (third party Python library)
``wx._core`` is a very nice module to test Jedi's speed and memory performance
on big Python modules. Its size is ~16kLOC (one file). It also seems to look
like a typical big Python modules. A mix between a lot of different Python
things.
You can view a markup version of it here:
http://svn.wxwidgets.org/viewvc/wx/wxPython/trunk/src/gtk/_core.py?view=markup
"""
import resource
import time
import sys
try:
import urllib.request as urllib2
except ImportError:
import urllib2
import gc
from os.path import abspath, dirname
import objgraph
sys.path.insert(0, dirname(dirname(abspath(__file__))))
import jedi
def process_memory():
"""
In kB according to
http://stackoverflow.com/questions/938733/total-memory-used-by-python-process
"""
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
uri = 'http://svn.wxwidgets.org/viewvc/wx/wxPython/trunk/src/gtk/_core.py?revision=74740&content-type=text%2Fplain&view=co'
wx_core = urllib2.urlopen(uri).read()
def run():
start = time.time()
print('Process Memory before: %skB' % process_memory())
# After this the module should be cached.
# Need to invent a path so that it's really cached.
jedi.Script(wx_core, path='foobar.py').completions()
gc.collect() # make sure that it's all fair and the gc did its job.
print('Process Memory after: %skB' % process_memory())
print(objgraph.most_common_types(limit=50))
print('\nIt took %s seconds to parse the file.' % (time.time() - start))
print('First pass')
run()
print('\nSecond pass')
run()
print('\nThird pass')
run()