From 7be71a01213698a66a79142c832772d80e9aff37 Mon Sep 17 00:00:00 2001 From: nvbn Date: Wed, 26 Aug 2015 14:34:39 +0300 Subject: [PATCH] #334 Add performance test --- requirements.txt | 1 + tests/functional/test_performance.py | 56 ++++++++++++++++++++++++++++ tests/functional/utils.py | 17 +++++---- 3 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 tests/functional/test_performance.py diff --git a/requirements.txt b/requirements.txt index 92ed391f..76ca36a2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ wheel setuptools>=17.1 pexpect pypandoc +pytest-benchmark diff --git a/tests/functional/test_performance.py b/tests/functional/test_performance.py new file mode 100644 index 00000000..2310f158 --- /dev/null +++ b/tests/functional/test_performance.py @@ -0,0 +1,56 @@ +from pexpect import TIMEOUT +import pytest +import time +from tests.functional.utils import spawn, functional, bare + +dockerfile = u''' +FROM ubuntu:latest +RUN apt-get update +RUN apt-get install -yy python3 python3-pip python3-dev git +RUN pip3 install -U setuptools +RUN ln -s /usr/bin/pip3 /usr/bin/pip +RUN adduser --disabled-password --gecos '' test +ENV SEED "{seed}" +COPY thefuck /src +WORKDIR /src +RUN pip install . +USER test +RUN echo 'eval $(thefuck --alias)' > /home/test/.bashrc +RUN echo > /home/test/.bash_history +RUN git config --global user.email "you@example.com" +RUN git config --global user.name "Your Name" +'''.format(seed=time.time()) + + +@pytest.fixture +def proc(request): + return spawn(request, 'ubuntu-python3-bash-performance', + dockerfile, u'bash', install=False, copy_src=True) + + +def plot(proc): + proc.sendline(u'cd /home/test/') + proc.sendline(u'fuck') + assert proc.expect([TIMEOUT, u'No fucks given']) + proc.sendline(u'git init') + proc.sendline(u'git add .') + proc.sendline(u'git commit -a -m init') + proc.sendline(u'git brnch') + proc.sendline(u'fuck') + assert proc.expect([TIMEOUT, u'git branch']) + proc.send('\n') + assert proc.expect([TIMEOUT, u'master']) + proc.sendline(u'echo test') + proc.sendline(u'echo tst') + proc.sendline(u'fuck') + assert proc.expect([TIMEOUT, u'echo test']) + proc.send('\n') + assert proc.expect([TIMEOUT, u'test']) + + +@functional +@pytest.mark.skipif( + bool(bare), reason='Would lie on a bare run') +@pytest.mark.benchmark(min_rounds=10) +def test_performance(proc, benchmark): + assert benchmark(plot, proc) is None diff --git a/tests/functional/utils.py b/tests/functional/utils.py index 6efe05e6..558d198c 100644 --- a/tests/functional/utils.py +++ b/tests/functional/utils.py @@ -8,28 +8,31 @@ import sys import pexpect from tests.utils import root + bare = os.environ.get('BARE') enabled = os.environ.get('FUNCTIONAL') -def build_container(tag, dockerfile): +def build_container(tag, dockerfile, copy_src=False): tmpdir = mkdtemp() try: - with Path(tmpdir).joinpath('Dockerfile').open('w') as file: + if copy_src: + subprocess.call(['cp', '-a', str(root), tmpdir]) + dockerfile_path = Path(tmpdir).joinpath('Dockerfile') + with dockerfile_path.open('w') as file: file.write(dockerfile) - if subprocess.call(['docker', 'build', '--tag={}'.format(tag), tmpdir], - cwd=str(root)) != 0: + if subprocess.call(['docker', 'build', '--tag={}'.format(tag), tmpdir]) != 0: raise Exception("Can't build a container") finally: shutil.rmtree(tmpdir) -def spawn(request, tag, dockerfile, cmd, install=True): +def spawn(request, tag, dockerfile, cmd, install=True, copy_src=False): if bare: proc = pexpect.spawnu(cmd) else: tag = 'thefuck/{}'.format(tag) - build_container(tag, dockerfile) + build_container(tag, dockerfile, copy_src) proc = pexpect.spawnu('docker run --volume {}:/src --tty=true ' '--interactive=true {} {}'.format(root, tag, cmd)) if install: @@ -39,7 +42,7 @@ def spawn(request, tag, dockerfile, cmd, install=True): proc.logfile = sys.stdout - request.addfinalizer(proc.terminate) + request.addfinalizer(lambda: proc.terminate(True)) return proc