tests:perf:capsys upgrades

This commit is contained in:
Casper da Costa-Luis 2020-12-24 22:46:19 +00:00
parent 31367d0a7a
commit ebea958110
No known key found for this signature in database
GPG Key ID: 986B408043AE090D
1 changed files with 78 additions and 98 deletions

View File

@ -11,8 +11,7 @@ except ImportError:
import sys import sys
from tqdm import tqdm, trange from tqdm import tqdm, trange
from .tests_tqdm import importorskip, skip, StringIO, closing, _range, \ from .tests_tqdm import importorskip, skip, _range, patch_lock
patch_lock
def cpu_sleep(t): def cpu_sleep(t):
@ -81,12 +80,6 @@ def retry_on_except(n=3, check_cpu_time=True):
return wrapper 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='', def simple_progress(iterable=None, total=None, file=sys.stdout, desc='',
leave=False, miniters=1, mininterval=0.1, width=60): leave=False, miniters=1, mininterval=0.1, width=60):
"""Simple progress bar reproducing tqdm's major features""" """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() @retry_on_except()
def test_iter_basic_overhead(): def test_iter_basic_overhead(capsys):
"""Test overhead of iteration based tqdm""" """Test overhead of iteration based tqdm"""
total = int(1e6) total = int(1e6)
with closing(MockIO()) as our_file: a = 0
a = 0 with trange(total) as t:
with trange(total, file=our_file) as t: with relative_timer() as time_tqdm:
with relative_timer() as time_tqdm: for i in t:
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 += i 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()) assert_performance(3, 'trange', time_tqdm(), 'range', time_bench())
@retry_on_except() @retry_on_except()
def test_manual_basic_overhead(): def test_manual_basic_overhead(capsys):
"""Test overhead of manual tqdm""" """Test overhead of manual tqdm"""
total = int(1e6) total = int(1e6)
with closing(MockIO()) as our_file: with tqdm(total=total * 10, leave=True) as t:
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)
a = 0 a = 0
with relative_timer() as time_bench: with relative_timer() as time_tqdm:
for i in _range(total): for i in _range(total):
a += i 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()) assert_performance(5, 'tqdm', time_tqdm(), 'range', time_bench())
def worker(total, blocking=True): def worker(total, blocking=True):
def incr_bar(x): def incr_bar(x):
with closing(StringIO()) as our_file: for _ in trange(total, lock_args=None if blocking else (False,),
for _ in trange(total, file=our_file, lock_args=None if blocking else miniters=1, mininterval=0, maxinterval=0):
(False,), miniters=1, mininterval=0, maxinterval=0): pass
pass
return x + 1 return x + 1
return incr_bar return incr_bar
@retry_on_except() @retry_on_except()
@patch_lock(thread=True) @patch_lock(thread=True)
def test_lock_args(): def test_lock_args(capsys):
"""Test overhead of nonblocking threads""" """Test overhead of nonblocking threads"""
ThreadPoolExecutor = importorskip('concurrent.futures').ThreadPoolExecutor ThreadPoolExecutor = importorskip('concurrent.futures').ThreadPoolExecutor
@ -246,99 +234,91 @@ def test_lock_args():
@retry_on_except(10) @retry_on_except(10)
def test_iter_overhead_hard(): def test_iter_overhead_hard(capsys):
"""Test overhead of iteration based tqdm (hard)""" """Test overhead of iteration based tqdm (hard)"""
total = int(1e5) total = int(1e5)
with closing(MockIO()) as our_file: a = 0
a = 0 with trange(total, leave=True, miniters=1,
with trange(total, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t:
mininterval=0, maxinterval=0) as t: with relative_timer() as time_tqdm:
with relative_timer() as time_tqdm: for i in t:
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 += i 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()) assert_performance(130, 'trange', time_tqdm(), 'range', time_bench())
@retry_on_except(10) @retry_on_except(10)
def test_manual_overhead_hard(): def test_manual_overhead_hard(capsys):
"""Test overhead of manual tqdm (hard)""" """Test overhead of manual tqdm (hard)"""
total = int(1e5) total = int(1e5)
with closing(MockIO()) as our_file: with tqdm(total=total * 10, leave=True, miniters=1,
with tqdm(total=total * 10, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t:
mininterval=0, maxinterval=0) as t:
a = 0
with relative_timer() as time_tqdm:
for i in _range(total):
a += i
t.update(10)
a = 0 a = 0
with relative_timer() as time_bench: with relative_timer() as time_tqdm:
for i in _range(total): for i in _range(total):
a += i 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()) assert_performance(130, 'tqdm', time_tqdm(), 'range', time_bench())
@retry_on_except(10) @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)""" """Test overhead of iteration based tqdm vs simple progress bar (hard)"""
total = int(1e4) total = int(1e4)
with closing(MockIO()) as our_file: a = 0
a = 0 with trange(total, leave=True, miniters=1,
with trange(total, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t:
mininterval=0, maxinterval=0) as t: with relative_timer() as time_tqdm:
with relative_timer() as time_tqdm: for i in t:
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 += i 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()) assert_performance(10, 'trange', time_tqdm(), 'simple_progress', time_bench())
@retry_on_except(10) @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)""" """Test overhead of manual tqdm vs simple progress bar (hard)"""
total = int(1e4) total = int(1e4)
with closing(MockIO()) as our_file: with tqdm(total=total * 10, leave=True, miniters=1,
with tqdm(total=total * 10, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t:
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)
a = 0 a = 0
with relative_timer() as time_bench: with relative_timer() as time_tqdm:
for i in _range(total): for i in _range(total):
a += i 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()) assert_performance(10, 'tqdm', time_tqdm(), 'simple_progress', time_bench())