Skip to content

Commit

Permalink
Merge pull request #1245 from ajt89/no-web-failures-file
Browse files Browse the repository at this point in the history
Save failures.csv in --no-web mode
  • Loading branch information
cyberw authored Jan 28, 2020
2 parents 5f2d915 + 4a9fb23 commit 13bf7ef
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
5 changes: 4 additions & 1 deletion locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,13 +765,16 @@ def stats_writer(base_filepath, stats_history_enabled=False):


def write_stat_csvs(base_filepath, stats_history_enabled=False):
"""Writes the requests and distribution csvs."""
"""Writes the requests, distribution, and failures csvs."""
with open(base_filepath + '_stats.csv', 'w') as f:
f.write(requests_csv())

with open(base_filepath + '_stats_history.csv', 'a') as f:
f.write(stats_history_csv(stats_history_enabled) + "\n")

with open(base_filepath + '_failures.csv', 'w') as f:
f.write(failures_csv())


def sort_stats(stats):
return [stats[key] for key in sorted(six.iterkeys(stats))]
Expand Down
42 changes: 41 additions & 1 deletion locust/test/test_stats.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import time
import unittest
import re
import os

import locust
from locust.core import HttpLocust, TaskSet, task
from locust.core import HttpLocust, TaskSet, task, Locust
from locust.inspectlocust import get_task_ratio_dict
from locust.rpc.protocol import Message
from locust.stats import CachedResponseTimes, RequestStats, StatsEntry, diff_response_time_dicts, global_stats
from locust.test.testcases import LocustTestCase
from six.moves import xrange

from .testcases import WebserverTestCase
from .test_runners import mocked_options


class TestRequestStats(unittest.TestCase):
Expand Down Expand Up @@ -269,6 +271,44 @@ def test_print_percentile_stats(self):
self.assertEqual(len(headlines), len(info[5].split()))


class TestWriteStatCSVs(LocustTestCase):
STATS_BASE_NAME = "test"
STATS_FILENAME = "{}_stats.csv".format(STATS_BASE_NAME)
STATS_HISTORY_FILENAME = "{}_stats_history.csv".format(STATS_BASE_NAME)
STATS_FAILURES_FILENAME = "{}_failures.csv".format(STATS_BASE_NAME)

def setUp(self):
class User(Locust):
setup_run_count = 0
task_run_count = 0
locust_error_count = 0
wait_time = locust.wait_time.constant(1)

class task_set(TaskSet):
@task
def my_task(self):
User.task_run_count += 1
locust.runners.locust_runner = locust.runners.LocalLocustRunner([User], mocked_options())
self.remove_file_if_exists(self.STATS_FILENAME)
self.remove_file_if_exists(self.STATS_HISTORY_FILENAME)
self.remove_file_if_exists(self.STATS_FAILURES_FILENAME)

def tearDown(self):
locust.runners.locust_runner.quit()
self.remove_file_if_exists(self.STATS_FILENAME)
self.remove_file_if_exists(self.STATS_HISTORY_FILENAME)
self.remove_file_if_exists(self.STATS_FAILURES_FILENAME)

def remove_file_if_exists(self, filename):
if os.path.exists(filename):
os.remove(filename)

def test_write_stat_csvs(self):
locust.stats.write_stat_csvs(self.STATS_BASE_NAME)
self.assertTrue(os.path.exists(self.STATS_FILENAME))
self.assertTrue(os.path.exists(self.STATS_HISTORY_FILENAME))
self.assertTrue(os.path.exists(self.STATS_FAILURES_FILENAME))

class TestStatsEntryResponseTimesCache(unittest.TestCase):
def setUp(self, *args, **kwargs):
super(TestStatsEntryResponseTimesCache, self).setUp(*args, **kwargs)
Expand Down

0 comments on commit 13bf7ef

Please sign in to comment.