Ejemplo n.º 1
0
    def initializeNetwork(self, sock):
        try:
            sock.settimeout(
                None
            )  # infinite, no timeouts from now on - jython does not have it
        except:
            pass
        self.writer = ProfWriter(sock)
        self.reader = ProfReader(sock, self)
        self.reader.start()

        time.sleep(0.1)  # give threads time to start
    def initializeNetwork(self, sock):
        try:
            sock.settimeout(None)  # infinite, no timeouts from now on - jython does not have it
        except:
            pass
        self.writer = ProfWriter(sock)
        self.reader = ProfReader(sock, self)
        self.reader.start()

        time.sleep(0.1)  # give threads time to start
class Profiler(object):
    def __init__(self):
        try:
            import yappi_profiler
            self.profiling_backend = yappi_profiler.YappiProfile()
            print('Starting yappi profiler\n')
        except ImportError:
            import cProfile
            self.profiling_backend = cProfile.Profile()
            print('Starting cProfile profiler\n')

    def connect(self, host, port):
        s = StartClient(host, port)

        self.initializeNetwork(s)

    def initializeNetwork(self, sock):
        try:
            sock.settimeout(None)  # infinite, no timeouts from now on - jython does not have it
        except:
            pass
        self.writer = ProfWriter(sock)
        self.reader = ProfReader(sock, self)
        self.reader.start()

        time.sleep(0.1)  # give threads time to start

    def process(self, message):
        if hasattr(message, 'save_snapshot'):
            self.save_snapshot(message.id, generate_snapshot_filepath(message.save_snapshot.filepath))
        else:
            raise AssertionError("Unknown request %s" % dir(message))

    def run(self, file):
        m = save_main_module(file, 'run_profiler')
        globals = m.__dict__
        try:
            globals['__builtins__'] = __builtins__
        except NameError:
            pass  # Not there on Jython...

        self.start_profiling()

        pydev_imports.execfile(file, globals, globals)  # execute the script

        self.stop_profiling()
        self.save_snapshot(0, generate_snapshot_filepath(base_snapshot_path))

    def start_profiling(self):
        self.profiling_backend.enable()

    def stop_profiling(self):
        self.profiling_backend.disable()

    def get_snapshot(self):
        return self.profiling_backend.getstats()

    def dump_snapshot(self, filename):
        dir = os.path.dirname(filename)
        if not os.path.exists(dir):
            os.makedirs(dir)

        self.profiling_backend.dump_stats(filename)
        return filename

    def save_snapshot(self, id, filename):
        self.stop_profiling()
        filename = self.dump_snapshot(filename)

        m = ProfilerResponse(id=id, snapshot_filepath=filename)

        print('Snapshot saved to %s' % filename)

        self.writer.addCommand(m)
        self.start_profiling()
Ejemplo n.º 4
0
class Profiler(object):
    def __init__(self):
        try:
            import yappi_profiler
            self.profiling_backend = yappi_profiler.YappiProfile()
            print('Starting yappi profiler\n')
        except ImportError:
            import cProfile
            self.profiling_backend = cProfile.Profile()
            print('Starting cProfile profiler\n')

    def connect(self, host, port):
        s = StartClient(host, port)

        self.initializeNetwork(s)

    def initializeNetwork(self, sock):
        try:
            sock.settimeout(
                None
            )  # infinite, no timeouts from now on - jython does not have it
        except:
            pass
        self.writer = ProfWriter(sock)
        self.reader = ProfReader(sock, self)
        self.reader.start()

        time.sleep(0.1)  # give threads time to start

    def process(self, message):
        if hasattr(message, 'save_snapshot'):
            self.save_snapshot(
                message.id,
                generate_snapshot_filepath(message.save_snapshot.filepath,
                                           remote_run), remote_run)
        else:
            raise AssertionError("Unknown request %s" % dir(message))

    def run(self, file):
        m = save_main_module(file, 'run_profiler')
        globals = m.__dict__
        try:
            globals['__builtins__'] = __builtins__
        except NameError:
            pass  # Not there on Jython...

        self.start_profiling()

        try:
            pydev_imports.execfile(file, globals,
                                   globals)  # execute the script
        finally:
            self.stop_profiling()
            self.save_snapshot(
                0, generate_snapshot_filepath(base_snapshot_path, remote_run),
                remote_run)

    def start_profiling(self):
        self.profiling_backend.enable()

    def stop_profiling(self):
        self.profiling_backend.disable()

    def get_snapshot(self):
        self.profiling_backend.create_stats()
        return self.profiling_backend.stats

    def dump_snapshot(self, filename):
        dir = os.path.dirname(filename)
        if not os.path.exists(dir):
            os.makedirs(dir)

        self.profiling_backend.dump_stats(filename)
        return filename

    def save_snapshot(self, id, filename, send_stat=False):
        self.stop_profiling()
        if filename is not None:
            filename = self.dump_snapshot(filename)
            print('Snapshot saved to %s' % filename)

        if not send_stat:
            response = ProfilerResponse(id=id, snapshot_filepath=filename)
        else:
            response = ProfilerResponse(id=id)
            statsToResponse(self.get_snapshot(), response)

        self.writer.addCommand(response)
        self.start_profiling()
Ejemplo n.º 5
0
class Profiler(object):
    def __init__(self):
        pass

    def connect(self, host, port):
        s = StartClient(host, port)

        self.initializeNetwork(s)

    def initializeNetwork(self, sock):
        try:
            sock.settimeout(None)  # infinite, no timeouts from now on - jython does not have it
        except:
            pass
        self.writer = ProfWriter(sock)
        self.reader = ProfReader(sock, self)
        self.writer.start()
        self.reader.start()

        time.sleep(0.1)  # give threads time to start

    def process(self, message):
        if message.HasField('ystats_string'):
            self.stats_string(message.id)
        elif message.HasField('ystats'):
            self.func_stats(message.id)
        else:
            raise AssertionError("malformed request")

    def run(self, file):
        m = save_main_module(file, 'run_profiler')
        globals = m.__dict__
        try:
            globals['__builtins__'] = __builtins__
        except NameError:
            pass  # Not there on Jython...

        self.start_profiling()

        pydev_imports.execfile(file, globals, globals)  # execute the script

        # self.stats_string()

        time.sleep(10)

    def start_profiling(self):
        yappi.start(profile_threads=False)

    def stats_string(self, id):
        output = StringIO.StringIO()
        yappi.get_func_stats().print_all(out=output)
        m = ProfilerResponse()
        m.id = id
        m.ystats_string = output.getvalue()
        self.writer.addCommand(m)

    def func_stats(self, id):
        yfunc_stats = yappi.get_func_stats()
        m = ProfilerResponse()
        m.id = id
        ystats = Stats()

        for fstat in yfunc_stats:
            func_stat = ystats.func_stats.add()
            copy_fields(func_stat, fstat)
        m.ystats.CopyFrom(ystats)
        self.writer.addCommand(m)