Skip to content

Commit

Permalink
Improve progress bar
Browse files Browse the repository at this point in the history
Show the number of the current benchmark and fix the number
displayed in the progress bar (show the number before and
not after the benchmark).
  • Loading branch information
Johannes Bechberger committed Aug 28, 2020
1 parent 66ee5fe commit 263db13
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 30 deletions.
59 changes: 42 additions & 17 deletions temci/run/run_processor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import copy
import random
import traceback
from pprint import pprint

from temci.build.build_processor import BuildProcessor
from temci.build.builder import Builder, BuildError
Expand Down Expand Up @@ -155,7 +154,6 @@ def _can_run_next_block(self) -> bool:
to_bench_count))
return False
if self.block_run_count >= self.maximum_of_max_runs() and self.block_run_count >= self.maximum_of_min_runs():
#print("benchmarked too often, block run count ", self.block_run_count, self.block_run_count + self.run_block_size > self.min_runs)
logging.warning("Benchmarked program blocks too often and aborted therefore now.")
return False
return True
Expand Down Expand Up @@ -209,48 +207,75 @@ def benchmark(self):
start_label = discard_label if self.discarded_runs > 0 else label
label_format = "{:32s}"
if show_progress:
run_count = [0]

def bench(run: int) -> bool:
if run < self.discarded_runs:
runs.label = label_format.format(discard_label)
self._benchmarking_block_run(block_size=1, discard=True)
else:
if self._finished() or all(b.max_runs < run_count[0] for b in self.run_blocks):
recorded_run = run - self.discarded_runs
if self._finished() or all(b.max_runs < recorded_run for b in self.run_blocks):
return False
self._benchmarking_block_run(run=run_count[0])
run_count[0] += 1
if run == self.discarded_runs - 1:
runs.label = label_format.format(label)
self._benchmarking_block_run(run=recorded_run)
return True

import click
with click.progressbar(range(0, self.max_runs + self.discarded_runs),
label=label_format.format(start_label),
file=None if self.pool.run_driver.runs_benchmarks else "-") as runs:
discard_label = "Discarded benchmark {{}} out of {}".format(self.discarded_runs)
if self.fixed_runs:
label = "Benchmark {{}} out of {}".format(self.max_runs)
else:
label = "Benchmark {{}} out of {} to {}".format(self.min_runs, self.max_runs)

def alter_label(run: int):
if run < self.discarded_runs:
runs.label = label_format.format(discard_label.format(run + 1))
else:
runs.label = label_format.format(label.format(run - self.discarded_runs + 1))

runs.short_limit = 0
every = Settings()["run/watch_every"]
if Settings()["run/watch"]:
with Screen(scroll=True, keep_first_lines=1) as f:
sys.stdout = f
sys.stderr = f
runs.file = f if self.pool.run_driver.runs_benchmarks else "-"
import click._termui_impl
click._termui_impl.BEFORE_BAR = "\r"
click._termui_impl.AFTER_BAR = "\n"
for run in runs:
runs.file = f if self.pool.run_driver.runs_benchmarks else "-"
runs._last_line = ""

def render():
f.reset()
runs._last_line = ""
runs.render_progress()
f.advance_line()
print(ReporterRegistry.get_for_name("console", self.stats_helper).report(
with_tester_results=False, to_string=True), file=f)
if run % every == 0:
f.copy_over()
f.flush2()

for run in runs:
alter_label(run)
f.enable()
render()
if run % every == 0 or True:
f.display()
f.reset()
if not bench(run):
break
f.disable()
runs.finish()
render()
f.display()

else:
alter_label(0)
for run in runs:
alter_label(run)
runs._last_line = ""
runs.render_progress()
if not bench(run):
break
runs.finish()
runs.render_progress()
else:
time_per_run = self._make_discarded_runs()
last_round_time = time.time()
Expand All @@ -267,7 +292,7 @@ def bench(run: int) -> bool:
and self.show_report:
self.print_report()
raise
if self.show_report:
if self.show_report and not Settings()["run/watch"]:
self.print_report()
self.store_and_teardown()

Expand Down
48 changes: 35 additions & 13 deletions temci/utils/curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, scroll: bool = False, keep_first_lines: int = 0, print_buffer
self.x = 0
self.y = self.keep_first_lines
self.print_buffer_on_exit = print_buffer_on_exit
self.enabled = True
if scroll:
t = threading.Thread(target=self._create_scroll_thread())
t.setDaemon(True)
Expand All @@ -42,34 +43,40 @@ def func():
curses.flushinp()
updated = True
if c == curses.KEY_LEFT:
self.move_cursor(x_offset=-1)
self._move_cursor(x_offset=-1)
elif c == curses.KEY_RIGHT:
self.move_cursor(x_offset=1)
self._move_cursor(x_offset=1)
elif c == curses.KEY_UP or c == curses.KEY_SR:
self.move_cursor(y_offset=-1)
self._move_cursor(y_offset=-1)
elif c == curses.KEY_DOWN or c == curses.KEY_SF:
self.move_cursor(y_offset=1)
self._move_cursor(y_offset=1)
else:
updated = False
time.sleep(0.05)
if updated:
self.flush2()
self._flush2()
time.sleep(0.05)
return func

def __enter__(self):
return self

def copy_over(self):
def _copy_over(self):
self._shown_buffer = self.buffer

def reset(self):
"""
Clear the current screen recording (does not change the displayed screen)
"""
self.current_line = 0
self.buffer = [""]

def isatty(self):
""" Only required for click """
return True

def write(self, text: str):
if not self.enabled:
pass
text = text.replace("\n", "\r\n")
for line in text.splitlines(keepends=True):
first = True
Expand All @@ -95,29 +102,38 @@ def advance_line(self):
def flush(self):
pass

def flush2(self):
def display(self):
"""
Replace the current screen with the recorded. Call reset() if you don't want to add to this screen.
"""
self._copy_over()
self._flush2()


def _flush2(self):
""" Refreshes the screen """
self.scr.refresh()
max_y, max_x = self.scr.getmaxyx()
for y in range(0, min(max_y, self.keep_first_lines, len(self._shown_buffer))):
self.scr.addstr(0, y, self._shown_buffer[y][0:max_x].replace("\\[", ""))
end = max(0, min(len(self._shown_buffer) - 1, max_y - self.keep_first_lines + self.y))
for y in range(self.y, end):
self.scr.addstr(y + self.keep_first_lines - self.y, 0, " " * max_x)
self.scr.addstr(y + self.keep_first_lines - self.y, 0, self._shown_buffer[y][self.x:min(len(self._shown_buffer[y]) - self.x, max_x) + self.x])
self.scr.addstr(y + self.keep_first_lines - self.y, 0,
self._shown_buffer[y][self.x:min(len(self._shown_buffer[y]) - self.x, max_x) + self.x])
for y in range(end + self.keep_first_lines - self.y, max_y):
try:
self.scr.addstr(y, 0, " " * max_x)
except:
pass
self.scr.refresh()

def move_cursor(self, x_offset: int = 0, y_offset: int = 0):
def _move_cursor(self, x_offset: int = 0, y_offset: int = 0):
self.x = max(0, self.x + x_offset)
self.y = max(self.keep_first_lines, self.y + y_offset)

def writelines(self, lines: List[str]):
for line in lines:
self.write(line.replace("\n", "\r\n"))
self.write(line)

def __exit__(self, exc_type, exc_val, exc_tb):
try:
Expand All @@ -129,4 +145,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):
except BaseException as ex:
pass
if self.print_buffer_on_exit:
sys.stdout.writelines([s + "\n" for s in self._shown_buffer])
sys.stdout.writelines([s + "\n" for s in self._shown_buffer])

def enable(self):
self.enabled = True

def disable(self):
self.enabled = False

0 comments on commit 263db13

Please sign in to comment.