Best Python code snippet using yandex-tank
criterions.py
Source:criterions.py  
...38            "Average response time higher"39            " than %sms for %ss, since %s" %40            (self.rt_limit, self.seconds_count, self.cause_second[0]["ts"]))41        return explanation42    def widget_explain(self):43        items = (self.rt_limit, self.seconds_count, self.seconds_limit)44        return "Avg Time >%sms for %s/%ss" % items, float(45            self.seconds_count) / self.seconds_limit46class HTTPCodesCriterion(AbstractCriterion):47    """ HTTP codes criterion """48    @staticmethod49    def get_type_string():50        return 'http'51    def __init__(self, autostop, param_str):52        AbstractCriterion.__init__(self)53        self.seconds_count = 054        self.codes_mask = param_str.split(',')[0].lower()55        self.codes_regex = re.compile(self.codes_mask.replace("x", '.'))56        self.autostop = autostop57        level_str = param_str.split(',')[1].strip()58        if level_str[-1:] == '%':59            self.level = float(level_str[:-1]) / 10060            self.is_relative = True61        else:62            self.level = int(level_str)63            self.is_relative = False64        self.seconds_limit = expand_to_seconds(param_str.split(',')[2])65    def notify(self, data, stat):66        matched_responses = self.count_matched_codes(67            self.codes_regex, data["overall"]["proto_code"]["count"])68        if self.is_relative:69            if data["overall"]["interval_real"]["len"]:70                matched_responses = float(matched_responses) / data["overall"][71                    "interval_real"]["len"]72            else:73                matched_responses = 074        logger.debug(75            "HTTP codes matching mask %s: %s/%s", self.codes_mask,76            matched_responses, self.level)77        if matched_responses >= self.level:78            if not self.seconds_count:79                self.cause_second = (data, stat)80            logger.debug(self.explain())81            self.seconds_count += 182            self.autostop.add_counting(self)83            if self.seconds_count >= self.seconds_limit:84                return True85        else:86            self.seconds_count = 087        return False88    def get_rc(self):89        return self.RC_HTTP90    def get_level_str(self):91        """ format level str """92        if self.is_relative:93            level_str = str(100 * self.level) + "%"94        else:95            level_str = self.level96        return level_str97    def explain(self):98        items = (99            self.codes_mask, self.get_level_str(), self.seconds_count,100            self.cause_second[0].get('ts'))101        return "%s codes count higher than %s for %ss, since %s" % items102    def widget_explain(self):103        items = (104            self.codes_mask, self.get_level_str(), self.seconds_count,105            self.seconds_limit)106        return "HTTP %s>%s for %s/%ss" % items, float(107            self.seconds_count) / self.seconds_limit108class NetCodesCriterion(AbstractCriterion):109    """ Net codes criterion """110    @staticmethod111    def get_type_string():112        return 'net'113    def __init__(self, autostop, param_str):114        AbstractCriterion.__init__(self)115        self.seconds_count = 0116        self.codes_mask = param_str.split(',')[0].lower()117        self.codes_regex = re.compile(self.codes_mask.replace("x", '.'))118        self.autostop = autostop119        level_str = param_str.split(',')[1].strip()120        if level_str[-1:] == '%':121            self.level = float(level_str[:-1]) / 100122            self.is_relative = True123        else:124            self.level = int(level_str)125            self.is_relative = False126        self.seconds_limit = expand_to_seconds(param_str.split(',')[2])127    def notify(self, data, stat):128        codes = copy.deepcopy(data["overall"]["net_code"]["count"])129        if '0' in codes.keys():130            codes.pop('0')131        matched_responses = self.count_matched_codes(self.codes_regex, codes)132        if self.is_relative:133            if data["overall"]["interval_real"]["len"]:134                matched_responses = float(matched_responses) / data["overall"][135                    "interval_real"]["len"]136            else:137                matched_responses = 0138        logger.debug(139            "Net codes matching mask %s: %s/%s", self.codes_mask,140            matched_responses, self.level)141        if matched_responses >= self.level:142            if not self.seconds_count:143                self.cause_second = (data, stat)144            logger.debug(self.explain())145            self.seconds_count += 1146            self.autostop.add_counting(self)147            if self.seconds_count >= self.seconds_limit:148                return True149        else:150            self.seconds_count = 0151        return False152    def get_rc(self):153        return self.RC_NET154    def get_level_str(self):155        """ format level str """156        if self.is_relative:157            level_str = str(100 * self.level) + "%"158        else:159            level_str = self.level160        return level_str161    def explain(self):162        items = (163            self.codes_mask, self.get_level_str(), self.seconds_count,164            self.cause_second[0].get("ts"))165        return "%s net codes count higher than %s for %ss, since %s" % items166    def widget_explain(self):167        items = (168            self.codes_mask, self.get_level_str(), self.seconds_count,169            self.seconds_limit)170        return "Net %s>%s for %s/%ss" % items, float(171            self.seconds_count) / self.seconds_limit172class QuantileCriterion(AbstractCriterion):173    """ quantile criterion """174    @staticmethod175    def get_type_string():176        return 'quantile'177    def __init__(self, autostop, param_str):178        AbstractCriterion.__init__(self)179        self.seconds_count = 0180        self.quantile = float(param_str.split(',')[0])181        self.rt_limit = expand_to_milliseconds(param_str.split(',')[1])182        self.seconds_limit = expand_to_seconds(param_str.split(',')[2])183        self.autostop = autostop184    def notify(self, data, stat):185        quantiles = dict(186            zip(187                data["overall"]["interval_real"]["q"]["q"], data["overall"][188                    "interval_real"]["q"]["value"]))189        if self.quantile not in quantiles.keys():190            logger.warning("No quantile %s in %s", self.quantile, quantiles)191        if self.quantile in quantiles.keys() \192                and quantiles[self.quantile] / 1000.0 > self.rt_limit:193            if not self.seconds_count:194                self.cause_second = (data, stat)195            logger.debug(self.explain())196            self.seconds_count += 1197            self.autostop.add_counting(self)198            if self.seconds_count >= self.seconds_limit:199                return True200        else:201            self.seconds_count = 0202        return False203    def get_rc(self):204        return self.RC_TIME205    def explain(self):206        items = (207            self.quantile, self.rt_limit, self.seconds_count,208            self.cause_second[0].get("ts"))209        return "Percentile %s higher than %sms for %ss, since %s" % items210    def widget_explain(self):211        items = (212            self.quantile, self.rt_limit, self.seconds_count,213            self.seconds_limit)214        return "%s%% >%sms for %s/%ss" % items, float(215            self.seconds_count) / self.seconds_limit216class SteadyCumulativeQuantilesCriterion(AbstractCriterion):217    """ quantile criterion """218    @staticmethod219    def get_type_string():220        return 'steady_cumulative'221    def __init__(self, autostop, param_str):222        raise NotImplementedError223        AbstractCriterion.__init__(self)224        self.seconds_count = 0225        self.quantile_hash = ""226        self.seconds_limit = expand_to_seconds(param_str.split(',')[0])227        self.autostop = autostop228    def notify(self, data, stat):229        quantiles = dict(230            zip(data["overall"]["q"]["q"], data["overall"]["q"]["values"]))231        quantile_hash = json.dumps(quantiles)232        logging.debug("Cumulative quantiles hash: %s", quantile_hash)233        if self.quantile_hash == quantile_hash:234            if not self.seconds_count:235                self.cause_second = (data, stat)236            logger.debug(self.explain())237            self.seconds_count += 1238            self.autostop.add_counting(self)239            if self.seconds_count >= self.seconds_limit:240                return True241        else:242            self.seconds_count = 0243        self.quantile_hash = quantile_hash244        return False245    def get_rc(self):246        return self.RC_STEADY247    def explain(self):248        items = (self.seconds_count, self.cause_second[0]["ts"])249        return "Cumulative percentiles are steady for %ss, since %s" % items250    def widget_explain(self):251        items = (self.seconds_count, self.seconds_limit)252        return "Steady for %s/%ss" % items, float(253            self.seconds_count) / self.seconds_limit254class TimeLimitCriterion(AbstractCriterion):255    """ time limit criterion """256    @staticmethod257    def get_type_string():258        return 'limit'259    def __init__(self, autostop, param_str):260        AbstractCriterion.__init__(self)261        self.start_time = time.time()262        self.end_time = time.time()263        self.time_limit = expand_to_seconds(param_str)264    def notify(self, data, stat):265        self.end_time = time.time()266        return (self.end_time - self.start_time) > self.time_limit267    def get_rc(self):268        return self.RC_TIME269    def explain(self):270        return "Test time elapsed. Limit: %ss, actual time: %ss" % (271            self.time_limit, self.end_time - self.start_time)272    def widget_explain(self):273        return "Time limit: %ss, actual time: %ss" % (...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!!
