From c24d46ff3faa9bb14f716122a252846213287291 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Tue, 10 Nov 2015 23:33:10 +0000 Subject: [PATCH 1/3] performance tests, flake8 coverage for tests --- Makefile | 1 + tox.ini | 1 + tqdm/tests/tests_perf.py | 75 ++++++++++++++++++++++++++++++++++++++++ tqdm/tests/tests_tqdm.py | 6 ++-- 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 tqdm/tests/tests_perf.py diff --git a/Makefile b/Makefile index 9113a6a0..6482e1a6 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,7 @@ flake8: flake8 --max-line-length=80 --count --statistics --exit-zero tqdm/ flake8 --max-line-length=80 --count --statistics --exit-zero examples/ flake8 --max-line-length=80 --count --statistics --exit-zero . + flake8 --max-line-length=80 --count --statistics --exit-zero tqdm/tests/ test: tox --skip-missing-interpreters diff --git a/tox.ini b/tox.ini index 16b54349..58566a14 100644 --- a/tox.ini +++ b/tox.ini @@ -22,6 +22,7 @@ commands = flake8 --max-line-length=80 --count --statistics --exit-zero tqdm/ flake8 --max-line-length=80 --count --statistics --exit-zero examples/ flake8 --max-line-length=80 --count --statistics --exit-zero . + flake8 --max-line-length=80 --count --statistics --exit-zero tqdm/tests/ [testenv:setup.py] deps = diff --git a/tqdm/tests/tests_perf.py b/tqdm/tests/tests_perf.py new file mode 100644 index 00000000..8e98ddcb --- /dev/null +++ b/tqdm/tests/tests_perf.py @@ -0,0 +1,75 @@ +from time import time + +try: + from StringIO import StringIO + _range = xrange +except: + from io import StringIO + _range = range + +from tqdm import trange +from tqdm import tqdm + + +_tic_toc = [None] + + +def tic(): + _tic_toc[0] = time() + + +def toc(): + return time() - _tic_toc[0] + + +def test_iter_overhead(): + """ Test overhead of iteration based tqdm """ + total = int(1e6) + + our_file = StringIO() + a = 0 + tic() + for i in trange(total, file=our_file): # pragma: no cover + a = a + i + time_tqdm = toc() + our_file.close() + + a = 0 + tic() + for i in _range(total): + a = a + i + time_bench = toc() + + # Compute relative overhead of tqdm against native range() + try: + assert(time_tqdm < 5 * time_bench) + except AssertionError: + raise AssertionError('trange(%g): %f, range(%g): %f' % + (total, time_tqdm, total, time_bench)) + +def test_manual_overhead(): + """ Test overhead of manual tqdm """ + total = int(1e6) + + our_file = StringIO() + t = tqdm(total=total*10, file=our_file, leave=True) + a = 0 + tic() + for i in _range(total): + a = a + i + t.update(10) + time_tqdm = toc() + our_file.close() + + a = 0 + tic() + for i in _range(total): + a = a + i + time_bench = toc() + + # Compute relative overhead of tqdm against native range() + try: + assert(time_tqdm < 12 * time_bench) + except AssertionError: + raise AssertionError('tqdm(%g): %f, range(%g): %f' % + (total, time_tqdm, total, time_bench)) diff --git a/tqdm/tests/tests_tqdm.py b/tqdm/tests/tests_tqdm.py index e847bef9..cffbb73a 100644 --- a/tqdm/tests/tests_tqdm.py +++ b/tqdm/tests/tests_tqdm.py @@ -3,12 +3,10 @@ from __future__ import unicode_literals import csv import re from time import sleep - try: from StringIO import StringIO except: from io import StringIO - from tqdm import format_interval from tqdm import format_meter from tqdm import tqdm @@ -34,12 +32,12 @@ def test_format_meter(): assert format_meter(0, 1000, 13, ncols=68, prefix='desc: ') == \ "desc: 0%| | 0/1000 [00:13 Date: Wed, 11 Nov 2015 01:14:03 +0100 Subject: [PATCH 2/3] fix flake8, preformance test overhead threshold when running with coverage --- tqdm/tests/tests_perf.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tqdm/tests/tests_perf.py b/tqdm/tests/tests_perf.py index 8e98ddcb..b8d933d5 100644 --- a/tqdm/tests/tests_perf.py +++ b/tqdm/tests/tests_perf.py @@ -29,7 +29,7 @@ def test_iter_overhead(): our_file = StringIO() a = 0 tic() - for i in trange(total, file=our_file): # pragma: no cover + for i in trange(total, file=our_file): a = a + i time_tqdm = toc() our_file.close() @@ -42,11 +42,12 @@ def test_iter_overhead(): # Compute relative overhead of tqdm against native range() try: - assert(time_tqdm < 5 * time_bench) + assert(time_tqdm < 25 * time_bench) except AssertionError: raise AssertionError('trange(%g): %f, range(%g): %f' % (total, time_tqdm, total, time_bench)) + def test_manual_overhead(): """ Test overhead of manual tqdm """ total = int(1e6) @@ -69,7 +70,7 @@ def test_manual_overhead(): # Compute relative overhead of tqdm against native range() try: - assert(time_tqdm < 12 * time_bench) + assert(time_tqdm < 25 * time_bench) except AssertionError: raise AssertionError('tqdm(%g): %f, range(%g): %f' % (total, time_tqdm, total, time_bench)) From 1921d8746431cc436307a697572dcf9a3b2949be Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Wed, 11 Nov 2015 12:23:15 +0100 Subject: [PATCH 3/3] separate performance tests without coverage to increase speed, Makefile jobserver support --- .travis.yml | 1 + Makefile | 29 +++++++++++++++++------------ setup.py | 7 ++++--- tox.ini | 10 ++++++++-- tqdm/tests/tests_perf.py | 4 ++-- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/.travis.yml b/.travis.yml index 584ddd24..41ee4f1f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ env: - TOXENV=pypy - TOXENV=pypy3 - TOXENV=flake8 + - TOXENV=perf before_install: - pip install codecov install: diff --git a/Makefile b/Makefile index 6482e1a6..b0496cf9 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # IMPORTANT: for compatibility with `python setup.py make [alias]`, ensure: -# 1. Every alias is preceded by @make (eg: @make alias) +# 1. Every alias is preceded by @[+]make (eg: @make alias) # 2. A maximum of one @make alias or command per line # # Sample makefile compatible with `python setup.py make`: @@ -21,6 +21,7 @@ testnose testsetup testcoverage + testperf installdev install build @@ -29,25 +30,26 @@ none alltests: - @make testcoverage - @make flake8 - @make testsetup + @+make testcoverage + @+make testperf + @+make flake8 + @+make testsetup all: - @make alltests - @make build + @+make alltests + @+make build flake8: - flake8 --max-line-length=80 --count --statistics --exit-zero tqdm/ - flake8 --max-line-length=80 --count --statistics --exit-zero examples/ - flake8 --max-line-length=80 --count --statistics --exit-zero . - flake8 --max-line-length=80 --count --statistics --exit-zero tqdm/tests/ + @+flake8 --max-line-length=80 --count --statistics --exit-zero tqdm/ + @+flake8 --max-line-length=80 --count --statistics --exit-zero examples/ + @+flake8 --max-line-length=80 --count --statistics --exit-zero . + @+flake8 --max-line-length=80 --count --statistics --exit-zero tqdm/tests/ test: tox --skip-missing-interpreters testnose: - nosetests tqdm -v + nosetests tqdm --ignore-files="tests_perf\.py" -v testsetup: python setup.py check --restructuredtext --strict @@ -55,7 +57,10 @@ testsetup: testcoverage: rm -f .coverage # coverage erase - nosetests tqdm --with-coverage --cover-package=tqdm -v + nosetests tqdm --with-coverage --cover-package=tqdm --cover-erase --cover-min-percentage=80 --ignore-files="tests_perf\.py" -v + +testperf: # do not use coverage, slows down the perf test and fail + nosetests tqdm/tests/tests_perf.py -v installdev: python setup.py develop --uninstall diff --git a/setup.py b/setup.py index 32a655b5..4bd14aa1 100755 --- a/setup.py +++ b/setup.py @@ -7,10 +7,10 @@ import sys import subprocess # For Makefile parsing import shlex -try: # pragma: no cover +try: # pragma: no cover import ConfigParser import StringIO -except ImportError: # pragma: no cover +except ImportError: # pragma: no cover # Python 3 compatibility import configparser as ConfigParser import io as StringIO @@ -31,7 +31,8 @@ def parse_makefile_aliases(filepath): # Adding a fake section to make the Makefile a valid Ini file ini_str = '[root]\n' with open(filepath, 'r') as fd: - ini_str = ini_str + fd.read().replace('@make ', '') + ini_str = ini_str + fd.read().replace('\t@', '\t').\ + replace('\t+', '\t').replace('\tmake ', '\t') ini_fp = StringIO.StringIO(ini_str) # Parse using ConfigParser config = ConfigParser.RawConfigParser() diff --git a/tox.ini b/tox.ini index 58566a14..a7cec2a3 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ # and then run "tox" from this directory. [tox] -envlist = py26, py27, py32, py33, py34, pypy, pypy3, flake8, setup.py +envlist = py26, py27, py32, py33, py34, pypy, pypy3, flake8, setup.py, perf [testenv] passenv = TRAVIS TRAVIS_JOB_ID TRAVIS_BRANCH @@ -13,7 +13,7 @@ deps = coverage<4 coveralls commands = - nosetests --with-coverage --cover-package=tqdm -v tqdm/ + nosetests --with-coverage --cover-package=tqdm --ignore-files="tests_perf\.py" -v tqdm/ coveralls [testenv:flake8] @@ -31,3 +31,9 @@ deps = commands = python setup.py check --restructuredtext --metadata --strict python setup.py make none + +[testenv:perf] +deps = + nose +commands = + nosetests tqdm/tests/tests_perf.py -v diff --git a/tqdm/tests/tests_perf.py b/tqdm/tests/tests_perf.py index b8d933d5..831272fb 100644 --- a/tqdm/tests/tests_perf.py +++ b/tqdm/tests/tests_perf.py @@ -42,7 +42,7 @@ def test_iter_overhead(): # Compute relative overhead of tqdm against native range() try: - assert(time_tqdm < 25 * time_bench) + assert(time_tqdm < 6 * time_bench) except AssertionError: raise AssertionError('trange(%g): %f, range(%g): %f' % (total, time_tqdm, total, time_bench)) @@ -70,7 +70,7 @@ def test_manual_overhead(): # Compute relative overhead of tqdm against native range() try: - assert(time_tqdm < 25 * time_bench) + assert(time_tqdm < 17 * time_bench) except AssertionError: raise AssertionError('tqdm(%g): %f, range(%g): %f' % (total, time_tqdm, total, time_bench))