mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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