From ec40ec4eb87b6aba31608e58194bb4ac1b1d64bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mesut=20G=C3=BCne=C5=9F?= Date: Fri, 22 Jan 2016 14:05:36 +0200 Subject: [PATCH 1/3] fixing --statfile option if value is None --- locust/main.py | 16 +++++++++++++++- locust/stats.py | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/locust/main.py b/locust/main.py index a7b41a6d53..682c116d74 100644 --- a/locust/main.py +++ b/locust/main.py @@ -12,7 +12,7 @@ import web from log import setup_logging, console_logger -from stats import stats_printer, print_percentile_stats, print_error_report, print_stats +from stats import stats_printer, print_percentile_stats, print_error_report, print_stats, stats_persist from inspectlocust import print_task_ratio, get_task_ratio_dict from core import Locust, HttpLocust from runners import MasterLocustRunner, SlaveLocustRunner, LocalLocustRunner @@ -228,6 +228,16 @@ def parse_options(): help="show program's version number and exit" ) + # A file that contains the current request stats. + parser.add_option( + '--statsfile', + action='store', + type='str', + dest='statsfile', + default=None, + help="Store current request stats to this file in CSV format.", + ) + # Finalize # Return three-tuple of parser + the output from parse_args (opt obj, args) opts, args = parser.parse_args() @@ -416,6 +426,9 @@ def main(): if not options.only_summary and (options.print_stats or (options.no_web and not options.slave)): # spawn stats printing greenlet gevent.spawn(stats_printer) + + if options.statsfile: + gevent.spawn(stats_persist, options.statsfile) def shutdown(code=0): """ @@ -448,3 +461,4 @@ def sig_term_handler(): if __name__ == '__main__': main() + \ No newline at end of file diff --git a/locust/stats.py b/locust/stats.py index 267dee4e4e..2dedf59a5a 100644 --- a/locust/stats.py +++ b/locust/stats.py @@ -7,7 +7,7 @@ from log import console_logger STATS_NAME_WIDTH = 60 - + class RequestStatsAdditionError(Exception): pass @@ -496,8 +496,26 @@ def print_error_report(): console_logger.info("-" * (80 + STATS_NAME_WIDTH)) console_logger.info("") +def store_stats(filename, stats): + with open(filename, "w") as f: + f.write("path,method,num_requests,num_failures,min_response_time,max_response_time,avg_response_time\n") + for k in stats: + r = stats[k] + # need to add try - except block if one of the value is None + try: + f.write("%s,%s,%d,%d,%d,%d,%d\n" % (r.name,r.method,r.num_requests,r.num_failures,r.min_response_time,r.max_response_time,r.avg_response_time)); + except: + pass + def stats_printer(): from runners import locust_runner while True: print_stats(locust_runner.request_stats) gevent.sleep(2) + +def stats_persist(filename): + from runners import locust_runner + while True: + store_stats(filename, locust_runner.request_stats); + gevent.sleep(2) + \ No newline at end of file From 9e46f5fb1d084f3fb574706330014597c79032f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mesut=20G=C3=BCne=C5=9F?= Date: Wed, 1 Mar 2017 17:38:56 +0300 Subject: [PATCH 2/3] exception for only TypeError thanks @justiniso we should raise exception for only TypeError --- locust/stats.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/locust/stats.py b/locust/stats.py index c799707977..35ca94ed43 100644 --- a/locust/stats.py +++ b/locust/stats.py @@ -520,7 +520,7 @@ def store_stats(filename, stats): # need to add try - except block if one of the value is None try: f.write("%s,%s,%d,%d,%d,%d,%d\n" % (r.name,r.method,r.num_requests,r.num_failures,r.min_response_time,r.max_response_time,r.avg_response_time)); - except: + except TypeError: pass def stats_printer(): @@ -534,4 +534,4 @@ def stats_persist(filename): while True: store_stats(filename, locust_runner.request_stats); gevent.sleep(2) - \ No newline at end of file + From 54f700532c8b92c514ac5fdf69f802d9d46a09ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mesut=20G=C3=BCne=C5=9F?= Date: Wed, 1 Mar 2017 17:40:23 +0300 Subject: [PATCH 3/3] importing stats_persist --- locust/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/locust/main.py b/locust/main.py index ad16b171d0..8ef47dc990 100644 --- a/locust/main.py +++ b/locust/main.py @@ -12,7 +12,7 @@ from . import web from .log import setup_logging, console_logger -from .stats import stats_printer, print_percentile_stats, print_error_report, print_stats +from .stats import stats_printer, print_percentile_stats, print_error_report, print_stats, stats_persist from .inspectlocust import print_task_ratio, get_task_ratio_dict from .core import Locust, HttpLocust from .runners import MasterLocustRunner, SlaveLocustRunner, LocalLocustRunner @@ -461,4 +461,4 @@ def sig_term_handler(): if __name__ == '__main__': main() - \ No newline at end of file +