#!/usr/bin/env python # -*- coding: utf-8 -*- # This file is a hacked version of line_profiler.py from the kernprof package # https://github.com/rkern/line_profiler # It has all IPython hardcoded dependencies removed in order to make it # usable inside Blender. # Modification 2016 12 23 1415 by Michel J. Anders (varkenvarken) # Article on how to use it: # http://blog.michelanders.nl/2016/12/profiling-using-kernprof-in-blender-addons.html #This software is OSI Certified Open Source Software. #OSI Certified is a certification mark of the Open Source Initiative. #Copyright (c) 2008, Enthought, Inc. #All rights reserved. #Redistribution and use in source and binary forms, with or without #modification, are permitted provided that the following conditions are met: #* Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. #* Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. #* Neither the name of Enthought, Inc. nor the names of its contributors may # be used to endorse or promote products derived from this software without # specific prior written permission. #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED #WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR #ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES #(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; #LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON #ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT #(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS #SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from __future__ import print_function try: import cPickle as pickle except ImportError: import pickle try: from cStringIO import StringIO except ImportError: from io import StringIO import functools import inspect import linecache import optparse import os import sys from _line_profiler import LineProfiler as CLineProfiler # Python 2/3 compatibility utils # =========================================================== PY3 = sys.version_info[0] == 3 # exec (from https://bitbucket.org/gutworth/six/): if PY3: import builtins exec_ = getattr(builtins, "exec") del builtins else: def exec_(_code_, _globs_=None, _locs_=None): """Execute code in a namespace.""" if _globs_ is None: frame = sys._getframe(1) _globs_ = frame.f_globals if _locs_ is None: _locs_ = frame.f_locals del frame elif _locs_ is None: _locs_ = _globs_ exec("""exec _code_ in _globs_, _locs_""") # ============================================================ CO_GENERATOR = 0x0020 def is_generator(f): """ Return True if a function is a generator. """ isgen = (f.__code__.co_flags & CO_GENERATOR) != 0 return isgen class LineProfiler(CLineProfiler): """ A profiler that records the execution times of individual lines. """ def __call__(self, func): """ Decorate a function to start the profiler on function entry and stop it on function exit. """ self.add_function(func) if is_generator(func): wrapper = self.wrap_generator(func) else: wrapper = self.wrap_function(func) return wrapper def wrap_generator(self, func): """ Wrap a generator to profile it. """ @functools.wraps(func) def wrapper(*args, **kwds): g = func(*args, **kwds) # The first iterate will not be a .send() self.enable_by_count() try: item = next(g) finally: self.disable_by_count() input = (yield item) # But any following one might be. while True: self.enable_by_count() try: item = g.send(input) finally: self.disable_by_count() input = (yield item) return wrapper def wrap_function(self, func): """ Wrap a function to profile it. """ @functools.wraps(func) def wrapper(*args, **kwds): self.enable_by_count() try: result = func(*args, **kwds) finally: self.disable_by_count() return result return wrapper def dump_stats(self, filename): """ Dump a representation of the data to a file as a pickled LineStats object from `get_stats()`. """ lstats = self.get_stats() with open(filename, 'wb') as f: pickle.dump(lstats, f, pickle.HIGHEST_PROTOCOL) def print_stats(self, stream=None, stripzeros=False): """ Show the gathered statistics. """ lstats = self.get_stats() show_text(lstats.timings, lstats.unit, stream=stream, stripzeros=stripzeros) def run(self, cmd): """ Profile a single executable statment in the main namespace. """ import __main__ main_dict = __main__.__dict__ return self.runctx(cmd, main_dict, main_dict) def runctx(self, cmd, globals, locals): """ Profile a single executable statement in the given namespaces. """ self.enable_by_count() try: exec_(cmd, globals, locals) finally: self.disable_by_count() return self def runcall(self, func, *args, **kw): """ Profile a single function call. """ self.enable_by_count() try: return func(*args, **kw) finally: self.disable_by_count() def add_module(self, mod): """ Add all the functions in a module and its classes. """ from inspect import isclass, isfunction nfuncsadded = 0 for item in mod.__dict__.values(): if isclass(item): for k, v in item.__dict__.items(): if isfunction(v): self.add_function(v) nfuncsadded += 1 elif isfunction(item): self.add_function(item) nfuncsadded += 1 return nfuncsadded def show_func(filename, start_lineno, func_name, timings, unit, stream=None, stripzeros=False): """ Show results for a single function. """ if stream is None: stream = sys.stdout template = '%6s %9s %12s %8s %8s %-s' d = {} total_time = 0.0 linenos = [] for lineno, nhits, time in timings: total_time += time linenos.append(lineno) if stripzeros and total_time == 0: return stream.write("Total time: %g s\n" % (total_time * unit)) if os.path.exists(filename) or filename.startswith("