Пример #1
0
def calculate_analytics_dataset(build_id, test_name, lg_type, start_time, end_time, aggregation, sampler,
                                scope, metric, status):
    data = None
    axe = 'count'
    if metric == "Throughput":
        timestamps, data, _ = get_tps(build_id, test_name, lg_type, start_time, end_time, aggregation, sampler,
                                      scope=scope, status=status)
        data = data['responses']
    # elif metric == "Hits":
    #     timestamps, data, _ = get_hits(build_id, test_name, lg_type, start_time, end_time, aggregation, sampler,
    #                                    scope=scope, status=status)
    #     data = data['hits']
    elif metric == "Errors":
        timestamps, data, _ = get_errors(build_id, test_name, lg_type, start_time, end_time, aggregation, sampler,
                                         scope=scope)
        data = data['errors']
    elif metric in ["Min", "Median", "Max", "pct90", "pct95", "pct99"]:
        timestamps, data, _ = get_backend_requests(build_id, test_name, lg_type, start_time, end_time, aggregation,
                                                   sampler, scope=scope, aggr=metric, status=status)
        data = data['response']
        axe = 'time'

    elif "xx" in metric:
        timestamps, data, _ = get_response_codes(build_id, test_name, lg_type, start_time, end_time, aggregation,
                                                 sampler, scope=scope, aggr=metric, status=status)
        data = data['rcodes']
    return data, axe
Пример #2
0
    def get(self):
        args = self._parser_get.parse_args(strict=False)
        project = Project.get_or_404(args["project_id"])
        report = APIReport.query.filter(
            and_(APIReport.id == args['test_id'],
                 APIReport.project_id == project.id)).first()
        start_time = str_to_timestamp(report.start_time)
        if report.end_time:
            current_time = str_to_timestamp(report.end_time)
        else:
            current_time = datetime.utcnow().timestamp()
        str_start_time = datetime.fromtimestamp(
            start_time, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.000Z")
        str_current_time = datetime.fromtimestamp(
            current_time, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.000Z")
        duration_till_now = current_time - start_time
        if duration_till_now < args['wait_till']:
            return {"message": "not enough results", "code": 0}
        ts_array, data, users = get_tps(report.build_id,
                                        report.name,
                                        report.lg_type,
                                        str_start_time,
                                        str_current_time,
                                        "1s",
                                        args["sampler"],
                                        scope=args["request"],
                                        status=args["status"])
        users = list(users["users"].values())
        _tmp = []
        tps = list()
        usrs = list()
        errors = list()
        if 'm' in args["aggregation"]:
            ss = int(args["aggregation"].replace('m', '')) * 60
        elif 's' in args["aggregation"]:
            ss = int(args["aggregation"].replace('s', ''))
        else:
            ss = 60
        calculation_mapping = {
            'max': max,
            'min': min,
            'mean': mean,
            'sum': sum
        }
        calculation = args['calculation'] if args['calculation'] in list(
            calculation_mapping.keys()) else 'sum'
        for index, _ in enumerate(data["responses"].values()):
            if isinstance(_, int):
                _tmp.append(_)
            if len(_tmp) and (len(_tmp) % ss) == 0:
                tps.append(round(calculation_mapping[calculation](_tmp)))
                usrs.append(users[index])
                _tmp = []
        if _tmp:
            tps.append(round(calculation_mapping[calculation](_tmp)))
            usrs.append(users[-1])
            _tmp = []
        _, data, _ = get_errors(report.build_id,
                                report.name,
                                report.lg_type,
                                str_start_time,
                                str_current_time,
                                args["aggregation"],
                                args["sampler"],
                                scope=args["request"])
        for each in data['errors'].values():
            if each:
                errors.append(each)
        total = int(
            get_response_time_per_test(report.build_id, report.name,
                                       report.lg_type, args["sampler"],
                                       args["request"], "total"))
        error_rate = float(sum(errors)) / float(total) * 100
        try:
            max_tp, user_index = arrays.non_decreasing(
                tps[:-1], deviation=args["deviation"], val=True)
            max_users = args["u_aggr"] * round(
                usrs[user_index] / args["u_aggr"])
            max_tp = tps[user_index]
            current_users = args["u_aggr"] * round(
                usrs[user_index + 1] / args["u_aggr"])
            if current_users == max_users:
                current_users += args["u_aggr"]
            if current_users == 0:
                current_users = max_users + args["u_aggr"]
            if current_users < max_users:
                current_users = max_users + args["u_aggr"]
        except TypeError:
            return {"message": "not enough results", "code": 0}
        except IndexError:
            if error_rate > args["max_errors"]:
                return {
                    "message":
                    f"error rate reached 100% for {args['request']} transaction",
                    "errors_rate": 100.0,
                    "code": 1
                }
            else:
                return {"message": "not enough results", "code": 0}

        response = {
            "ts": ts_array[-1],
            "max_users": max_users,
            "max_throughput": round(max_tp, 2),
            "current_users": current_users,
            "current_throughput": round(tps[user_index], 2),
            "errors_rate": round(error_rate, 2)
        }
        if args["u"]:
            user_array = args["u"]
            if max_users not in user_array:
                user_array.append(max_users)
            if current_users not in user_array:
                user_array.append(current_users)
            user_array.sort()
            user_array.reverse()
            uber_array = {}
            _, users = get_backend_users(report.build_id, report.lg_type,
                                         str_start_time, str_current_time,
                                         "1s")
            u = user_array.pop()
            start_time = _[0]
            end_time = _[-1]
            response["test_start"] = start_time
            response["test_end"] = end_time
            for key, value in users["users"].items():
                if value > u and max_users >= u:
                    _, data, _ = get_tps(report.build_id,
                                         report.name,
                                         report.lg_type,
                                         start_time,
                                         key,
                                         "1s",
                                         args["sampler"],
                                         scope=args["request"],
                                         status=args["status"])
                    tp = [
                        0 if v is None else v
                        for v in list(data['responses'].values())[:-1]
                    ]
                    array_size = len(tp)
                    tp = calculation_mapping[calculation](
                        tp) if len(tp) != 0 else 0
                    if array_size != ss:
                        coefficient = float(array_size / ss)
                    else:
                        coefficient = 1
                    tp = int(tp / coefficient)
                    if round(
                            tp, 2
                    ) > response["max_throughput"] and u != current_users:
                        response["max_throughput"] = round(tp, 2)
                        response["max_users"] = u
                        current_users = u + args["u_aggr"]
                        response["current_users"] = current_users
                    _, data, _ = get_backend_requests(report.build_id,
                                                      report.name,
                                                      report.lg_type,
                                                      start_time,
                                                      key,
                                                      "1s",
                                                      args["sampler"],
                                                      scope=args["request"],
                                                      status=args["status"])
                    rt = []
                    started = False
                    for v in list(data['response'].values())[:-1]:
                        if v and v > 0:
                            started = True
                        if started and v and v > 0:
                            rt.append(v)
                    rt = self.part(rt, args["u_aggr"]) if rt else 0
                    uber_array[str(u)] = {
                        "throughput": round(tp, 2),
                        "response_time": round(rt, 2)
                    }
                    start_time = key
                    u = user_array.pop()
            if str(response["max_users"]) not in list(uber_array.keys()):
                _, data, _ = get_backend_requests(report.build_id,
                                                  report.name,
                                                  report.lg_type,
                                                  start_time,
                                                  end_time,
                                                  "1s",
                                                  args["sampler"],
                                                  scope=args["request"],
                                                  status=args["status"])
                rt = []
                started = False
                for v in list(data['response'].values())[:-1]:
                    if v and v > 0:
                        started = True
                    if started and v and v > 0:
                        rt.append(v)
                rt = self.part(rt, args["u_aggr"]) if rt else 0
                uber_array[str(max_users)] = {
                    "throughput": response["max_throughput"],
                    "response_time": round(rt, 2)
                }
            else:
                uber_array[str(response["max_users"]
                               )]["throughput"] = response["max_throughput"]
            if str(current_users) not in list(uber_array.keys()):

                uber_array[str(current_users)] = {
                    "throughput":
                    response["current_throughput"],
                    "response_time":
                    uber_array.get(
                        str(max_users),
                        list(uber_array.values())[-1])["response_time"]
                }
            else:
                uber_array[str(current_users)]["throughput"] = response[
                    "current_throughput"]
            response["benchmark"] = uber_array
        if args["extended_output"]:
            response["details"] = {}
            response["tps"] = tps
            for index, value in enumerate(usrs):
                if not response["details"].get(
                        value) or response["details"][value] > tps[index]:
                    response["details"][value] = tps[index]
        if (arrays.non_decreasing(tps[:-1], deviation=args["deviation"])
                and error_rate <= args["max_errors"]
                and response["current_throughput"] *
            (1 + args["max_deviation"]) >= response["max_throughput"]):
            response["message"] = "proceed"
            response["code"] = 0
            return response
        else:
            response["message"] = "saturation"
            response["code"] = 1
            return response