Skip to content

Commit

Permalink
Add distribution and request csvs to commandline usage.
Browse files Browse the repository at this point in the history
Original-Author: Kevin Peter Savage (https://github.com/kevinpetersavage)
  • Loading branch information
cpennington committed Mar 30, 2015
1 parent a4de8fa commit 783a421
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import inspect
import logging
import socket
import time
from optparse import OptionParser

import web
Expand Down Expand Up @@ -228,6 +229,23 @@ def parse_options():
help="show program's version number and exit"
)

# Writes output statistics as csv
parser.add_option(
'--write-stats',
action='store_true',
dest='write_stats',
default=None,
help="writes output statistics as csv files"
)

parser.add_option(
'--stats-prefix',
action='store',
dest='stats_prefix',
default=None,
help="path prefix to use when writing stats files"
)

# Finalize
# Return three-tuple of parser + the output from parse_args (opt obj, args)
opts, args = parser.parse_args()
Expand Down Expand Up @@ -290,6 +308,28 @@ def is_locust(tup):
)


def write_distribution_stats_csv(file_prefix, timestamp=None):
if timestamp is None:
timestamp = time.time()

file_name = "{0}distribution_{1}.csv".format(file_prefix, timestamp)
console_logger.info("Writing distribution stats to %s", file_name)
with open(file_name, 'wb') as file:
data = runners.locust_runner.stats.get_percentile_dataset(include_empty=True)
file.write(data.csv)


def write_stats_csv(file_prefix, timestamp=None):
if timestamp is None:
timestamp = time.time()

file_name = "{0}stats_{1}.csv".format(file_prefix, timestamp)
console_logger.info("Writing request stats to %s", file_name)
with open(file_name, 'wb') as file:
data = runners.locust_runner.stats.get_request_stats_dataset()
file.write(data.csv)


def load_locustfile(path):
"""
Import given locustfile path and return (docstring, callables).
Expand Down Expand Up @@ -442,6 +482,16 @@ def sig_term_handler():
code = 0
if len(runners.locust_runner.errors):
code = 1

if options.write_stats:
if options.stats_prefix is not None:
file_prefix = os.path.expanduser(options.stats_prefix) + '_'
else:
file_prefix = ''
timestamp = time.time()
write_distribution_stats_csv(file_prefix, timestamp)
write_stats_csv(file_prefix, timestamp)

shutdown(code=code)
except KeyboardInterrupt as e:
shutdown(0)
Expand Down

0 comments on commit 783a421

Please sign in to comment.