Best Python code snippet using yandex-tank
case.py
Source:case.py
1#-------------------------------------------------------------------------------2# Name: case3# Purpose:4#5# Author: yuboli6#7# Created: 04/05/20178# Copyright: (c) yuboli 20179# Licence: <your licence>10#-------------------------------------------------------------------------------11import uuid, time12from run import Run13from logger import LOGGER14from run import Run15import traceback16import gc1718class Case(object):19 def __init__(self, scenario, **kwargs):20 self.case_id = str(uuid.uuid1())21 time.sleep(0.01)22 self.__parent = scenario23 self.executable = ''24 self.name = ''25 self.parameters = ''26 self.cmd = ''27 self.dependencies = []28 self.pass_bypass = []29 self.critical = False30 self.failrerun = 031 self.repetition = 132 self.execution = 133 self.failrepetition = 034 self.sleep_time = 035 self.reboot = False36 self.always_pass = False37 self.mustrun = False38 self.timeout = 60039 self.__runs = []40 self.base_share_path = r"\\sample.com\ec\proj\iag\peg\icg\AUTO_TEST\USTAF\LOGs"41 self.platform = ''42 self.test_type = ''43 self.gating = True44 self.bEnv = False45 self.start = '0.0'46 self.end = '0.0'47 self.force_repeat = False4849 '''50 Post execution parmas51 '''52 #self.event = []53 self.pretty_event = []54 self.hsd_id = []55 self.hsd_url = []56 self.pretty_result = 'NA'57 self.pass_rate = 0.058 self.error_log = ''59 self.std_log = ''6061 self.total_count = 062 self.pass_count = 06364 def addRun(self, r):65 self.__runs.append(r)6667 def runs(self):68 for r in self.__runs:69 yield r7071 @property72 def std_log_otf(self):7374 std_log = ''75 for run in self.runs():76 std_log += run.std_out_err + '\r\n'7778 return std_log7980 @property81 def error_log_otf(self):82 error_log = ''83 for run in self.runs():84 error_log += '||'.join(['|'.join(_) for _ in run.error_log])8586 return error_log8788 @ property89 def isPass(self):90 '''''''''''''''''91 ' Vote strategy '92 '''''''''''''''''93 __pass_count = 094 __not_pass_count = 095 for run in self.__runs:96 if run.result >= run.Pass:97 __pass_count += 198 else:99 __not_pass_count += 1100101 if __pass_count > __not_pass_count:102 return True103 else:104 return False105106 def result(self):107 __pass_count = 0108 __fail_count = 0109 __not_run_count = 0110 if len(self.__runs) == 0:111 self.pretty_event = []112 self.hsd_id = []113 self.hsd_url = []114 self.pass_rate = 0.0115 return "not-run"116 else:117 self.pretty_event = []118 self.pass_rate = 0.0119 for run in self.__runs:120 if run.result >= run.Pass:121 __pass_count += 1122 elif run.result == run.NotRun:123 __not_run_count += 1124 else:125 __fail_count += 1126127 for e in run.pretty_event:128 if e not in self.pretty_event:129 self.pretty_event.append(e)130131 if run.hsd_id not in self.hsd_id:132 try:133 self.hsd_id.append(str(run.hsd_id))134 except:135 LOGGER.error(traceback.format_exc())136 if run.hsd_url not in self.hsd_url:137 try:138 self.hsd_url.append(str(run.hsd_url))139 except:140 LOGGER.error(traceback.format_exc())141142 self.pass_rate = float(__pass_count)/(__pass_count + __fail_count + __not_run_count)143144 self.total_count = __pass_count + __not_run_count + __fail_count145 self.pass_count = __pass_count146147 if __fail_count > 0 and __pass_count <= __fail_count:148 return "fail"149 elif __pass_count > __fail_count and __fail_count > 0:150 return "repeat-pass"151 elif __pass_count > __fail_count and __fail_count == 0:152 return "pass"153 elif __fail_count == 0 and __not_run_count > 0:154 return "running"155156157 def checkDependencyPass(self):158 for case_name in self.dependencies:159 case = self.__parent.case(name=case_name)160 if not case.isPass:161 LOGGER.warning('dependency case %s failed!' % case_name)162 return False163 return True164165 def checkPassByPass(self):166 for case_name in self.pass_bypass:167 case = self.__parent.case(name=case_name)168 if case.isPass:169 LOGGER.warning('passBypass case %s passed!' % case_name)170 return True171 return False172173 def getID(self):174 return self.case_id175176 @property177 def getScenario(self):178 return self.__parent179180 def clearAllRuns(self):181 del self.pretty_event182 self.pretty_event = []183 del self.hsd_id184 self.hsd_id = []185 del self.hsd_url186 self.hsd_url = []187 self.pretty_result = 'NA'188 del self.__runs189 self.__runs = []190 self.pass_rate = 0.0191 self.total_count = 0192 self.pass_count = 0193 self.start = '0.0'194 self.end = '0.0'195 self.error_log = ''196 self.std_log = ''197198 c = gc.collect()199 LOGGER.debug("collect {} after del runs instance".format(c))200201 def getRun(self, run_id):202 for run in self.__runs:203 if run.run_id == run_id:204 return run205 return None
...
circuit_breaker_aggregator.py
Source:circuit_breaker_aggregator.py
1import time2from enum import Enum3class ResultType(Enum):4 SUCCESS = "success",5 ERROR = "error"6class WindowType(Enum):7 """8 Window type to circuit breaker takes a decision.9 COUNTER: The state will be changed only when the total failures is greater10 than failure_rate_threshold11 TIMER: The state will be changed when in the window_size the percentage of12 total failures is greather than failure_rate_threshold13 """14 COUNTER = 115 TIMER = 216class CircuitBreakerCounterAggregator:17 def __init__(self, breaker, window_size):18 self.__breaker = breaker19 self.__total_count = 020 self.__fail_count = 021 self.__window_size = window_size22 self.__minimum_number_of_calls = min(self.__window_size,23 self.__breaker.minimum_number_of_calls)24 def record(self, result: ResultType):25 self.__breaker._inc_counter()26 if result == ResultType.ERROR:27 self.__breaker._inc_fail_counter()28 self.__total_count = self.__breaker.counter29 self.__fail_count = self.__breaker.fail_counter30 if self.__total_count >= self.__window_size:31 self.__breaker._state_storage.reset_counters()32 def get_failure_threshold(self):33 if self.__total_count == 0 or self.__total_count < self.__minimum_number_of_calls:34 return -1.035 return (self.__fail_count * 100) / self.__total_count36 @property37 def total(self):38 return self.__total_count39class CircuitBreakerTimerAggregator:40 def __init__(self, breaker, window_size):41 self.__breaker = breaker42 self.__total_count = 043 self.__fail_count = 044 self.__window_size = window_size45 self.__minimum_number_of_calls = self.__breaker.minimum_number_of_calls46 self.__difference_in_seconds = 047 def record(self, result: ResultType):48 timer = time.time()49 self.__breaker._inc_counter_timer(timer)50 if result == ResultType.ERROR:51 self.__breaker._inc_fail_counter_timer(timer)52 self.__total_count = self.__breaker.get_counter_timer(53 timer, self.__window_size)54 self.__fail_count = self.__breaker.get_fail_counter_timer(55 timer, self.__window_size)56 def get_failure_threshold(self):57 print(58 f'VALIDACAO TEMPO={self.__difference_in_seconds < self.__window_size and self.__total_count < self.__minimum_number_of_calls}')59 if self.__total_count == 0 or self.__total_count < self.__minimum_number_of_calls:60 return -1.061 return (self.__fail_count * 100) / self.__total_count62 @property63 def total(self):...
_counter.py
Source:_counter.py
1# encoding: utf-82"""3.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>4"""5from __future__ import absolute_import, unicode_literals6from ._enum import ExitCode7class ResultCounter(object):8 @property9 def success_count(self):10 return self.__success_count11 @property12 def fail_count(self):13 return self.__fail_count14 @property15 def skip_count(self):16 return self.__skip_count17 @property18 def total_count(self):19 return self.success_count + self.fail_count + self.skip_count20 @property21 def created_table_count(self):22 return self.__create_table_count23 def __init__(self):24 self.__create_table_count = 025 self.__success_count = 026 self.__fail_count = 027 self.__skip_count = 028 def __repr__(self):29 return "results: " + ", ".join(30 [31 "success={:d}".format(self.__success_count),32 "failed={:d}".format(self.__fail_count),33 "skip={:s}".format(self.__skip_count),34 "return_code={:d}".format(self.get_return_code()),35 ]36 )37 def inc_success(self, is_create_table):38 self.__success_count += 139 if is_create_table:40 self.__create_table_count += 141 def inc_fail(self):42 self.__fail_count += 143 def inc_skip(self):44 self.__skip_count += 145 def get_return_code(self):46 if self.__success_count > 0:47 return ExitCode.SUCCESS48 if self.__fail_count > 0:49 return ExitCode.FAILED_CONVERT...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!