From 7f288eb0b0d58a690f6954605ab66ac6b39e1429 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 11 Apr 2014 16:01:26 +0200 Subject: [PATCH] Add a nice and small profile script for Jedi. --- scripts/memory_check.py | 2 +- scripts/profile.py | 46 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100755 scripts/profile.py diff --git a/scripts/memory_check.py b/scripts/memory_check.py index 496520dd..e63c08fd 100755 --- a/scripts/memory_check.py +++ b/scripts/memory_check.py @@ -18,7 +18,7 @@ import jedi def used_memory(): """Return the total MB of System Memory in use.""" - return psutil.virtual_memory().used / 2**20 + return psutil.virtual_memory().used / 2 ** 20 def profile_preload(mod): diff --git a/scripts/profile.py b/scripts/profile.py new file mode 100755 index 00000000..7e583c0e --- /dev/null +++ b/scripts/profile.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +""" +Profile a piece of Python code with ``cProfile``. Tries a completion on a +certain piece of code. + +Usage: + profile.py [] [-n ] [-d] [-o] [-s ] + profile.py -h | --help + +Options: + -h --help Show this screen. + -n Number of passes before profiling [default: 1]. + -d --debug Enable Jedi internal debugging. + -o --omit Omit profiler, just do a normal run. + -s 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[''] + n = int(args['-n']) + for i in range(n): + run(code, i) + + jedi.set_debug_function(notices=args['--debug']) + cProfile.runctx('run(code, n)', globals(), locals(), sort=args['-s']) + + +if __name__ == '__main__': + args = docopt(__doc__) + if args[''] is None: + args[''] = 'import numpy; numpy.array([0])' + main(args)