Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tcpecho_client.py Source File

tcpecho_client.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2011-2013 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 import socket
00018 import string, random
00019 from time import time
00020 
00021 from mbed_settings import SERVER_ADDRESS
00022 
00023 ECHO_PORT = 7
00024 
00025 LEN_PACKET = 127
00026 N_PACKETS = 5000
00027 TOT_BITS = float(LEN_PACKET * N_PACKETS * 8) * 2
00028 MEGA = float(1024 * 1024)
00029 UPDATE_STEP = (N_PACKETS/10)
00030 
00031 class TCP_EchoClient:
00032     def __init__(self, host):
00033         self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
00034         self.s.connect((host, ECHO_PORT))
00035         self.packet = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(LEN_PACKET))
00036 
00037     def __packet(self):
00038         # Comment out the checks when measuring the throughput
00039         # self.packet = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(LEN_PACKET))
00040         self.s.send(self.packet)
00041         data = self.s.recv(LEN_PACKET)
00042         # assert self.packet == data, "packet error:\n%s\n%s\n" % (self.packet, data)
00043 
00044     def test(self):
00045         start = time()
00046         for i in range(N_PACKETS):
00047             if (i % UPDATE_STEP) == 0: print '%.2f%%' % ((float(i)/float(N_PACKETS)) * 100.)
00048             self.__packet()
00049         t = time() - start
00050         print 'Throughput: (%.2f)Mbits/s' % ((TOT_BITS / t)/MEGA)
00051 
00052     def __del__(self):
00053         self.s.close()
00054 
00055 while True:
00056     e = TCP_EchoClient(SERVER_ADDRESS)
00057     e.test()