Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python cProfile背叛了我吗?_Python_Profiling_Protocol Buffers_Pyzmq_Cprofile - Fatal编程技术网

Python cProfile背叛了我吗?

Python cProfile背叛了我吗?,python,profiling,protocol-buffers,pyzmq,cprofile,Python,Profiling,Protocol Buffers,Pyzmq,Cprofile,我想知道为什么基于pyzmq和protobuf的消息传递乒乓比预期的慢得多,所以我使用cProfile检查了您在本文末尾找到的脚本 protoc --python_out=. rpc.proto python -m cProfile -o rpc.pstats ./test_rpc.py 返回 3.604 sec for 10000 messages, 360.41us/m, 2774 m/s 及 给我(仅针对客户端流程): rpc.proto: package rpc; message

我想知道为什么基于
pyzmq
protobuf
的消息传递乒乓比预期的慢得多,所以我使用
cProfile
检查了您在本文末尾找到的脚本

protoc --python_out=. rpc.proto
python -m cProfile -o rpc.pstats ./test_rpc.py
返回

3.604 sec for 10000 messages, 360.41us/m, 2774 m/s

给我(仅针对客户端流程):

rpc.proto:

package rpc;

message rpc_request {
    enum RpcType {GET = 0; SET = 1; QUIT = 2; }
    required RpcType type = 1;
    required string name = 2;
    optional string value = 3; }

message rpc_reply {
    optional string value = 3; }

您要测量的是进程时间,而不是默认测量的系统时间。这是通过使用探查器的自定义计时器功能完成的,例如time.process\u time()

例如:

import cProfile
import time
import pstats
import io

pr = cProfile.Profile(time.process_time)

pr.enable()

time.sleep(1)
for i in range(0, 1000):
    print("hello world")
time.sleep(1)
pr.disable()

s = io.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())
print函数消耗的处理时间最多,统计数据将显示这一点。 如果不将time.process_time()作为计时器函数传递,统计数据将显示sleep函数使用了大部分时间

import sys
import time
import threading
import subprocess
import zmq

import rpc_pb2  # must be generated first

if '--server' in sys.argv:
    print('zmq_protobuf_rpc_server')
    context = zmq.Context()
    socket = context.socket(zmq.REP)
    socket.bind("tcp://*:5556")

    request = rpc_pb2.rpc_request()
    reply = rpc_pb2.rpc_reply()

    while True:

        request.ParseFromString(socket.recv())
        if request.type == rpc_pb2.rpc_request.SET:
            value = request.value
        elif request.type == rpc_pb2.rpc_request.GET:
            reply.value = "value"
        socket.send(reply.SerializeToString())
        if request.type == rpc_pb2.rpc_request.QUIT:
            break

else:
    def zmq_reply(req_msg, rep_msg, socket):
        socket.send(req_msg.SerializeToString())
        rep_msg.ParseFromString(socket.recv())

    def rpc_set(req_msg, rep_msg, socket, name, value):
        req_msg.type = rpc_pb2.rpc_request.SET
        req_msg.name = name
        req_msg.value = value
        zmq_reply(req_msg, rep_msg, socket)

    def rpc_get(req_msg, rep_msg, socket, name):
        req_msg.type = rpc_pb2.rpc_request.GET
        req_msg.name = name
        zmq_reply(req_msg, rep_msg, socket)
        return rep_msg.value

    print('zmq_protobuf_rpc')
    p = subprocess.Popen([sys.executable, '-u', __file__, '--server'])
    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    socket.connect("tcp://127.0.0.1:5556")  # IPC would be a bit faster

    request = rpc_pb2.rpc_request()
    reply = rpc_pb2.rpc_reply()

    # wait for the server to be responsive
    rpc_set(request, reply, socket, 'hello', 'hello')

    N = 10000

    t = time.time()
    for i in range(N):
        rpc_set(request, reply, socket, 'name', str(i))
    t = time.time() - t
    print("%.3f sec for %d messages, %.2fus/m, %d m/s" 
          % (t, N, t / N * 1000000, N/t))

    request.type = rpc_pb2.rpc_request.QUIT
    zmq_reply(request, reply, socket)

    p.wait()
package rpc;

message rpc_request {
    enum RpcType {GET = 0; SET = 1; QUIT = 2; }
    required RpcType type = 1;
    required string name = 2;
    optional string value = 3; }

message rpc_reply {
    optional string value = 3; }
import cProfile
import time
import pstats
import io

pr = cProfile.Profile(time.process_time)

pr.enable()

time.sleep(1)
for i in range(0, 1000):
    print("hello world")
time.sleep(1)
pr.disable()

s = io.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())