UDPPacketLose.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import os
  2. import time
  3. import random
  4. import threading
  5. import socket
  6. from TCAction import TCActionBase
  7. from NativeLog import NativeLog
  8. AP_PROP_KEY = ("ssid", "password", "apc")
  9. class SendThread(threading.Thread):
  10. def __init__(self, sock, send_len, target_addr, delay):
  11. threading.Thread.__init__(self)
  12. self.sock = sock
  13. self.send_len = send_len
  14. self.target_addr = target_addr
  15. self.delay = delay
  16. self.count = 0
  17. self.exit_event = threading.Event()
  18. pass
  19. def exit(self):
  20. self.exit_event.set()
  21. def run(self):
  22. data = "A" * self.send_len
  23. if self.sock is None:
  24. return
  25. while True:
  26. if self.exit_event.isSet() is True:
  27. break
  28. try:
  29. self.sock.sendto(data, self.target_addr)
  30. except StandardError:
  31. break
  32. self.count += 1
  33. time.sleep(self.delay * 0.001)
  34. def calculate(self):
  35. return self.count
  36. class RecvThread(threading.Thread):
  37. def __init__(self, sock):
  38. threading.Thread.__init__(self)
  39. self.sock = sock
  40. self.exit_event = threading.Event()
  41. self.calc_event = threading.Event()
  42. self.bytes_recv = 0
  43. def start_calc(self):
  44. self.calc_event.set()
  45. def stop_calc(self):
  46. self.calc_event.clear()
  47. self.exit_event.set()
  48. def run(self):
  49. if self.sock is None:
  50. return
  51. ret = True
  52. while ret:
  53. if self.exit_event.isSet() is True:
  54. break
  55. try:
  56. data, addr = self.sock.recvfrom(65535)
  57. except StandardError:
  58. break
  59. if self.calc_event.isSet() is True:
  60. self.bytes_recv += len(data)
  61. if len(data) == 0:
  62. start = time.time()
  63. while time.time() - start < 30:
  64. try:
  65. data, addr = self.sock.recvfrom(65535)
  66. except StandardError:
  67. break
  68. if len(data) == 0:
  69. break
  70. else:
  71. self.bytes_recv += len(data)
  72. else:
  73. ret = False
  74. def get_bytes_recv(self):
  75. return self.bytes_recv
  76. pass
  77. class TestCase(TCActionBase.CommonTCActionBase):
  78. def __init__(self, test_case, test_env, timeout=30, log_path=TCActionBase.LOG_PATH):
  79. TCActionBase.CommonTCActionBase.__init__(self, test_case, test_env, timeout, log_path)
  80. self.att_test_list = range(60)
  81. # load param from excel
  82. cmd_set = test_case["cmd set"]
  83. for i in range(1, len(cmd_set)):
  84. if cmd_set[i][0] != "dummy":
  85. cmd_string = "self." + cmd_set[i][0]
  86. exec cmd_string
  87. self.result_cntx = TCActionBase.ResultCheckContext(self, test_env, self.tc_name)
  88. pass
  89. def execute(self):
  90. TCActionBase.TCActionBase.execute(self)
  91. self.result_cntx.start()
  92. try:
  93. # configurable params
  94. send_len = self.send_len
  95. pc_send = self.pc_send
  96. target_send = self.target_send
  97. test_time = self.test_time
  98. delay = self.delay
  99. ap_ssid = self.get_parameter("ap_ssid")
  100. ap_password = self.get_parameter("ap_password")
  101. pc_ip = self.get_parameter("pc_ip")
  102. target_ip = self.get_parameter("target_ip")
  103. # configurable params
  104. except StandardError, e:
  105. NativeLog.add_trace_critical("Error configuration for UDP script, error is %s" % e)
  106. raise StandardError("Error configuration")
  107. udp_port = random.randint(40000, 50000)
  108. # reboot before executing
  109. checker_stings = ["R SSC1 C ready!!!"]
  110. test_action_string = ["SSC SSC1 reboot"]
  111. fail_string = "Fail, Fail to reboot"
  112. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  113. return
  114. # disable recv print on target
  115. checker_stings = ["R SSC1 C +RECVPRINT"]
  116. test_action_string = ["SSC SSC1 soc -R -o 0"]
  117. fail_string = "Fail, Fail to disable recv print"
  118. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  119. return
  120. # create socket on pc
  121. udp_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
  122. udp_sock.bind((pc_ip, udp_port))
  123. udp_sock.settimeout(1)
  124. # connect to AP
  125. checker_stings = ["R SSC1 C +JAP:CONNECTED"]
  126. test_action_string = ["SSC SSC1 sta -C -s %s -p %s" % (ap_ssid, ap_password)]
  127. fail_string = "Fail, Fail to JAP"
  128. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string,
  129. check_freq=1, check_time=30) is False:
  130. return
  131. # close all connection
  132. checker_stings = ["R SSC1 C +CLOSEALL"]
  133. test_action_string = ["SSC SSC1 soc -T"]
  134. fail_string = "Fail, Fail to create server"
  135. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  136. return
  137. # create UDP socket on target
  138. checker_stings = ["R SSC1 A <client_sock>:\+BIND:(\d+),OK"]
  139. test_action_string = ["SSC SSC1 soc -B -t UDP -p %s" % udp_port]
  140. fail_string = "Fail, Fail bind"
  141. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  142. return
  143. send_thread = SendThread(udp_sock if pc_send is True else None,
  144. send_len, (target_ip, udp_port), delay)
  145. send_thread.start()
  146. recv_thread = RecvThread(udp_sock if target_send is True else None)
  147. recv_thread.start()
  148. # start calculate
  149. recv_thread.start_calc()
  150. send_count = 0
  151. if target_send is True:
  152. # do send from target
  153. start = time.time()
  154. while time.time() - start < test_time * 60:
  155. checker_stings = ["P SSC1 RE \+SEND:0,OK"]
  156. test_action_string = ["SSC SSC1 soc -S -s <client_sock> -l %s -n 1000 -i %s -p %s -j %s" % (
  157. send_len, pc_ip, udp_port, delay)]
  158. fail_string = "Fail, Fail to send"
  159. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string, check_freq=0.1,
  160. check_time=3000) is False:
  161. return
  162. send_count += 1000
  163. else:
  164. time.sleep(test_time * 60)
  165. send_thread.exit()
  166. send_thread.join()
  167. # stop throughput calculate
  168. while True:
  169. if recv_thread.isAlive() is False:
  170. recv_thread.stop_calc()
  171. recv_thread.join()
  172. break
  173. recv_count = 0
  174. if pc_send is True:
  175. # get received data len from PC
  176. self.load_and_exe_one_step(["R SSC1 A <recv_len>:RECVLEN:(\d+)"],
  177. ["SSC SSC1 soc -Q -s <client_sock> -o 1"],
  178. "Fail, Fail to get recv data len")
  179. try:
  180. rx_data_len = int(self.get_parameter("recv_len"))
  181. except StandardError:
  182. rx_data_len = 0
  183. if (rx_data_len % send_len) > 0:
  184. recv_count = rx_data_len / send_len + 1
  185. else:
  186. recv_count = rx_data_len / send_len
  187. send_count = send_thread.calculate()
  188. if recv_thread.get_bytes_recv() > 0:
  189. if (recv_thread.get_bytes_recv() % send_len) > 0:
  190. recv_count = recv_thread.get_bytes_recv() / send_len + 1
  191. else:
  192. recv_count = recv_thread.get_bytes_recv() / send_len
  193. udp_sock.close()
  194. NativeLog.add_trace_critical("send_count is %s, recv_count is %s" % (send_count, recv_count))
  195. self.result_cntx.set_result("Succeed")
  196. NativeLog.add_trace_critical(
  197. "UDP Packet lose rate is %.2f%%" % (float(send_count - recv_count) / send_count * 100))
  198. # finally, execute done
  199. def result_check(self, port_name, data):
  200. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  201. self.result_cntx.append_data(port_name, data)
  202. def main():
  203. pass
  204. if __name__ == '__main__':
  205. main()