Skip to content

Commit

Permalink
Convert benchmark tests into unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jan 27, 2018
1 parent 44df713 commit 0fd5dfe
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 99 deletions.
93 changes: 0 additions & 93 deletions tests/benchmarks.py

This file was deleted.

158 changes: 158 additions & 0 deletions tests/test_benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
"""
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
import sys
import logging


logging.basicConfig(
stream=sys.stdout,
level=logging.INFO
)

STATEMENT_LIST = utils.generate_strings(10)


class BenchmarkingMixin(object):

def setUp(self):
super(BenchmarkingMixin, self).setUp()

self.logger = logging.getLogger(__name__)

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.
"""

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

duration = utils.get_response_time(chatbot)

self.logger.info('Duration was %f seconds' % 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(1.9, kwargs)

def test_english_corpus_training(self):
"""
Test the amount of time it takes to train with the English corpus.
"""
import unittest
raise unittest.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(1.9, kwargs)

def test_english_corpus_training(self):
"""
Test the amount of time it takes to train with the English corpus.
"""
import unittest
raise unittest.SkipTest('TODO: This test needs to be written.')
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

0 comments on commit 0fd5dfe

Please sign in to comment.