def __init__(self): self.pprop = IniConfigure() self.sreport = StressReporter() self.pstat = ApiStatistic() self.managers = {} self.ignoreImportExcept = True self.setRunnerByArgs(False, ["-r", "show"])
def __init__(self): self.runModeEnum = {'slook':-22, 'scenario':-21, 'param':-11, 'look':-10, 'show':-9, 'debug':0, 'run':9, 'rerun':10, 'stop':-9} self.testResInfo = ['Passed', 'Warned', 'Failed', 'NotRun'] self.tlog = TestLoggerFactory() self.tprop = IniConfigure("mtest.ini") self.tassert = TestAssert(self.tlog) self.tcInfos = TValueGroup({}) self.init()
def __init__(self): self.cserviceInfo = [] self.cserviceProxy = [] self.cprop = IniConfigure()
class ServerDriver: def __init__(self): self.cserviceInfo = [] self.cserviceProxy = [] self.cprop = IniConfigure() def addServiceProxy(self, cserviceCls, handleUrl): if handleUrl is not None and handleUrl.strip() != "": self.cserviceProxy.append((cserviceCls, handleUrl)) def addService(self, moduleCls, handleUrl, moduleInfo): self.cserviceInfo.append((moduleCls, handleUrl, moduleInfo)) def startService(self): self.__registerService(self.__getLocalExIp(), self.ports) from server.claunch import launchHttpServer launchHttpServer(self._serverArgs, self.ports) def __getLocalExIp(self): try: import socket return socket.gethostbyname_ex(socket.gethostname())[2][0] except:return "" def __registerService(self, exip, ports): from libs.syslog import slog slog.warn("%s:%s register to %s with %s" % (exip, ", ".join([str(p) for p in ports]), self.regServer, self.regName)) if self.regServer is not None: from server.cclient import curlCservice curlCservice(self.regServer, 'CServiceTool/RegistServer', hostport="%s:%s" % (exip, ports[0]), serverName=self.regName) def setDriverByArgs(self, args): from libs.objop import ArgsOperation cArgs, parseMsg, isSuccess = ArgsOperation.parseArgs(list(args), [], None, *self.__getDefine()) from libs.syslog import slog slog.info(parseMsg) if not isSuccess: import sys sys.exit(-1) import os if cArgs.config != None and os.path.exists(cArgs.config): self.cprop.load(cArgs.config) self.regServer = cArgs.regServer self.regName = cArgs.regName self.ports = [8089] if len(cArgs.ports) == 0 else cArgs.ports host, port = "", int(self.ports[0]) self._serverArgs = (host, port, cArgs.timeout, cArgs.isCpuPriority, cArgs.webroot, cArgs.mainpage, cArgs.uploadFolder, cArgs.servicePath, self.cserviceInfo, self.cserviceProxy, cArgs.stubFiles, cArgs.processes, cArgs.setProcessLog) del cArgs def __getDefine(self): return (("c", "config", "run properties file", "cservice.ini"), ("t", "stubFiles", "python stub files", [], 'list'), ("f", "webroot", "Html folder", "NotConfigure"), ("uploadFolder", "upload folder", ""), ("m", "mainpage", "home or main page, such as HomePage.html"), ("o", "timeout", "timeout", 5, "float"), ("p", "ports", "http ports, such as -p 80 -p 8080", [], "list"), ("isCpuPriority", "CPU priority: request handle by process (logs may lost) if true, otherwise request handle by threads", False, "bool"), ("processes", "processes launched: for Linux fork process, for windows create sub process", 1, "int"), ("setProcessLog", "when forking process, set different log name for each process if true", False, "bool"), ("servicePath", "URL path of service", "cservice"), ("s", "regServer", "register server address", None), ("n", "regName", "register server name", None) )
class StressScheduler: def __init__(self): self.pprop = IniConfigure() self.sreport = StressReporter() self.pstat = ApiStatistic() self.managers = {} self.ignoreImportExcept = True self.setRunnerByArgs(False, ["-r", "show"]) def setRunnerByArgs(self, isShowMsg, args): cArgs, parseMsg, isSuccess = ArgsOperation.parseArgs(list(args), [], toJsonObj, *self.__getDefine()) if isShowMsg: print(parseMsg) if not isSuccess: sys.exit(-1) self.pprop.load(cArgs.config) propConf = cArgs.prop for sec in propConf: if not self.pprop.sections.__contains__(sec): self.pprop.sections[sec] = propConf[sec] else: for p in propConf[sec]: self.pprop.sections[sec][p] = propConf[sec][p] self.mode = cArgs.mode self.inPattern = cArgs.__dict__['in'] self.apiIntervalTps = cArgs.apiIntervalTps self.perfArgs = (cArgs.startThreads, cArgs.maxThreads, cArgs.step, cArgs.expTps) if cArgs.isResetPerf else None self.sreport.setReporter(cArgs.maxTime, cArgs.isKeepRunning) if cArgs.url != "": self.__checkCurl(cArgs) from libs.refrect import DynamicLoader DynamicLoader.getClassFromFile("stressScenario", self.ignoreImportExcept, True, *cArgs.file) def __checkCurl(self, cArgs): url, body = cArgs.url, cArgs.body from server.cclient import curl stsHandler = lambda thid, degree: curl(url.replace("{thid}", str(thid)).replace("{degree}", str(degree)), body.replace("{thid}", str(thid)).replace("{degree}", str(degree)) if (body and body != "") else None) if self.mode == "debug": print(stsHandler(0, 0)) self.addScenario(stsHandler, cArgs.startThreads, cArgs.maxThreads, cArgs.step, cArgs.expTps) def __getDefine(self): return (("r", "mode", "run|show|debug", 'run'), ("t", "file", "files", [], 'list'), ("i", "in", "in pattern", ''), ("c", "config", "test run config file", "mtest.ini"), ("p", "prop", "section.name=value, configure mtest.ini by CMD arguments", [], "prop"), ("apiIntervalTps", "", 200, 'int'), ("maxTime", "", 30, 'int'), ("isKeepRunning", "", False, 'bool'), ("url", "curl URL for stress test, such as http://127.0.0.1/", ''), ("body", "post body", ""), ("isResetPerf", "reset perf args if true", False, 'bool'), ("startThreads", "startThreads of curl stress test", 2, 'int'), ("maxThreads", "maxThreads of curl stress test", 30, 'int'), ("step", "step of curl stress test", 2, 'int'), ("expTps", "expected TPS", 1024, 'int') ) def __isInscope__(self, name): import re return self.inPattern == "" or re.match(self.inPattern, name) is not None def getApiRunner(self, stsHandler): name = stsHandler.__name__ try: r = self.managers[name] except: r = StatisticRunner(stsHandler, self.sreport, self.apiIntervalTps) self.managers[name] = r return r def addScenario(self, stsHandler, startThreads, maxThreads, step, expTps, interval): if self.perfArgs is not None: startThreads , maxThreads , step, expTps = self.perfArgs self.managers[stsHandler.__name__] = (ThreadStatisticRunner(self.sreport, stsHandler, startThreads , maxThreads , step, expTps, interval)) def launch(self): try: self._planch() finally: self.pstat.finishApi() sys.exit(0) def _planch(self): isRunning = 0 for m in self.managers.values(): if self.__isInscope__(m.stsHandler.__name__): print(m) if self.mode == "debug": m.isDebug = 1 print("%s\t%s\n" % ("Passed" if m.runHandler(1, 1)[0] else "Failed", m.stsHandler.__name__)) elif self.mode != "show": m.increase(m.startThreads) isRunning = 1 continue m.stop() stopwait = 0 while isRunning and stopwait < 10: isRunning, ac = 0, 0 for m in self.managers.values(): ac += m.getAliveRunner() isRunning = isRunning or m.isRunning m.reportStatus() time.sleep(0.3) if os.path.exists("stop.perf"): print "Stop test for stop.perf" os.system("rm -rf stop.perf") m.stop() if not isRunning and ac > 0: stopwait += 0.3 isRunning = 1 if stopwait > 10: print "Force stop testing"
class TestCaseFactory: def __init__(self): self.runModeEnum = { 'slook': -22, 'scenario': -21, 'param': -11, 'look': -10, 'show': -9, 'debug': 0, 'run': 9, 'rerun': 10, 'stop': -9 } self.testResInfo = ['Passed', 'Warned', 'Failed', 'NotRun'] self.tlog = TestLoggerFactory() self.tprop = IniConfigure() self.tassert = TestAssert(self.tlog) self.tcInfos = {} self.startTime = time.time() self.init() def init(self, runMode="debug", testrunConfig="", logFilePath="", tcPattern=None, outTcPattern=None, searchKey=None, propConf={}): if not self.tprop.load(testrunConfig): if testrunConfig != "": slog.warn("Not found testrunConfig: %s" % testrunConfig) for sec in propConf: if not self.tprop.sections.__contains__(sec): self.tprop.sections[sec] = propConf[sec] else: for p in propConf[sec]: self.tprop.sections[sec][p] = propConf[sec][p] self.isModeling = False self.setRunCase(tcPattern, outTcPattern, searchKey, runMode) self.setRunLog(logFilePath) def setRunLog(self, logs): logManager.removeHandler(slog, 1) logManager.addFileHandler(slog, None, "mtest.log") # set syslog for test self.tlog.clearLogger() if len(logs) == 0: logs = ['console'] for l in logs: if l.endswith(".html"): from tmodel.model.logreport import HtmlTestReport self.tlog.registerLogger(HtmlTestReport, l, False) else: self.tlog.registerLogger(TestLogger, l, l.endswith(".xml")) def setRunCase(self, tcPattern=None, outTcPattern=None, searchKey=None, runMode=None, isclearresult=False): self.tcReg = PyRegExp( tcPattern) if tcPattern != None and tcPattern != "" else None self.tcRegOutScope = PyRegExp( outTcPattern ) if outTcPattern != None and outTcPattern != "" else None self.searchKey = None if searchKey is None or searchKey == "" else searchKey.replace( ",", " ").split() if runMode is not None: try: self.runMode = self.runModeEnum[runMode] except: self.runMode = self.runModeEnum['debug'] if isclearresult: self.tcInfos.clear() def addRunMode(self, modeName, mode): self.runModeEnum[modeName] = mode def __checkSignal(self): if os.path.exists("stop.signal"): self.runMode = self.runModeEnum['stop'] else: while os.path.exists("pause.signal"): time.sleep(30) slog.info(".") def isInMode(self, modeName, modeType=0, isCheckSignal=False): if isCheckSignal and self.runMode >= self.runModeEnum['debug']: self.__checkSignal() try: modeCmp = self.runMode - self.runModeEnum[modeName] if modeType > 0: return modeCmp >= 0 elif modeType == 0: return modeCmp == 0 else: return modeCmp <= 0 except: return False def isInScope(self, csName, searchKeys=None): if self.searchKey is not None: if searchKeys is None: return False v = searchKeys.values() for k in self.searchKey: if not v.__contains__(k): return False return (self.tcReg is None or self.tcReg.isMatch(csName)) and \ (self.tcRegOutScope is None or not self.tcRegOutScope.isMatch(csName)) def addResultType(self, resType, resInfo): while resType > 3: try: self.testResInfo[resType] = resInfo break except: self.testResInfo.append(None) def getResInfo(self, resType): return self.testResInfo[resType] def getTCInfo(self, tcName): if not self.tcInfos.keys().__contains__(tcName): tcInfo = TValueGroup({}) tcInfo.orders = None tcInfo.isTested = False self.tcInfos[tcName] = tcInfo return self.tcInfos[tcName] def getTCDespcription(self, sparam, despFormat): if despFormat == "" or despFormat == None: if sparam.paramName == None: desp = "" else: desp = "(%s%s:%s)" % ( ("[%s]" % sparam.orderNum) if sparam.orderNum > 0 else "", sparam.paramName, sparam[sparam.paramName]) else: desp = despFormat for skey in sparam.keys(): stype = sparam.getType(skey) if stype == None: stype = str(sparam[skey])[0:26] desp = desp.replace("{%s}" % skey, stype) return desp def getTCFullName(self, tcName, pIndex, desp): return "%s%s %s" % (tcName, pIndex if pIndex > 0 else "", desp) def getCaseInfo(self, tsInfo, pIndex, desp, searchKey): if tsInfo.__contains__(pIndex): return tsInfo[pIndex] caseInfo = {'d': desp, 'r': MTConst.notRun, 't': 0, 'k': searchKey} tsInfo[pIndex] = caseInfo return caseInfo def getRunInfo(self, tcName, tempObj): tcInfo = self.getTCInfo(tcName) runList = [] if not tcInfo.isTested or self.runMode == 10: filteredList = [] for caseName in tcInfo.keys(): try: caseFun = eval("tempObj.%s" % caseName) except: raise Exception("Deuplicate class %s, no method %s" % (tempObj.__class__, caseName)) if not hasattr( caseFun, '__name__') or 'ScenarioDecorator' != caseFun.__name__: filteredList.append(caseName) for fName in filteredList: tcInfo.__delitem__(fName) runList = tcInfo.keys() if tcInfo.orders != None: tOrders = type(tcInfo.orders) if tOrders == bool: runList = list(runList) runList.sort(reverse=not tcInfo.orders) if tOrders == list and len(tcInfo.orders) > 0 and len(runList) > 1: runList = list(runList) orderList = [] for caseName in tcInfo.orders: if runList.__contains__(caseName): orderList.append(caseName) runList.remove(caseName) runList = orderList + runList return (tcInfo, runList) def mergeResult(self, tcInfos, tstatuses): for tsName in tcInfos.keys(): atcInfo, tcInfo = tcInfos[tsName], self.tcInfos[tsName] for tcName in atcInfo.keys(): atcResMap, tcResMap = atcInfo[tcName], tcInfo[tcName] for tpIndex in atcResMap.keys(): if tstatuses.__contains__(atcResMap[tpIndex]['r']): tcResMap[tpIndex] = atcResMap[tpIndex] def report(self, repHandler=slog.info, sumTitle='Test Summary:'): repRes = self.reportResult() if self.isInMode('show') or self.isInMode('scenario'): repHandler('\r\nFind Test Case(%s of %s):', repRes[MTConst.passed], repRes['num']) self.reportResult(MTConst.passed, repHandler) repHandler('Summary: %s of %s', repRes[MTConst.passed], repRes['num']) else: repHandler('\r\n%s\r\n%s' % (sumTitle, '-' * 60)) sumInfo = '\r\nTotal\t%s, %sm:\r\n\t' % ( repRes['num'], int(repRes['time'] / 60 + 0.5)) hasRunRes = False for resType in range(4): try: resInfo = self.getResInfo(resType) resNum = ObjOperation.tryGetVal(repRes, resType, 0) if resType != MTConst.notRun: repHandler('%s\t%s:', resInfo, resNum) self.reportResult(resType, repHandler) sumInfo += (" + %s: %s" if hasRunRes else "%s: %s") % (resInfo, resNum) hasRunRes = True except Exception as ex: slog.warn(ex) continue repHandler(sumInfo) # repHandler('\r\nLog File:\r\n\t%s\n\n' % self.tlog.GetLoggerPath()) return repRes def reportResult(self, resType=None, repHandler=slog.info): runRep = { 'startTime': self.startTime, 'endTime': time.time(), 'num': 0, 'time': 0, 'cases': { MTConst.passed: {}, MTConst.warned: {}, MTConst.failed: {}, MTConst.notRun: {} }, MTConst.passed: 0, MTConst.warned: 0, MTConst.failed: 0, MTConst.notRun: 0 } for tsName in self.tcInfos.keys(): tcInfo = self.getTCInfo(tsName) for tcName in tcInfo.keys(): tcResMap = tcInfo[tcName] for tpIndex in tcResMap.keys(): tcResInfo = tcResMap[tpIndex] tcRes = tcResInfo['r'] tcTime = tcResInfo['t'] runRep[tcRes] = ObjOperation.tryGetVal(runRep, tcRes, 0) + 1 runRep['num'] += 1 runRep['time'] += tcTime tcFullName = self.getTCFullName(tcName, tpIndex, tcResInfo['d']) if resType is None: searchkey = tcResInfo['k'] author = ObjOperation.tryGetVal( searchkey, 'author', '') priority = ObjOperation.tryGetVal( searchkey, 'priority', '') runRep['cases'][tcRes][tcFullName] = "%ss%s%s" % ( tcTime, "" if author == "" else (", %s" % author), "" if priority == "" else (", %s" % priority)) elif resType == tcRes: repHandler("\t%s%s", tcFullName, ("\t%ss" % tcTime) if tcTime > 0 else "") return runRep