123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- # coding=utf-8
- import json
- import random
- import string
- import time
- from concurrent.futures import ThreadPoolExecutor
- from base import init_env
- init_env(True)
- from apps.web.core.networking import MessageSender
- from apps.web.device.models import Device
- def make_sid():
- return '{}{}'.format(str(time.time() * 1000), random.choice(string.ascii_lowercase))
- dev = Device.get_dev("866327040126559")
- START_PAYLOAD = {
- "IMEI": "866327040126559",
- "funCode": "14",
- "data": "02001000010001",
- }
- STOP_PAYLOAD = {
- "IMEI": "866327040126559",
- "funCode": "0D",
- "data": "0200",
- }
- STATUS_PAYLOAD = {
- "IMEI": "866327040126559",
- "funCode": "0F",
- "data": "00"
- }
- class TimeOutException(Exception):
- pass
- class SerialException(Exception):
- pass
- class SystemException(Exception):
- pass
- def send(payload):
- result = MessageSender.send(
- device=dev,
- payload=payload,
- cmd=210,
- timeout=15
- )
- print "\r{}".format(payload.get("funCode"))
- if result["rst"] != 0:
- if result['rst'] == -1:
- raise TimeOutException()
- elif result['rst'] == 1:
- raise SerialException()
- else:
- raise SystemException()
- return result
- def syncTest():
- count = 1000
- success = 0
- serialError = 0
- timeoutError = 0
- sysError = 0
- while count:
- count = count-1
- try:
- send([START_PAYLOAD, STOP_PAYLOAD, STATUS_PAYLOAD][count % 3])
- except TimeOutException as e:
- timeoutError += 1
- except SerialException as e:
- serialError += 1
- except SystemException as e:
- sysError += 1
- else:
- success += 1
- time.sleep(random.randint(1, 5))
- with open("counter.txt", "w") as f:
- data = {
- "all": 1000-count,
- "success": success,
- "serialError": serialError,
- "timeoutError": timeoutError,
- "sysError": sysError
- }
- f.write(
- json.dumps(data)
- )
- count = 1000
- success = list()
- serialError = list()
- timeoutError = list()
- sysError = list()
- def func():
- # 数据样本较少 不考虑锁的问题
- try:
- send(STATUS_PAYLOAD)
- except TimeOutException as e:
- timeoutError.append(1)
- except SerialException as e:
- serialError.append(1)
- except SystemException as e:
- sysError.append(1)
- else:
- success.append(1)
- def asyncTest():
- pool = ThreadPoolExecutor(max_workers=5)
- for i in range(count):
- pool.submit(func)
- pool.shutdown(wait=True)
- with open("counter_async.txt", "w") as f:
- data = {
- "all": count,
- "success": len(success),
- "serialError": len(serialError),
- "timeoutError": len(timeoutError),
- "sysError": len(sysError)
- }
- f.write(
- json.dumps(data)
- )
- asyncTest()
|