From dea9d877ff5e61274be85e33d1ee7afefbe4788b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 10 Oct 2026 08:58:31 +0530 Subject: [PATCH] Add a tab bar benchmark to benchmark.py Times laying out and drawing the horizontal tab bar for every built-in style with 10, 50 and 200 tabs. Use --compare-with to also time kitty/tab_bar.py as it is at some git revision. --- benchmark.py | 107 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 6 deletions(-) diff --git a/benchmark.py b/benchmark.py index 51a98c2a0..6c500051d 100755 --- a/benchmark.py +++ b/benchmark.py @@ -7,20 +7,29 @@ import os import select import shutil import signal +import statistics import struct import subprocess import sys import tempfile import termios import time +from collections.abc import Iterator +from contextlib import ExitStack from pty import CHILD, fork +from types import ModuleType +from unittest.mock import patch from kitty.constants import kitten_exe, kitty_exe -from kitty.fast_data_types import ChildMonitor, Screen, safe_pipe +from kitty.fast_data_types import ChildMonitor, Region, Screen, safe_pipe, set_options from kitty.utils import read_screen_size BENCHMARK_WINDOW_ID = 1 -ALL_BENCHMARKS = ('ascii', 'unicode', 'unique_unicode', 'csi', 'images', 'long_escape_codes') +PARSING_BENCHMARKS = ('ascii', 'unicode', 'unique_unicode', 'csi', 'images', 'long_escape_codes') +ALL_BENCHMARKS = PARSING_BENCHMARKS + ('tab_bar',) +TAB_BAR_STYLES = ('fade', 'separator', 'powerline', 'slant') +TAB_BAR_NUM_TABS = (10, 50, 200) +TAB_BAR_COLUMNS = 200 def perf_output() -> str: @@ -81,7 +90,7 @@ def run_perf_reports(perf_exe: str) -> None: def run_parsing_benchmark( - benchmarks: tuple[str, ...] = ALL_BENCHMARKS, + benchmarks: tuple[str, ...] = PARSING_BENCHMARKS, with_scrollback: bool = True, cell_width: int = 10, cell_height: int = 20, @@ -153,6 +162,83 @@ def run_parsing_benchmark( sys.stdout.write(str(screen.linebuf)) +def tab_bar_module_at(rev: str) -> ModuleType: + "Load kitty/tab_bar.py as it is at the specified git revision" + src = subprocess.check_output(['git', 'show', f'{rev}:kitty/tab_bar.py'], text=True, cwd=os.path.dirname(os.path.abspath(__file__))) + m = ModuleType(f'kitty.tab_bar_at_{rev}') + m.__package__ = 'kitty' + exec(compile(src, f'{rev}:kitty/tab_bar.py', 'exec'), m.__dict__) + return m + + +class TabBarBenchmarkBoss: + class mappings: + current_keyboard_mode_name = '' + + window_id_map: dict[int, object] = {} + + def tab_for_id(self, tab_id: int) -> None: + return None + + +def time_tab_bar_updates(tab_bar: ModuleType, style: str, num_tabs: int, repetitions: int | None) -> float: + "Return the median time in seconds taken to lay out and draw a horizontal tab bar" + from kitty.options.types import defaults + + set_options(defaults._replace(tab_bar_style=style)) + cell_width, cell_height = 10, 20 + width, height = TAB_BAR_COLUMNS * cell_width, 200 + central = Region((0, 0, width, height - cell_height, width, height - cell_height)) + bar = Region((0, height - cell_height, width, height, width, cell_height)) + boss = TabBarBenchmarkBoss() + with ExitStack() as stack: + # There is no OS window, so replace the functions that need one with + # plain functions, rather than mocks, whose overhead would distort timings + for name, func in { + 'cell_size_for_window': lambda *a: (cell_width, cell_height), + 'viewport_for_window': lambda *a: (central, bar, width, height, cell_width, cell_height), + 'set_tab_bar_render_data': lambda *a: None, + 'update_tab_bar_edge_colors': lambda *a: None, + 'get_boss': lambda: boss, + }.items(): + stack.enter_context(patch.object(tab_bar, name, new=func)) + tb = tab_bar.TabBar(1) + tb.layout() + # A mix of short and long titles, so that the long ones need truncating + data = tuple(tab_bar.TabBarData(title='~' if i % 3 == 0 else f'~/projects/some-project-{i}', tab_id=i + 1, is_active=i == 1) for i in range(num_tabs)) + for _ in range(5): + tb.update(data) + times = [] + for _ in range(repetitions or max(20, 4000 // num_tabs)): + start = time.perf_counter() + tb.update(data) + times.append(time.perf_counter() - start) + return statistics.median(times) + + +def run_tab_bar_benchmark(compare_with: str = '', repetitions: int | None = None) -> None: + import kitty.tab_bar + + modules = {'working tree': kitty.tab_bar} + if compare_with: + try: + modules = {compare_with: tab_bar_module_at(compare_with), **modules} + except subprocess.CalledProcessError: + raise SystemExit(f'Could not read kitty/tab_bar.py at the git revision: {compare_with}') + width = max(12, *map(len, modules)) + + def rows() -> Iterator[str]: + yield f'{"style":<10} {"tabs":>4} ' + ' '.join(f'{name:>{width}}' for name in modules) + for style in TAB_BAR_STYLES: + for num_tabs in TAB_BAR_NUM_TABS: + times = (time_tab_bar_updates(m, style, num_tabs, repetitions) * 1000 for m in modules.values()) + yield f'{style:<10} {num_tabs:>4} ' + ' '.join(f'{t:>{width - 2}.3f}ms' for t in times) + + print(f'Median time to update a horizontal tab bar of {TAB_BAR_COLUMNS} cells:') + for row in rows(): + print(row, flush=True) + + def exec_under_perf(perf_exe: str, print_report: bool = False) -> None: """Re-exec this script as a child of perf record. @@ -186,7 +272,7 @@ def exec_under_perf(perf_exe: str, print_report: bool = False) -> None: def main() -> None: - p = argparse.ArgumentParser(description='Run kitty parsing benchmarks') + p = argparse.ArgumentParser(description='Run kitty parsing and tab bar benchmarks') p.add_argument( 'benchmarks', nargs='*', @@ -222,7 +308,13 @@ def main() -> None: type=int, default=None, metavar='N', - help='Number of repetitions of each benchmark (default: kitten default of 100)', + help='Number of repetitions of each benchmark (default: kitten default of 100, for tab_bar it depends on the number of tabs)', + ) + p.add_argument( + '--compare-with', + default='', + metavar='GIT_REVISION', + help='For the tab_bar benchmark, also time kitty/tab_bar.py as it is at the specified git revision, for comparison', ) args = p.parse_args() @@ -235,7 +327,10 @@ def main() -> None: return benchmarks = tuple(args.benchmarks) if args.benchmarks else ALL_BENCHMARKS - run_parsing_benchmark(benchmarks=benchmarks, with_scrollback=args.with_scrollback, repetitions=args.repetitions) + if parsing_benchmarks := tuple(b for b in benchmarks if b in PARSING_BENCHMARKS): + run_parsing_benchmark(benchmarks=parsing_benchmarks, with_scrollback=args.with_scrollback, repetitions=args.repetitions) + if 'tab_bar' in benchmarks: + run_tab_bar_benchmark(compare_with=args.compare_with, repetitions=args.repetitions) if __name__ == '__main__':