[Zope-CVS] CVS: Products/ZCTextIndex/tests - hs-tool.py:1.1.2.1 .cvsignore:1.1.2.1

Fred L. Drake, Jr. fdrake@acm.org
Fri, 10 May 2002 18:05:39 -0400


Update of /cvs-repository/Products/ZCTextIndex/tests
In directory cvs.zope.org:/tmp/cvs-serv7467

Added Files:
      Tag: TextIndexDS9-branch
	hs-tool.py .cvsignore 
Log Message:
Small script to help interpret HotShot logs.

=== Added File Products/ZCTextIndex/tests/hs-tool.py ===
#! /usr/bin/env python

import cPickle
import os.path
import sys

from hotshot.log import LogReader

def load_line_info(log):
    byline = {}
    prevloc = None
    for what, place, tdelta in log:
        if tdelta > 0:
            t, nhits = byline.get(prevloc, (0, 0))
            byline[prevloc] = (tdelta + t), (nhits + 1)
            prevloc = place
    return byline

def basename(path, cache={}):
    try:
        return cache[path]
    except KeyError:
        fn = os.path.split(path)[1]
        cache[path] = fn
        return fn

def print_results(results):
    for info, place in results:
        if not place:
            print 'Bad unpack:', info, place
            continue
        filename, line, funcname = place
        print '%8d %8d' % info, basename(filename), line

def get_cache_name(filename):
    d, fn = os.path.split(filename)
    cache_dir = os.path.join(d, '.hs-tool')
    cache_file = os.path.join(cache_dir, fn)
    return cache_dir, cache_file

def cache_results(filename, results):
    cache_dir, cache_file = get_cache_name(filename)
    if not os.path.exists(cache_dir):
        os.mkdir(cache_dir)
    fp = open(cache_file, 'wb')
    try:
        cPickle.dump(results, fp, 1)
    finally:
        fp.close()

def main():
    filename = 'profile.dat'
    if sys.argv[1:]:
        filename = sys.argv[1]
    cache_dir, cache_file = get_cache_name(filename)

    if (  os.path.isfile(cache_file)
          and os.path.getmtime(cache_file) > os.path.getmtime(filename)):
        # cached data is up-to-date:
        fp = open(cache_file, 'rb')
        results = cPickle.load(fp)
        fp.close()
    else:
        log = LogReader(filename)
        byline = load_line_info(log)
        # Sort
        results = [(v, k) for k, v in byline.items()]
        results.sort()
        cache_results(filename, results)

    print_results(results)


if __name__ == "__main__":
    main()


=== Added File Products/ZCTextIndex/tests/.cvsignore ===
.hs-tool