Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save stats to file with --statfile option #445

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Copy link

@jamiemul jamiemul Feb 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires from stats import stats_persist.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamiemul add import for stats_persist


def shutdown(code=0):
"""
Expand Down Expand Up @@ -448,3 +461,4 @@ def sig_term_handler():

if __name__ == '__main__':
main()

20 changes: 19 additions & 1 deletion locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .log import console_logger

STATS_NAME_WIDTH = 60

class RequestStatsAdditionError(Exception):
pass

Expand Down Expand Up @@ -512,8 +512,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 TypeError:
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)