Clone of official tools
host_tests/udpecho_client.py@13:ab47a20b66f0, 2016-07-14 (annotated)
- Committer:
- screamer
- Date:
- Thu Jul 14 20:21:19 2016 +0100
- Revision:
- 13:ab47a20b66f0
- Parent:
- 0:66f3b5499f7f
Apply latest tools
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:66f3b5499f7f | 1 | """ |
screamer | 0:66f3b5499f7f | 2 | mbed SDK |
screamer | 0:66f3b5499f7f | 3 | Copyright (c) 2011-2013 ARM Limited |
screamer | 0:66f3b5499f7f | 4 | |
screamer | 0:66f3b5499f7f | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:66f3b5499f7f | 6 | you may not use this file except in compliance with the License. |
screamer | 0:66f3b5499f7f | 7 | You may obtain a copy of the License at |
screamer | 0:66f3b5499f7f | 8 | |
screamer | 0:66f3b5499f7f | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:66f3b5499f7f | 10 | |
screamer | 0:66f3b5499f7f | 11 | Unless required by applicable law or agreed to in writing, software |
screamer | 0:66f3b5499f7f | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:66f3b5499f7f | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:66f3b5499f7f | 14 | See the License for the specific language governing permissions and |
screamer | 0:66f3b5499f7f | 15 | limitations under the License. |
screamer | 0:66f3b5499f7f | 16 | """ |
screamer | 0:66f3b5499f7f | 17 | from socket import socket, AF_INET, SOCK_DGRAM |
screamer | 0:66f3b5499f7f | 18 | import string, random |
screamer | 0:66f3b5499f7f | 19 | from time import time |
screamer | 0:66f3b5499f7f | 20 | |
screamer | 13:ab47a20b66f0 | 21 | from mbed_settings import CLIENT_ADDRESS |
screamer | 0:66f3b5499f7f | 22 | |
screamer | 0:66f3b5499f7f | 23 | ECHO_PORT = 7 |
screamer | 0:66f3b5499f7f | 24 | |
screamer | 0:66f3b5499f7f | 25 | LEN_PACKET = 127 |
screamer | 0:66f3b5499f7f | 26 | N_PACKETS = 5000 |
screamer | 0:66f3b5499f7f | 27 | TOT_BITS = float(LEN_PACKET * N_PACKETS * 8) * 2 |
screamer | 0:66f3b5499f7f | 28 | MEGA = float(1024 * 1024) |
screamer | 0:66f3b5499f7f | 29 | UPDATE_STEP = (N_PACKETS/10) |
screamer | 0:66f3b5499f7f | 30 | |
screamer | 0:66f3b5499f7f | 31 | class UDP_EchoClient: |
screamer | 0:66f3b5499f7f | 32 | s = socket(AF_INET, SOCK_DGRAM) |
screamer | 0:66f3b5499f7f | 33 | |
screamer | 0:66f3b5499f7f | 34 | def __init__(self, host): |
screamer | 0:66f3b5499f7f | 35 | self.host = host |
screamer | 0:66f3b5499f7f | 36 | self.packet = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(LEN_PACKET)) |
screamer | 0:66f3b5499f7f | 37 | |
screamer | 0:66f3b5499f7f | 38 | def __packet(self): |
screamer | 0:66f3b5499f7f | 39 | # Comment out the checks when measuring the throughput |
screamer | 0:66f3b5499f7f | 40 | # packet = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(LEN_PACKET)) |
screamer | 0:66f3b5499f7f | 41 | UDP_EchoClient.s.sendto(packet, (self.host, ECHO_PORT)) |
screamer | 0:66f3b5499f7f | 42 | data = UDP_EchoClient.s.recv(LEN_PACKET) |
screamer | 0:66f3b5499f7f | 43 | # assert packet == data, "packet error:\n%s\n%s\n" % (packet, data) |
screamer | 0:66f3b5499f7f | 44 | |
screamer | 0:66f3b5499f7f | 45 | def test(self): |
screamer | 0:66f3b5499f7f | 46 | start = time() |
screamer | 0:66f3b5499f7f | 47 | for i in range(N_PACKETS): |
screamer | 0:66f3b5499f7f | 48 | if (i % UPDATE_STEP) == 0: print '%.2f%%' % ((float(i)/float(N_PACKETS)) * 100.) |
screamer | 0:66f3b5499f7f | 49 | self.__packet() |
screamer | 0:66f3b5499f7f | 50 | t = time() - start |
screamer | 0:66f3b5499f7f | 51 | print 'Throughput: (%.2f)Mbits/s' % ((TOT_BITS / t)/MEGA) |
screamer | 0:66f3b5499f7f | 52 | |
screamer | 0:66f3b5499f7f | 53 | while True: |
screamer | 0:66f3b5499f7f | 54 | e = UDP_EchoClient(CLIENT_ADDRESS) |
screamer | 0:66f3b5499f7f | 55 | e.test() |