Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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