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 failures.csv in --no-web mode #1245

Merged
merged 5 commits into from
Jan 28, 2020
Merged
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
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