From ebea9581102982f4b548ecbc45304abf7674b63b Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Thu, 24 Dec 2020 22:46:19 +0000 Subject: [PATCH] tests:perf:capsys upgrades --- tests/tests_perf.py | 176 ++++++++++++++++++++------------------------ 1 file changed, 78 insertions(+), 98 deletions(-) diff --git a/tests/tests_perf.py b/tests/tests_perf.py index 684b3709..7c1e3271 100644 --- a/tests/tests_perf.py +++ b/tests/tests_perf.py @@ -11,8 +11,7 @@ except ImportError: import sys from tqdm import tqdm, trange -from .tests_tqdm import importorskip, skip, StringIO, closing, _range, \ - patch_lock +from .tests_tqdm import importorskip, skip, _range, patch_lock def cpu_sleep(t): @@ -81,12 +80,6 @@ def retry_on_except(n=3, check_cpu_time=True): return wrapper -class MockIO(StringIO): - """Wraps StringIO to mock a file with no I/O""" - def write(self, _): - return - - def simple_progress(iterable=None, total=None, file=sys.stdout, desc='', leave=False, miniters=1, mininterval=0.1, width=60): """Simple progress bar reproducing tqdm's major features""" @@ -166,64 +159,59 @@ def assert_performance(thresh, name_left, time_left, name_right, time_right): @retry_on_except() -def test_iter_basic_overhead(): +def test_iter_basic_overhead(capsys): """Test overhead of iteration based tqdm""" - total = int(1e6) - with closing(MockIO()) as our_file: - a = 0 - with trange(total, file=our_file) as t: - with relative_timer() as time_tqdm: - for i in t: - a += i - assert a == (total * total - total) / 2.0 - - a = 0 - with relative_timer() as time_bench: - for i in _range(total): + a = 0 + with trange(total) as t: + with relative_timer() as time_tqdm: + for i in t: a += i - our_file.write(a) + assert a == (total * total - total) / 2.0 + + a = 0 + with relative_timer() as time_bench: + for i in _range(total): + a += i + sys.stdout.write(str(a)) assert_performance(3, 'trange', time_tqdm(), 'range', time_bench()) @retry_on_except() -def test_manual_basic_overhead(): +def test_manual_basic_overhead(capsys): """Test overhead of manual tqdm""" - total = int(1e6) - with closing(MockIO()) as our_file: - with tqdm(total=total * 10, file=our_file, leave=True) as t: - a = 0 - with relative_timer() as time_tqdm: - for i in _range(total): - a += i - t.update(10) - + with tqdm(total=total * 10, leave=True) as t: a = 0 - with relative_timer() as time_bench: + with relative_timer() as time_tqdm: for i in _range(total): a += i - our_file.write(a) + t.update(10) + + a = 0 + with relative_timer() as time_bench: + for i in _range(total): + a += i + sys.stdout.write(str(a)) assert_performance(5, 'tqdm', time_tqdm(), 'range', time_bench()) def worker(total, blocking=True): def incr_bar(x): - with closing(StringIO()) as our_file: - for _ in trange(total, file=our_file, lock_args=None if blocking else - (False,), miniters=1, mininterval=0, maxinterval=0): - pass + for _ in trange(total, lock_args=None if blocking else (False,), + miniters=1, mininterval=0, maxinterval=0): + pass return x + 1 return incr_bar @retry_on_except() @patch_lock(thread=True) -def test_lock_args(): +def test_lock_args(capsys): """Test overhead of nonblocking threads""" ThreadPoolExecutor = importorskip('concurrent.futures').ThreadPoolExecutor @@ -246,99 +234,91 @@ def test_lock_args(): @retry_on_except(10) -def test_iter_overhead_hard(): +def test_iter_overhead_hard(capsys): """Test overhead of iteration based tqdm (hard)""" - total = int(1e5) - with closing(MockIO()) as our_file: - a = 0 - with trange(total, file=our_file, leave=True, miniters=1, - mininterval=0, maxinterval=0) as t: - with relative_timer() as time_tqdm: - for i in t: - a += i - assert a == (total * total - total) / 2.0 - - a = 0 - with relative_timer() as time_bench: - for i in _range(total): + a = 0 + with trange(total, leave=True, miniters=1, + mininterval=0, maxinterval=0) as t: + with relative_timer() as time_tqdm: + for i in t: a += i - our_file.write(("%i" % a) * 40) + assert a == (total * total - total) / 2.0 + + a = 0 + with relative_timer() as time_bench: + for i in _range(total): + a += i + sys.stdout.write(("%i" % a) * 40) assert_performance(130, 'trange', time_tqdm(), 'range', time_bench()) @retry_on_except(10) -def test_manual_overhead_hard(): +def test_manual_overhead_hard(capsys): """Test overhead of manual tqdm (hard)""" - total = int(1e5) - with closing(MockIO()) as our_file: - with tqdm(total=total * 10, file=our_file, leave=True, miniters=1, - mininterval=0, maxinterval=0) as t: - a = 0 - with relative_timer() as time_tqdm: - for i in _range(total): - a += i - t.update(10) - + with tqdm(total=total * 10, leave=True, miniters=1, + mininterval=0, maxinterval=0) as t: a = 0 - with relative_timer() as time_bench: + with relative_timer() as time_tqdm: for i in _range(total): a += i - our_file.write(("%i" % a) * 40) + t.update(10) + + a = 0 + with relative_timer() as time_bench: + for i in _range(total): + a += i + sys.stdout.write(("%i" % a) * 40) assert_performance(130, 'tqdm', time_tqdm(), 'range', time_bench()) @retry_on_except(10) -def test_iter_overhead_simplebar_hard(): +def test_iter_overhead_simplebar_hard(capsys): """Test overhead of iteration based tqdm vs simple progress bar (hard)""" - total = int(1e4) - with closing(MockIO()) as our_file: - a = 0 - with trange(total, file=our_file, leave=True, miniters=1, - mininterval=0, maxinterval=0) as t: - with relative_timer() as time_tqdm: - for i in t: - a += i - assert a == (total * total - total) / 2.0 - - a = 0 - s = simple_progress(_range(total), file=our_file, leave=True, - miniters=1, mininterval=0) - with relative_timer() as time_bench: - for i in s: + a = 0 + with trange(total, leave=True, miniters=1, + mininterval=0, maxinterval=0) as t: + with relative_timer() as time_tqdm: + for i in t: a += i + assert a == (total * total - total) / 2.0 + + a = 0 + s = simple_progress(_range(total), leave=True, + miniters=1, mininterval=0) + with relative_timer() as time_bench: + for i in s: + a += i assert_performance(10, 'trange', time_tqdm(), 'simple_progress', time_bench()) @retry_on_except(10) -def test_manual_overhead_simplebar_hard(): +def test_manual_overhead_simplebar_hard(capsys): """Test overhead of manual tqdm vs simple progress bar (hard)""" - total = int(1e4) - with closing(MockIO()) as our_file: - with tqdm(total=total * 10, file=our_file, leave=True, miniters=1, - mininterval=0, maxinterval=0) as t: - a = 0 - with relative_timer() as time_tqdm: - for i in _range(total): - a += i - t.update(10) - - simplebar_update = simple_progress(total=total * 10, file=our_file, leave=True, - miniters=1, mininterval=0) + with tqdm(total=total * 10, leave=True, miniters=1, + mininterval=0, maxinterval=0) as t: a = 0 - with relative_timer() as time_bench: + with relative_timer() as time_tqdm: for i in _range(total): a += i - simplebar_update(10) + t.update(10) + + simplebar_update = simple_progress(total=total * 10, leave=True, + miniters=1, mininterval=0) + a = 0 + with relative_timer() as time_bench: + for i in _range(total): + a += i + simplebar_update(10) assert_performance(10, 'tqdm', time_tqdm(), 'simple_progress', time_bench())