def test_manual_overhead_simplebar_hard(): """Test overhead of manual bars vs simple progress bar (hard)""" total = int(1e4) with closing(MockIO()) as our_file: t = bars(total=total * 10, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) a = 0 with relative_timer() as time_bars: 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 with relative_timer() as time_bench: for i in _range(total): a += i simplebar_update(10) assert_performance(5, 'bars', time_bars(), 'simple_progress', time_bench())
def test_manual_overhead_hard(): """Test overhead of manual bars (hard)""" total = int(1e5) with closing(MockIO()) as our_file: t = bars(total=total * 10, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) a = 0 with relative_timer() as time_bars: for i in _range(total): a += i t.update(10) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(("%i" % a) * 40) assert_performance(85, 'bars', time_bars(), 'range', time_bench())
def test_main(): """Test command line pipes""" ls_out = _sh('ls').replace('\r\n', '\n') ls = subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = _sh(sys.executable, '-c', 'from bars.cli import main; main()', stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test: assert ls_out in res.replace('\r\n', '\n') # semi-fake test which gets coverage: _SYS = sys.stdin, sys.argv with closing(StringIO()) as sys.stdin: sys.argv = [ '', '--desc', 'Test CLI --delim', '--ascii', 'True', '--delim', r'\0', '--buf_size', '64' ] sys.stdin.write('\0'.join(map(str, _range(int(123))))) # sys.stdin.write(b'\xff') # TODO sys.stdin.seek(0) main() sys.stdin = IN_DATA_LIST sys.argv = [ '', '--desc', 'Test CLI pipes', '--ascii', 'True', '--unit_scale', 'True' ] import bars.__main__ # NOQA with closing(StringIO()) as sys.stdin: IN_DATA = '\0'.join(IN_DATA_LIST) sys.stdin.write(IN_DATA) sys.stdin.seek(0) sys.argv = ['', '--ascii', '--bytes=True', '--unit_scale', 'False'] with closing(UnicodeIO()) as fp: main(fp=fp) assert str(len(IN_DATA)) in fp.getvalue() sys.stdin = IN_DATA_LIST # test --log with closing(StringIO()) as sys.stdin: sys.stdin.write('\0'.join(map(str, _range(int(123))))) sys.stdin.seek(0) # with closing(UnicodeIO()) as fp: main(argv=['--log', 'DEBUG'], fp=NULL) # assert "DEBUG:" in sys.stdout.getvalue() sys.stdin = IN_DATA_LIST # clean up sys.stdin, sys.argv = _SYS
def test_iter_overhead_simplebar_hard(): """Test overhead of iteration based bars vs simple progress bar (hard)""" total = int(1e4) with closing(MockIO()) as our_file: a = 0 with brange(total, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t: with relative_timer() as time_bars: 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 assert_performance(5, 'brange', time_bars(), 'simple_progress', time_bench())
def test_iter_overhead_hard(): """Test overhead of iteration based bars (hard)""" total = int(1e5) with closing(MockIO()) as our_file: a = 0 with brange(total, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t: with relative_timer() as time_bars: 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 our_file.write(("%i" % a) * 40) assert_performance(85, 'brange', time_bars(), 'range', time_bench())
def test_manual_overhead(): """Test overhead of manual bars""" total = int(1e6) with closing(MockIO()) as our_file: with bars(total=total * 10, file=our_file, leave=True) as t: a = 0 with relative_timer() as time_bars: for i in _range(total): a += i t.update(10) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(a) assert_performance(6, 'bars', time_bars(), 'range', time_bench())
def test_iter_overhead(): """Test overhead of iteration based bars""" total = int(1e6) with closing(MockIO()) as our_file: a = 0 with brange(total, file=our_file) as t: with relative_timer() as time_bars: 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 our_file.write(a) assert_performance(6, 'brange', time_bars(), 'range', time_bench())
def _sh(*cmd, **kwargs): return subprocess.Popen(cmd, stdout=subprocess.PIPE, **kwargs).communicate()[0].decode('utf-8') class Null(object): def __call__(self, *_, **__): return self def __getattr__(self, _): return self IN_DATA_LIST = map(str, _range(int(123))) NULL = Null() # WARNING: this should be the last test as it messes with sys.stdin, argv @with_setup(pretest, posttest) def test_main(): """Test command line pipes""" ls_out = _sh('ls').replace('\r\n', '\n') ls = subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = _sh(sys.executable, '-c', 'from bars.cli import main; main()', stdin=ls.stdout,