Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tcpecho_client.py 1.8KB

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