~ubuntu-branches/ubuntu/wily/pyzmq/wily

« back to all changes in this revision

Viewing changes to perf/remote_lat.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-02-24 19:23:15 UTC
  • mfrom: (1.2.1) (9 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: package-import@ubuntu.com-20130224192315-qhmwp3m3ymk8r60d
Tags: 2.2.0.1-1
* New upstream release
* relicense debian packaging to LGPL-3
* update watch file to use github directly
  thanks to Bart Martens for the file
* add autopkgtests
* drop obsolete DM-Upload-Allowed
* bump standard to 3.9.4, no changes required

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import zmq
23
23
 
24
24
 
25
 
def main ():
26
 
    use_poll = '-p' in sys.argv
27
 
    use_copy = '-c' in sys.argv
 
25
def main (argv):
 
26
    use_poll = '-p' in argv
 
27
    use_copy = '-c' in argv
28
28
    if use_copy:
29
 
        sys.argv.remove('-c')
 
29
        argv.remove('-c')
30
30
    if use_poll:
31
 
        sys.argv.remove('-p')
 
31
        argv.remove('-p')
32
32
 
33
 
    if len(sys.argv) != 4:
34
 
        print 'usage: remote_lat [-c use-copy] [-p use-poll] <connect-to> <message-size> <roundtrip-count>'
 
33
    if len(argv) != 4:
 
34
        print ('usage: remote_lat [-c use-copy] [-p use-poll] <connect-to> <message-size> <roundtrip-count>')
35
35
        sys.exit(1)
36
36
 
37
37
    try:
38
 
        connect_to = sys.argv[1]
39
 
        message_size = int(sys.argv[2])
40
 
        roundtrip_count = int(sys.argv[3])
41
 
    except (ValueError, OverflowError), e:
42
 
        print 'message-size and message-count must be integers'
 
38
        connect_to = argv[1]
 
39
        message_size = int(argv[2])
 
40
        roundtrip_count = int(argv[3])
 
41
    except (ValueError, OverflowError):
 
42
        print ('message-size and message-count must be integers')
43
43
        sys.exit(1)
44
44
 
45
45
    ctx = zmq.Context()
46
46
    s = ctx.socket(zmq.REQ)
47
 
    print connect_to
 
47
    s.setsockopt(zmq.LINGER, -1)
 
48
    print (connect_to)
48
49
    s.connect(connect_to)
49
 
 
50
50
    if use_poll:
51
51
        p = zmq.Poller()
52
52
        p.register(s)
53
53
 
54
 
    msg = ' ' * message_size
 
54
    # remove the b for Python2.5:
 
55
    msg = b' ' * message_size
55
56
 
56
 
    clock = zmq.Stopwatch()
 
57
    watch = zmq.Stopwatch()
57
58
    start = 0
58
 
    clock.start()
59
 
    # start = time.clock()
 
59
 
 
60
    block = zmq.NOBLOCK if use_poll else 0
 
61
    
 
62
    watch.start()
60
63
 
61
64
    for i in range (0, roundtrip_count):
62
65
        if use_poll:
63
66
            res = p.poll()
64
67
            assert(res[0][1] & zmq.POLLOUT)
65
 
        s.send(msg, zmq.NOBLOCK if use_poll else 0, copy=use_copy)
 
68
        s.send(msg, block, copy=use_copy)
66
69
 
67
70
        if use_poll:
68
71
            res = p.poll()
69
72
            assert(res[0][1] & zmq.POLLIN)
70
 
        msg = s.recv(zmq.NOBLOCK if use_poll else 0, copy=use_copy)
 
73
        msg = s.recv(block, copy=use_copy)
 
74
        
71
75
        assert len (msg) == message_size
72
76
 
73
 
    end = clock.stop()
74
 
    # end = time.clock()
75
 
 
76
 
    time.sleep(1)
77
 
 
78
 
    elapsed = (end - start)
79
 
    # elapsed = (end - start) * 1000000 # use with time.clock
 
77
    elapsed = watch.stop()
 
78
 
 
79
    # remove the b for Python2.5:
 
80
    t = s.send(b'done', copy=False, track=True)
 
81
    t.wait()
 
82
 
80
83
    latency = elapsed / (roundtrip_count * 2)
81
84
 
82
 
    print "message size: %.0f [B]" % (message_size, )
83
 
    print "roundtrip count: %.0f" % (roundtrip_count, )
84
 
    print "mean latency: %.3f [us]" % (latency, )
 
85
    print ("message size: %i [B]" % (message_size, ))
 
86
    print ("roundtrip count: %i" % (roundtrip_count, ))
 
87
    print ("mean latency: %.3f [us]" % (latency, ))
85
88
 
86
 
    # Let the context finish messaging before ending.
87
 
    # You may need to increase this time for longer or many messages.
88
 
    time.sleep(2.0)
 
89
    s.close()
 
90
    ctx.term()
 
91
    return latency
89
92
 
90
93
if __name__ == "__main__":
91
 
    main ()
 
94
    main (sys.argv)
92
95
 
93
96