Skip to content

Commit

Permalink
Merge pull request #2193 from SamPosh/stats_summary_refactor
Browse files Browse the repository at this point in the history
stats summary refactoring
  • Loading branch information
cyberw authored Sep 19, 2022
2 parents dd08f3f + 58ab7ca commit 76b3450
Showing 1 changed file with 44 additions and 19 deletions.
63 changes: 44 additions & 19 deletions locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,25 +784,44 @@ def on_worker_report(client_id: str, data: Dict[str, Any]) -> None:


def print_stats(stats: RequestStats, current=True) -> None:
for line in get_stats_summary(stats, current):
console_logger.info(line)
console_logger.info("")


def get_stats_summary(stats: RequestStats, current=True) -> List[str]:
"""
stats summary will be returned as list of string
"""
name_column_width = (STATS_NAME_WIDTH - STATS_TYPE_WIDTH) + 4 # saved characters by compacting other columns
console_logger.info(
summary = list()
summary.append(
("%-" + str(STATS_TYPE_WIDTH) + "s %-" + str(name_column_width) + "s %7s %12s |%7s %7s %7s%7s | %7s %11s")
% ("Type", "Name", "# reqs", "# fails", "Avg", "Min", "Max", "Med", "req/s", "failures/s")
)
separator = f'{"-" * STATS_TYPE_WIDTH}|{"-" * (name_column_width)}|{"-" * 7}|{"-" * 13}|{"-" * 7}|{"-" * 7}|{"-" * 7}|{"-" * 7}|{"-" * 8}|{"-" * 11}'
console_logger.info(separator)
summary.append(separator)
for key in sorted(stats.entries.keys()):
r = stats.entries[key]
console_logger.info(r.to_string(current=current))
console_logger.info(separator)
console_logger.info(stats.total.to_string(current=current))
console_logger.info("")
summary.append(r.to_string(current=current))
summary.append(separator)
summary.append(stats.total.to_string(current=current))
return summary


def print_percentile_stats(stats: RequestStats) -> None:
console_logger.info("Response time percentiles (approximated)")
for line in get_percentile_stats_summary(stats):
console_logger.info(line)
console_logger.info("")


def get_percentile_stats_summary(stats: RequestStats) -> List[str]:
"""
Percentile stats summary will be returned as list of string
"""
summary = ["Response time percentiles (approximated)"]
headers = ("Type", "Name") + tuple(get_readable_percentiles(PERCENTILES_TO_REPORT)) + ("# reqs",)
console_logger.info(
summary.append(
(
f"%-{str(STATS_TYPE_WIDTH)}s %-{str(STATS_NAME_WIDTH)}s %8s "
f"{' '.join(['%6s'] * len(PERCENTILES_TO_REPORT))}"
Expand All @@ -812,29 +831,35 @@ def print_percentile_stats(stats: RequestStats) -> None:
separator = (
f'{"-" * STATS_TYPE_WIDTH}|{"-" * STATS_NAME_WIDTH}|{"-" * 8}|{("-" * 6 + "|") * len(PERCENTILES_TO_REPORT)}'
)[:-1]
console_logger.info(separator)
summary.append(separator)
for key in sorted(stats.entries.keys()):
r = stats.entries[key]
if r.response_times:
console_logger.info(r.percentile())
console_logger.info(separator)
summary.append(r.percentile())
summary.append(separator)

if stats.total.response_times:
console_logger.info(stats.total.percentile())
console_logger.info("")
summary.append(stats.total.percentile())
return summary


def print_error_report(stats: RequestStats) -> None:
if not len(stats.errors):
return
console_logger.info("Error report")
console_logger.info("%-18s %-100s" % ("# occurrences", "Error"))
for line in get_error_report_summary(stats):
console_logger.info(line)


def get_error_report_summary(stats) -> List[str]:
summary = ["Error report"]
summary.append("%-18s %-100s" % ("# occurrences", "Error"))
separator = f'{"-" * 18}|{"-" * ((80 + STATS_NAME_WIDTH) - 19)}'
console_logger.info(separator)
summary.append(separator)
for error in stats.errors.values():
console_logger.info("%-18i %-100s" % (error.occurrences, error.to_name()))
console_logger.info(separator)
console_logger.info("")
summary.append("%-18i %-100s" % (error.occurrences, error.to_name()))
summary.append(separator)
summary.append("")
return summary


def stats_printer(stats: RequestStats) -> Callable[[], None]:
Expand Down

0 comments on commit 76b3450

Please sign in to comment.