Changeset - 5e905ff44e84
[Not reviewed]
default
0 2 0
drewp@bigasterisk.com - 11 years ago 2014-06-15 03:09:48
drewp@bigasterisk.com
CC now suports statprof profiling, and every other profile() call is now broken
Ignore-this: bc7c31af6598e7c132d2f847afe8de68
2 files changed with 11 insertions and 2 deletions:
0 comments (0 inline, 0 general)
bin/curvecalc
Show inline comments
 
@@ -397,117 +397,117 @@ class Main(object):
 
                reload(curveview)
 

	
 
                # old ones are not getting deleted right
 
                if hasattr(self, 'curvesetView'):
 
                    self.curvesetView.live = False
 

	
 
                # mem problem somewhere; need to hold a ref to this
 
                self.curvesetView = curveview.Curvesetview(self.graph, 
 
                    curvesVBox, zoomControlBox, self.curveset)
 
                self.curvesetView._mtimes = mtimes
 

	
 
                # this is scheduled after some tk shuffling, to
 
                # try to minimize the number of times we redraw
 
                # the curve at startup. If tk is very slow, it's
 
                # ok. You'll just get some wasted redraws.
 
                self.curvesetView.goLive()
 
            except Exception:
 
                print "reload failed:"
 
                traceback.print_exc()
 
        if self.opts.reload:
 
            reactor.callLater(1, self.refreshCurveView)
 

	
 

	
 
class MaxTime(object):
 
    """
 
    looks up the time in seconds for the session's current song
 
    """
 
    def __init__(self, graph, session):
 
        self.graph, self.session = graph, session
 
        graph.addHandler(self.update)
 

	
 
    def update(self):
 
        song = self.graph.value(self.session, L9['currentSong'])
 
        if song is None:
 
            self.maxtime = 0
 
            return
 
        musicfilename = showconfig.songOnDisk(song)
 
        self.maxtime = wavelength(musicfilename)
 
        log.info("new max time %r", self.maxtime)
 
        dispatcher.send("max time", maxtime=self.maxtime)
 

	
 
    def get(self):
 
        return self.maxtime
 

	
 
def launch(args, graph, session, opts, startTime, music):
 

	
 
    try:
 
        song = URIRef(args[0])
 
        graph.patchObject(context=session,
 
                          subject=session,
 
                          predicate=L9['currentSong'],
 
                          newObject=song)
 
    except IndexError:
 
        pass
 

	
 
    curveset = Curveset(graph=graph, session=session)
 
        
 
    log.debug("startup: output %s", time.time() - startTime)
 

	
 
    mt = MaxTime(graph, session)
 
    dispatcher.connect(lambda: mt.get(), "get max time", weak=False)
 

	
 
    start = Main(graph, opts, session, curveset, music)
 
    out = Output(graph, session, music, curveset, start.currentSubterms)
 

	
 

	
 
    dispatcher.send("show all")
 
        
 
    if opts.startup_only:
 
        log.debug("quitting now because of --startup-only")
 
        return
 

	
 
    def hoverTimeResponse(requestHandler):
 
        results = dispatcher.send("onPlayPause")
 
        times = [t for listener, t in results if t is not None]
 
        if not times:
 
            requestHandler.set_status(404)
 
            requestHandler.write("not hovering over any time")
 
            return
 
        with graph.currentState(
 
                tripleFilter=(session, L9['currentSong'], None)) as g:
 
            song = g.value(session, L9['currentSong'])
 
            json.dump({"song": song, "hoverTime" : times[0]}, requestHandler)
 
        
 
    serveCurveEdit(networking.curveCalc.port, hoverTimeResponse, start.curveset)
 

	
 
def main():
 
    startTime = time.time()
 
    parser = optparse.OptionParser()
 
    parser.set_usage("%prog [opts] [songURI]")
 
    parser.add_option("--debug", action="store_true",
 
                      help="log at DEBUG")
 
    parser.add_option("--reload", action="store_true",
 
                      help="live reload of themes and code")
 
    parser.add_option("--startup-only", action='store_true',
 
                      help="quit after loading everything (for timing tests)")
 
    parser.add_option("--profile", action='store_true', help='profile')
 
    parser.add_option("--profile", help='"hotshot" or "stat"')
 
    clientsession.add_option(parser)
 
    opts, args = parser.parse_args()
 

	
 
    log.setLevel(logging.DEBUG if opts.debug else logging.INFO)
 

	
 
    log.debug("startup: music %s", time.time() - startTime)
 

	
 

	
 
    session = clientsession.getUri('curvecalc', opts)
 

	
 
    music = Music()
 
    graph = SyncedGraph(networking.rdfdb.url, "curvecalc")
 

	
 
    graph.initiallySynced.addCallback(
 
        lambda _: launch(args, graph, session, opts, startTime, music))
 
    from light9 import prof
 
    prof.run(reactor.run, profile=opts.profile)
 

	
 
main()
 

	
light9/prof.py
Show inline comments
 
import sys, traceback, time, logging
 
log = logging.getLogger()
 

	
 
def run(main, profile=False):
 
def run(main, profile=None):
 
    if not profile:
 
        main()
 
        return
 
    
 
    if profile == 'hotshot':
 
    import hotshot, hotshot.stats
 
    p = hotshot.Profile("/tmp/pro")
 
    p.runcall(main)
 
    p.close()
 
    hotshot.stats.load("/tmp/pro").sort_stats('cumulative').print_stats()
 
    elif profile == 'stat':
 
        import statprof
 
        statprof.start()
 
        try:
 
            main()
 
        finally:
 
            statprof.stop()
 
            statprof.display()
 
    
 
def watchPoint(filename, lineno, event="call"):
 
    """whenever we hit this line, print a stack trace. event='call'
 
    for lines that are function definitions, like what a profiler
 
    gives you.
 

	
 
    Switch to 'line' to match lines inside functions. Execution speed
 
    will be much slower."""
 
    seenTraces = {} # trace contents : count
 
    def trace(frame, ev, arg):
 
        if ev == event:
 
            if (frame.f_code.co_filename, frame.f_lineno) == (filename, lineno):
 
                stack = ''.join(traceback.format_stack(frame))
 
                if stack not in seenTraces:
 
                    print "watchPoint hit"
 
                    print stack
 
                    seenTraces[stack] = 1
 
                else:
 
                    seenTraces[stack] += 1
 

	
 
        return trace
 
    sys.settrace(trace)
 

	
 
    # atexit, print the frequencies?
 

	
 
def logTime(func):
 
    def inner(*args, **kw):
 
        t1 = time.time()
 
        try:
 
            ret = func(*args, **kw)
 
        finally:
 
            log.info("Call to %s took %.1f ms" % (
 
                func.__name__, 1000 * (time.time() - t1)))
 
        return ret
 
    return inner
0 comments (0 inline, 0 general)