如何在AppEngine中测量Python导入加载时间/延迟

时间:2012-01-28 10:22:00

标签: python google-app-engine import module profiling

如何在import中测量Python模块的AppEngine加载时间/延迟?

我想收到这样的报告:

module_name -> 1s
...

我已经基于sys.path_hooks__import__修补实现/测试了一些解决方案,但它适用于纯Python,但不适用于AppEngine

任何有AppEngine / Python或纯Python经验的人都建议/分享一些提示吗?

2 个答案:

答案 0 :(得分:1)

Google App Engine caches imports这可能会使path_hooks陷入困境 由于导入的是缓存,因此它们通常不会在生产环境中引起任何重大问题。

答案 1 :(得分:0)

我发现这样的解决方案可以在App Engine上跟踪时间 - 不完全线程安全 - 您的想法是什么?

module.py

# coding=utf-8
## \authors Cezary K. Wagner

from threading import Lock

__indent = 0
__patched = False

def logImports():
  global __patched
  if __patched == True:
    return

  import time
  import logging
#  import inspect
  import __builtin__

  original__import__ = __builtin__.__import__

  def patched__import__(name, *args):
    global __indent
    lock = Lock()
    lock.acquire()
    spaces = ' ' * (__indent * 2)
#    callerFrame = inspect.currentframe().f_back
#    moduleName = callerFrame.f_globals['__name__']
#    lineNumber = callerFrame.f_lineno
#    logging.debug('%simport start %s %s:%s.' % (spaces, name, moduleName, lineNumber))
    __indent += 1
    start = time.clock()
    try:
      module = original__import__(name, *args)
      delta = time.clock() - start
    # import could fail
    finally:
      lock.release
      __indent -= 1
    if delta >= 1.0:
      logging.debug('%simport end %s in %ss' % (spaces, name, delta))
    return module

  __builtin__.__import__ = patched__import__
  __patched = True

usage.py

from module import logImports
logImports()

import math