~ubuntu-branches/ubuntu/vivid/pyzmq/vivid

« back to all changes in this revision

Viewing changes to perf/remote_thr.py

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-09-08 13:50:41 UTC
  • Revision ID: james.westby@ubuntu.com-20100908135041-bogeb6ykukjrcd89
Tags: upstream-0.1.20100703+git18f5d06155
ImportĀ upstreamĀ versionĀ 0.1.20100703+git18f5d06155

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
#    Copyright (c) 2007-2010 iMatix Corporation
 
3
#
 
4
#    This file is part of 0MQ.
 
5
#
 
6
#    0MQ is free software; you can redistribute it and/or modify it under
 
7
#    the terms of the Lesser GNU General Public License as published by
 
8
#    the Free Software Foundation; either version 3 of the License, or
 
9
#    (at your option) any later version.
 
10
#
 
11
#    0MQ is distributed in the hope that it will be useful,
 
12
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#    Lesser GNU General Public License for more details.
 
15
#
 
16
#    You should have received a copy of the Lesser GNU General Public License
 
17
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
 
 
20
import sys
 
21
import zmq
 
22
import time
 
23
 
 
24
def main ():
 
25
    use_poll = '-p' in sys.argv
 
26
    use_copy = '-c' in sys.argv
 
27
    if use_copy:
 
28
        sys.argv.remove('-c')
 
29
    if use_poll:
 
30
        sys.argv.remove('-p')
 
31
 
 
32
    if len (sys.argv) != 4:
 
33
        print 'usage: remote_thr [-c use-copy] [-p use-poll] <connect-to> <message-size> <message-count>'
 
34
        sys.exit(1)
 
35
 
 
36
    try:
 
37
        connect_to = sys.argv[1]
 
38
        message_size = int(sys.argv[2])
 
39
        message_count = int(sys.argv[3])
 
40
    except (ValueError, OverflowError), e:
 
41
        print 'message-size and message-count must be integers'
 
42
        sys.exit(1)
 
43
 
 
44
    ctx = zmq.Context()
 
45
    s = ctx.socket(zmq.PUB)
 
46
 
 
47
    #  Add your socket options here.
 
48
    #  For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM.
 
49
 
 
50
    if use_poll:
 
51
        p = zmq.Poller()
 
52
        p.register(s)
 
53
 
 
54
    s.connect(connect_to)
 
55
 
 
56
    # Wait for this side to connect.
 
57
    time.sleep(2.0)
 
58
 
 
59
    msg = ' ' * message_size
 
60
 
 
61
    for i in range(0, message_count):
 
62
        if use_poll:
 
63
            res = p.poll()
 
64
            assert(res[0][1] & zmq.POLLOUT)
 
65
        s.send(msg, zmq.NOBLOCK if use_poll else 0, copy=use_copy)
 
66
 
 
67
    # Let the context finish messaging before ending.
 
68
    # You may need to increase this time for longer or many messages.
 
69
    time.sleep(2.0)
 
70
 
 
71
if __name__ == "__main__":
 
72
    main ()