yiyuan.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # coding=utf-8
  2. import json
  3. import random
  4. import string
  5. import time
  6. from concurrent.futures import ThreadPoolExecutor
  7. from base import init_env
  8. init_env(True)
  9. from apps.web.core.networking import MessageSender
  10. from apps.web.device.models import Device
  11. def make_sid():
  12. return '{}{}'.format(str(time.time() * 1000), random.choice(string.ascii_lowercase))
  13. dev = Device.get_dev("866327040126559")
  14. START_PAYLOAD = {
  15. "IMEI": "866327040126559",
  16. "funCode": "14",
  17. "data": "02001000010001",
  18. }
  19. STOP_PAYLOAD = {
  20. "IMEI": "866327040126559",
  21. "funCode": "0D",
  22. "data": "0200",
  23. }
  24. STATUS_PAYLOAD = {
  25. "IMEI": "866327040126559",
  26. "funCode": "0F",
  27. "data": "00"
  28. }
  29. class TimeOutException(Exception):
  30. pass
  31. class SerialException(Exception):
  32. pass
  33. class SystemException(Exception):
  34. pass
  35. def send(payload):
  36. result = MessageSender.send(
  37. device=dev,
  38. payload=payload,
  39. cmd=210,
  40. timeout=15
  41. )
  42. print "\r{}".format(payload.get("funCode"))
  43. if result["rst"] != 0:
  44. if result['rst'] == -1:
  45. raise TimeOutException()
  46. elif result['rst'] == 1:
  47. raise SerialException()
  48. else:
  49. raise SystemException()
  50. return result
  51. def syncTest():
  52. count = 1000
  53. success = 0
  54. serialError = 0
  55. timeoutError = 0
  56. sysError = 0
  57. while count:
  58. count = count-1
  59. try:
  60. send([START_PAYLOAD, STOP_PAYLOAD, STATUS_PAYLOAD][count % 3])
  61. except TimeOutException as e:
  62. timeoutError += 1
  63. except SerialException as e:
  64. serialError += 1
  65. except SystemException as e:
  66. sysError += 1
  67. else:
  68. success += 1
  69. time.sleep(random.randint(1, 5))
  70. with open("counter.txt", "w") as f:
  71. data = {
  72. "all": 1000-count,
  73. "success": success,
  74. "serialError": serialError,
  75. "timeoutError": timeoutError,
  76. "sysError": sysError
  77. }
  78. f.write(
  79. json.dumps(data)
  80. )
  81. count = 1000
  82. success = list()
  83. serialError = list()
  84. timeoutError = list()
  85. sysError = list()
  86. def func():
  87. # 数据样本较少 不考虑锁的问题
  88. try:
  89. send(STATUS_PAYLOAD)
  90. except TimeOutException as e:
  91. timeoutError.append(1)
  92. except SerialException as e:
  93. serialError.append(1)
  94. except SystemException as e:
  95. sysError.append(1)
  96. else:
  97. success.append(1)
  98. def asyncTest():
  99. pool = ThreadPoolExecutor(max_workers=5)
  100. for i in range(count):
  101. pool.submit(func)
  102. pool.shutdown(wait=True)
  103. with open("counter_async.txt", "w") as f:
  104. data = {
  105. "all": count,
  106. "success": len(success),
  107. "serialError": len(serialError),
  108. "timeoutError": len(timeoutError),
  109. "sysError": len(sysError)
  110. }
  111. f.write(
  112. json.dumps(data)
  113. )
  114. asyncTest()