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

Convert benchmark tests into unit tests #1185

Merged
merged 4 commits into from
Jan 31, 2018
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
25 changes: 0 additions & 25 deletions chatterbot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,31 +171,6 @@ def get_response_time(chatbot):
return time.time() - start_time


def generate_strings(total_strings, string_length=20):
"""
Generate a list of random strings.

:param total_strings: The number of strings to generate.
:type total_strings: int

:param string_length: The length of each string to generate.
:type string_length: int

:returns: The generated list of random strings.
:rtype: list
"""
import random
import string

statements = []
for _ in range(0, total_strings):
text = ''.join(
random.choice(string.ascii_letters + string.digits + ' ') for _ in range(string_length)
)
statements.append(text)
return statements


def print_progress_bar(description, iteration_counter, total_items, progress_bar_length=20):
"""
Print progress bar
Expand Down
6 changes: 0 additions & 6 deletions docs/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ ChatBot response time
.. autofunction:: chatterbot.utils.get_response_time


Random string generation
------------------------

.. autofunction:: chatterbot.utils.generate_strings


Parsing datetime information
----------------------------

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description-file = README.md

[nosetests]
verbosity = 3
nocapture = true
exclude = (?:^tests_django$)
with-coverage = true
cover-package = chatterbot
Expand Down
93 changes: 0 additions & 93 deletions tests/benchmarks.py

This file was deleted.

147 changes: 147 additions & 0 deletions tests/test_benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
"""
These tests are designed to test execution time for
various chat bot configurations to help prevent
performance based regressions when changes are made.
"""

from .base_case import ChatBotSQLTestCase, ChatBotMongoTestCase
from chatterbot import ChatBot
from chatterbot import utils
from factory import Faker


# Generate a list of random sentences
STATEMENT_LIST = Faker('sentences', nb=10).generate({})


class BenchmarkingMixin(object):

def get_kwargs(self):
kwargs = super(BenchmarkingMixin, self).get_kwargs()
kwargs['trainer'] = 'chatterbot.trainers.ListTrainer'
kwargs['show_training_progress'] = False
return kwargs

def assert_response_duration(self, maximum_duration, test_kwargs):
"""
Assert that the response time did not exceed the maximum allowed amount.
"""
from sys import stdout

chatbot = ChatBot('Benchmark', **test_kwargs)
chatbot.train(STATEMENT_LIST)

duration = utils.get_response_time(chatbot)

stdout.write('\nBENCHMARK: Duration was %f seconds\n' % duration)

if duration > maximum_duration:
raise AssertionError(
'{duration} was greater than the maximum allowed '
'response time of {maximum_duration}'.format(
duration=duration,
maximum_duration=maximum_duration
)
)


class SqlBenchmarkingTests(BenchmarkingMixin, ChatBotSQLTestCase):
"""
Benchmarking tests for SQL storage.
"""

def get_kwargs(self):
kwargs = super(SqlBenchmarkingTests, self).get_kwargs()
kwargs['storage_adapter'] = 'chatterbot.storage.SQLStorageAdapter'
return kwargs

def test_levenshtein_distance_comparisons(self):
"""
Test the levenshtein distance comparison algorithm.
"""
kwargs = self.get_kwargs()
kwargs.update({
'logic_adapters': [
{
'import_path': 'chatterbot.logic.BestMatch',
'statement_comparison_function': 'chatterbot.comparisons.levenshtein_distance',
'response_selection_method': 'chatterbot.response_selection.get_first_response'
}
]
})

self.assert_response_duration(1, kwargs)

def test_synset_distance_comparisons(self):
"""
Test the synset distance comparison algorithm.
"""
kwargs = self.get_kwargs()
kwargs.update({
'logic_adapters': [
{
'import_path': 'chatterbot.logic.BestMatch',
'statement_comparison_function': 'chatterbot.comparisons.synset_distance',
'response_selection_method': 'chatterbot.response_selection.get_first_response'
}
]
})

self.assert_response_duration(3.5, kwargs)

def test_english_corpus_training(self):
"""
Test the amount of time it takes to train with the English corpus.
"""
self.skipTest('TODO: This test needs to be written.')


class MongoBenchmarkingTests(BenchmarkingMixin, ChatBotMongoTestCase):
"""
Benchmarking tests for Mongo DB storage.
"""

def get_kwargs(self):
kwargs = super(MongoBenchmarkingTests, self).get_kwargs()
kwargs['storage_adapter'] = 'chatterbot.storage.MongoDatabaseAdapter'
return kwargs

def test_levenshtein_distance_comparisons(self):
"""
Test the levenshtein distance comparison algorithm.
"""
kwargs = self.get_kwargs()
kwargs.update({
'logic_adapters': [
{
'import_path': 'chatterbot.logic.BestMatch',
'statement_comparison_function': 'chatterbot.comparisons.levenshtein_distance',
'response_selection_method': 'chatterbot.response_selection.get_first_response'
}
]
})

self.assert_response_duration(1, kwargs)

def test_synset_distance_comparisons(self):
"""
Test the synset distance comparison algorithm.
"""
kwargs = self.get_kwargs()
kwargs.update({
'logic_adapters': [
{
'import_path': 'chatterbot.logic.BestMatch',
'statement_comparison_function': 'chatterbot.comparisons.synset_distance',
'response_selection_method': 'chatterbot.response_selection.get_first_response'
}
]
})

self.assert_response_duration(3.5, kwargs)

def test_english_corpus_training(self):
"""
Test the amount of time it takes to train with the English corpus.
"""
self.skipTest('TODO: This test needs to be written.')
9 changes: 0 additions & 9 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ def test_remove_stop_words(self):
self.assertIn('test', list(words))
self.assertIn('string', list(words))

def test_generate_strings(self):
"""
Test that we can generate 2 strings of length 10.
"""
strings = utils.generate_strings(2, 10)
self.assertEqual(len(strings), 2)
self.assertEqual(len(strings[0]), 10)
self.assertEqual(len(strings[1]), 10)


class UtilityChatBotTestCase(ChatBotTestCase):

Expand Down
6 changes: 0 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ commands =
python runtests.py
python examples/django_app/manage.py test examples/django_app/

[testenv:benchmark]
deps = -rrequirements.txt
commands =
python setup.py develop --no-deps
python tests/benchmarks.py

[testenv:lint]
deps = flake8
commands = flake8
Expand Down