#coding: utf-8
import time

from lib.testCase.FlvPressureTest import FlvPressureTest
import logging
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

###########################################
#       推流h264 和 aac 文件压力测试
###########################################
from lib.testCase.H264PresureTest import H264PresureTest


def h264FilePushTest():
    test = H264PresureTest()
    test.setHost("10.100.12.3")
    test.setPort(1078)
    test.setMobileStart(10000000000)
    test.setChannel(1)
    test.setTerNum(10)
    test.setSendDur(0.001)
    test.setIsOpenPullStream(0)                           # 设置是否开启拉流
    test.setIsShowFrame(0)
    test.setVideoPath("h264/bbb3.h264")
    test.setAudioPath("aac/bbb3.aac")
    test.start()


def getPressTestObj():
    test = FlvPressureTest()
    test.setHost("10.100.11.125")
    test.setPort(1078)
    test.setMobileStart(10000000000)                  # 开始的设备号 （累加）
    test.setChannel(1)                                # 设置频道号
    test.setTerNum(10)                                 # 要启动的推拉流线程
    test.setIsOpenPullStream(1)                       # 设置是否开启拉流
    test.setIsShowFrame(0)                            # 拉流是否显示预览画面     （压力测试都应该填：0）
    test.setSendDur(0.007)                            # 设置socket 发送数据间隔
    test.setFlvPath("flv/aaa3.flv")                   # 设置视频路劲
    test.start()
    return test

###########################################
#       推流flv文件压力测试
###########################################
def flvFilePushTest():
    isLoop = 1                                         # 是否循环压力测试  0： 不循环  1：循环
    loopTime = 1 * 60 * 60                             # 循环压力测试的时长
    test = getPressTestObj()
    logger.info("-----------------------------启动新一轮压力测试-----------------------------")
    threadInfo = test.getThreadInfo()
    threadInfoPull = test.getThreadInfoPull()
    logger.info("剩余推流线程：" + str(len(threadInfo["threadObj"])))
    logger.info("剩余拉流线程：" + str(len(threadInfoPull["threadObj"])))
    if isLoop == 0:
        while len(threadInfo["threadObj"]) != 0 and len(threadInfoPull["threadObj"]) != 0:
            time.sleep(5)
            logger.info("剩余推流线程：" + str(len(threadInfo["threadObj"])))
            logger.info("剩余拉流线程：" + str(len(threadInfoPull["threadObj"])))
            threadInfo = test.getThreadInfo()
            threadInfoPull = test.getThreadInfoPull()
    else:
        if len(threadInfo["threadObj"]) == 0 and len(threadInfoPull["threadObj"]) == 0:
            test = getPressTestObj()
            threadInfo = test.getThreadInfo()
            threadInfoPull = test.getThreadInfoPull()
        while len(threadInfo["threadObj"]) != 0 and len(threadInfoPull["threadObj"]) != 0:
            time.sleep(5)
            logger.info("剩余推流线程：" + str(len(threadInfo["threadObj"])))
            logger.info("剩余拉流线程：" + str(len(threadInfoPull["threadObj"])))
            threadInfo = test.getThreadInfo()
            threadInfoPull = test.getThreadInfoPull()
        time.sleep(30)
        flvFilePushTest()

    logger.info("压力测试结束!!!!!!")


if __name__ == "__main__":
    flvFilePushTest()
    # h264FilePushTest()
