TCPRandomSend.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. from NativeLog import ThroughputResult
  9. class TestCase(TCActionBase.CommonTCActionBase):
  10. def __init__(self, test_case, test_env, timeout=30, log_path=TCActionBase.LOG_PATH):
  11. TCActionBase.CommonTCActionBase.__init__(self, test_case, test_env, timeout=timeout, log_path=log_path)
  12. self.send_len_config = range(1460)
  13. self.delay_config = [0, 0.01, 0.1, 0.5, 1]
  14. # load param from excel
  15. cmd_set = test_case["cmd set"]
  16. for i in range(1, len(cmd_set)):
  17. if cmd_set[i][0] != "dummy":
  18. cmd_string = "self." + cmd_set[i][0]
  19. exec cmd_string
  20. self.result_cntx = TCActionBase.ResultCheckContext(self, test_env, self.tc_name)
  21. pass
  22. def execute(self):
  23. TCActionBase.TCActionBase.execute(self)
  24. self.result_cntx.start()
  25. try:
  26. # configurable params
  27. send_len_config = self.send_len_config
  28. delay_config = self.delay_config
  29. send_count = self.send_count
  30. test_time = self.test_time * 60
  31. # configurable params
  32. except StandardError, e:
  33. NativeLog.add_trace_critical("Error configuration for TCPThroughput script, error is %s" % e)
  34. raise StandardError("Error configuration")
  35. # disable recv print during random send test
  36. checker_stings = ["R SSC1 C +RECVPRINT"]
  37. test_action_string = ["SSC SSC1 soc -R -o 0"]
  38. fail_string = "Fail, Fail to disable recv print"
  39. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  40. return
  41. pc_ip = self.get_parameter("pc_ip")
  42. tcp_port = random.randint(50000, 60000)
  43. # step 0 create tcp connection
  44. server_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
  45. server_sock.bind((pc_ip, tcp_port))
  46. server_sock.settimeout(1)
  47. server_sock.listen(5)
  48. checker_stings = ["R SSC1 A <client_sock>:\+BIND:(\d+),OK"]
  49. test_action_string = ["SSC SSC1 soc -B -t TCP"]
  50. fail_string = "Fail, Fail bind"
  51. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  52. return
  53. checker_stings = ["P SSC1 RE \+CONNECT:\d+,OK"]
  54. test_action_string = ["SSC SSC1 soc -C -s <client_sock> -i %s -p %s" % (pc_ip, tcp_port)]
  55. fail_string = "Fail, Fail to connect"
  56. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  57. return
  58. sock, addr = server_sock.accept()
  59. sock.settimeout(10)
  60. # set no delay so that tcp segment will be send as soon as send called
  61. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
  62. # step 1 start send
  63. start_time = time.time()
  64. while time.time() - start_time < test_time:
  65. for delay in delay_config:
  66. for i in xrange(send_count):
  67. send_len = random.choice(send_len_config)
  68. data = "A" * (send_len+1)
  69. try:
  70. sock.send(data)
  71. except socket.error, e:
  72. NativeLog.add_exception_log(e)
  73. NativeLog.add_trace_critical("Fail to send packets")
  74. return
  75. pass
  76. time.sleep(delay)
  77. NativeLog.add_prompt_trace("time escape: %s" % (time.time() - start_time))
  78. if (time.time() - start_time) > test_time:
  79. self.result_cntx.set_result("Succeed")
  80. else:
  81. self.result_cntx.set_result("Failed")
  82. # finally, execute done
  83. def result_check(self, port_name, data):
  84. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  85. self.result_cntx.append_data(port_name, data)
  86. def main():
  87. pass
  88. if __name__ == '__main__':
  89. main()