Ejemplo n.º 1
0
    def middleware(request):
        if request.path.startswith('/profiler'):
            return get_response(request)

        # print(f'[i] Starting sampling on {request.path}..')
        statprof.reset(
            getattr(settings, 'LIVEPROFILER_STATPROF_FREQUENCY', 100))
        statprof.start()

        response = get_response(request)

        statprof.stop()
        total_samples = statprof.state.sample_count
        if total_samples == 0:
            return response
        secs_per_sample = statprof.state.accumulated_time / total_samples

        # print('[i] Getting ZQM client...')
        client = get_client()
        client.insert_all([({
            'file': c.key.filename,
            'lineno': c.key.lineno,
            'function': c.key.name,
            'type': 'python',
        }, {
            'self_nsamples': c.self_sample_count,
            'cum_nsamples': c.cum_sample_count,
            'tot_nsamples': total_samples,
            'cum_time': c.cum_sample_count * secs_per_sample,
            'self_time': c.self_sample_count * secs_per_sample,
        }) for c in statprof.CallData.all_calls.values()])
        # print(f'[i] Saved {statprof.state.sample_count} samples for {request.path}.')

        return response
Ejemplo n.º 2
0
 def __enter__(self):
     try:
         statprof.reset(self.frequency)
     except AssertionError:
         pass  # statprof is already running
     statprof.start()
     return self
Ejemplo n.º 3
0
def worker(options):
    """
    Start background worker process.
    """
    workerPid = os.getpid()

    if not options.noaffinity:
        p = psutil.Process(workerPid)
        print "affinity [before]", p.cpu_affinity()
        p.cpu_affinity([options.cpuid])
        print "affinity [after]", p.cpu_affinity()

    factory = EchoServerFactory(options.wsuri)

    # The master already created the socket, just start listening and accepting
    ##
    reactor.adoptStreamPort(options.fd, AF_INET, factory)

    if not options.silence:
        print "Worker started on PID %s using factory %s and protocol %s" % (workerPid, factory, factory.protocol)
        # print "Worker %d PYPYLOG=%s" % (workerPid, os.environ.get('PYPYLOG', None))

    if options.profile:
        statprof.reset(PROFILER_FREQ)
        statprof.start()

    if not options.silence:
        def stat():
            if options.profile:
                statprof.stop()

            output = StringIO.StringIO()
            output.write("-" * 80 + "\n")
            output.write("Worker Statistics (PID %s)\n\n%s" % (workerPid, factory.stats.stats()))

            if options.profile:
                output.write("\n")
                # format = statprof.DisplayFormats.ByLine
                # format = statprof.DisplayFormats.ByMethod
                # statprof.display(output, format = format)
                statprof.display(output)

            output.write("-" * 80 + "\n\n")

            sys.stdout.write(output.getvalue())

            if options.profile:
                statprof.reset(PROFILER_FREQ)
                statprof.start()

            reactor.callLater(options.interval, stat)

        reactor.callLater(options.interval, stat)

    if False:
        import cProfile
        print "RUNNING cProfile"
        cProfile.run('reactor.run()')
    else:
        reactor.run()
Ejemplo n.º 4
0
def worker(options):
    """
    Start background worker process.
    """
    workerPid = os.getpid()

    if not options.noaffinity:
        p = psutil.Process(workerPid)
        print "affinity [before]", p.get_cpu_affinity()
        p.set_cpu_affinity([options.cpuid])
        print "affinity [after]", p.get_cpu_affinity()

    factory = EchoServerFactory(options.wsuri, debug=options.debug)

    # The master already created the socket, just start listening and accepting
    ##
    reactor.adoptStreamPort(options.fd, AF_INET, factory)

    if not options.silence:
        print "Worker started on PID %s using factory %s and protocol %s" % (workerPid, factory, factory.protocol)
        # print "Worker %d PYPYLOG=%s" % (workerPid, os.environ.get('PYPYLOG', None))

    if options.profile:
        statprof.reset(PROFILER_FREQ)
        statprof.start()

    if not options.silence:
        def stat():
            if options.profile:
                statprof.stop()

            output = StringIO.StringIO()
            output.write("-" * 80 + "\n")
            output.write("Worker Statistics (PID %s)\n\n%s" % (workerPid, factory.stats.stats()))

            if options.profile:
                output.write("\n")
                # format = statprof.DisplayFormats.ByLine
                # format = statprof.DisplayFormats.ByMethod
                # statprof.display(output, format = format)
                statprof.display(output)

            output.write("-" * 80 + "\n\n")

            sys.stdout.write(output.getvalue())

            if options.profile:
                statprof.reset(PROFILER_FREQ)
                statprof.start()

            reactor.callLater(options.interval, stat)

        reactor.callLater(options.interval, stat)

    if False:
        import cProfile
        print "RUNNING cProfile"
        cProfile.run('reactor.run()')
    else:
        reactor.run()
Ejemplo n.º 5
0
        def stat():
            if options.profile:
                statprof.stop()

            output = StringIO.StringIO()
            output.write("-" * 80 + "\n")
            output.write("Worker Statistics (PID %s)\n\n%s" %
                         (workerPid, factory.stats.stats()))

            if options.profile:
                output.write("\n")
                # format = statprof.DisplayFormats.ByLine
                # format = statprof.DisplayFormats.ByMethod
                # statprof.display(output, format = format)
                statprof.display(output)

            output.write("-" * 80 + "\n\n")

            sys.stdout.write(output.getvalue())

            if options.profile:
                statprof.reset(PROFILER_FREQ)
                statprof.start()

            reactor.callLater(options.interval, stat)
Ejemplo n.º 6
0
        def stat():
            if options.profile:
                statprof.stop()

            output = StringIO.StringIO()
            output.write("-" * 80)
            output.write("\nWorker with PID %s processed %d requests\n" %
                         (workerPid, site.cnt))

            if options.profile:
                output.write("\n")
                #format = statprof.DisplayFormats.ByLine
                #format = statprof.DisplayFormats.ByMethod
                #statprof.display(output, format = format)
                statprof.display(output)

            output.write("-" * 80)
            output.write("\n")
            output.write("\n")

            sys.stdout.write(output.getvalue())

            if options.profile:
                statprof.reset()
                statprof.start()

            reactor.callLater(options.interval, stat)
Ejemplo n.º 7
0
      def stat():
         if options.profile:
            statprof.stop()

         output = StringIO.StringIO()
         output.write("-" * 80)
         output.write("\nWorker with PID %s processed %d requests\n"  % (workerPid, site.cnt))

         if options.profile:
            output.write("\n")
            #format = statprof.DisplayFormats.ByLine
            #format = statprof.DisplayFormats.ByMethod
            #statprof.display(output, format = format)
            statprof.display(output)

         output.write("-" * 80)
         output.write("\n")
         output.write("\n")

         sys.stdout.write(output.getvalue())

         if options.profile:
            statprof.reset()
            statprof.start()

         reactor.callLater(options.interval, stat)
Ejemplo n.º 8
0
 def process_request(self, request):
     try:
         statprof.reset(
             getattr(settings, 'LIVEPROFILER_STATPROF_FREQUENCY', 100))
     except:
         pass
     statprof.start()
Ejemplo n.º 9
0
 def inner(*args, **kwargs):
     statprof.reset(frequency=1000)
     statprof.start()
     try:
         return fn(*args, **kwargs)
     finally:
         statprof.stop()
         statprof.display()
Ejemplo n.º 10
0
 def inner(*args, **kwargs):
     statprof.reset(frequency=1000)
     statprof.start()
     try:
         return fn(*args, **kwargs)
     finally:
         statprof.stop()
         statprof.display()
Ejemplo n.º 11
0
 def __enter__(self):
     import statprof
     try:
         statprof.reset(self.frequency)
     except AssertionError:
         pass  # statprof is already running
     statprof.start()
     return self
Ejemplo n.º 12
0
 def inner(*args, **kwargs):
     if deco_kwargs.get('traceback'):
         traceback.print_stack()
     print('starting %s' % fn.__name__)
     start = time.time()
     stat_profile = deco_kwargs.get('stat_profile')
     if stat_profile:
         import statprof
         statprof.reset(frequency=10000)
         statprof.start()
     fn(*args, **kwargs)
     fn_time = time.time() - start
     print('finished %s in %s s' % (fn.__name__, fn_time))
     if stat_profile:
         statprof.stop()
         statprof.display()
     return fn_time
Ejemplo n.º 13
0
 def inner(*args, **kwargs):
     if deco_kwargs.get('traceback'):
         traceback.print_stack()
     print('starting %s' % fn.__name__)
     start = time.time()
     stat_profile = deco_kwargs.get('stat_profile')
     if stat_profile:
         import statprof
         statprof.reset(frequency=1000)
         statprof.start()
     try:
         return fn(*args, **kwargs)
     finally:
         fn_name = fn.__name__
         print('finished %s in %.3f s' % (fn_name, time.time() - start))
         if stat_profile:
             statprof.stop()
             statprof.display()
Ejemplo n.º 14
0
def statprofile(ui, func, fp):
    try:
        import statprof
    except ImportError:
        raise util.Abort(_('statprof not available - install using "easy_install statprof"'))

    freq = ui.configint("profiling", "freq", default=1000)
    if freq > 0:
        statprof.reset(freq)
    else:
        ui.warn(_("invalid sampling frequency '%s' - ignoring\n") % freq)

    statprof.start()
    try:
        return func()
    finally:
        statprof.stop()
        statprof.display(fp)
Ejemplo n.º 15
0
def statprofile(ui, func, fp):
    try:
        import statprof
    except ImportError:
        raise util.Abort(_(
            'statprof not available - install using "easy_install statprof"'))

    freq = ui.configint('profiling', 'freq', default=1000)
    if freq > 0:
        statprof.reset(freq)
    else:
        ui.warn(_("invalid sampling frequency '%s' - ignoring\n") % freq)

    statprof.start()
    try:
        return func()
    finally:
        statprof.stop()
        statprof.display(fp)
Ejemplo n.º 16
0
 def inner(*args, **kwargs):
     log_fn = logger.debug
     if deco_kwargs.get('traceback'):
         traceback.print_stack()
     if deco_kwargs.get('traceback'):
         traceback.print_stack()
     log_fn('starting %s', fn.__name__)
     start = time.time()
     stat_profile = deco_kwargs.get('stat_profile')
     if stat_profile:
         import statprof
         statprof.reset(frequency=10000)
         statprof.start()
     try:
         return fn(*args, **kwargs)
     finally:
         log_fn('finished %s in %s s', fn.__name__, time.time() - start)
         if stat_profile:
             statprof.stop()
             statprof.display()
Ejemplo n.º 17
0
def statprofile(ui, fp):
    try:
        import statprof
    except ImportError:
        raise error.Abort(_(
            'statprof not available - install using "easy_install statprof"'))

    freq = ui.configint('profiling', 'freq', default=1000)
    if freq > 0:
        # Cannot reset when profiler is already active. So silently no-op.
        if statprof.state.profile_level == 0:
            statprof.reset(freq)
    else:
        ui.warn(_("invalid sampling frequency '%s' - ignoring\n") % freq)

    statprof.start()
    try:
        yield
    finally:
        statprof.stop()
        statprof.display(fp)
Ejemplo n.º 18
0
      def stat():
         if options.profile:
            statprof.stop()

         output = StringIO.StringIO()
         output.write("-" * 80 + "\n")
         output.write("Worker Statistics (PID %s)\n\n%s"  % (workerPid, factory.stats.stats()))

         if options.profile:
            output.write("\n")
            #format = statprof.DisplayFormats.ByLine
            #format = statprof.DisplayFormats.ByMethod
            #statprof.display(output, format = format)
            statprof.display(output)

         output.write("-" * 80 + "\n\n")

         sys.stdout.write(output.getvalue())

         if options.profile:
            statprof.reset(PROFILER_FREQ)
            statprof.start()

         reactor.callLater(options.interval, stat)
Ejemplo n.º 19
0
 def process_request(self, request):
     statprof.reset(getattr(settings, 'LIVEPROFILER_STATPROF_FREQUENCY', 100))
     statprof.start()