Skip to content

Commit 9cff6f6

Browse files
authored
Count time spent in package installation (#147)
1 parent d664105 commit 9cff6f6

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

src/kup/__main__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import subprocess
88
import sys
99
import textwrap
10+
import time
1011
from argparse import ArgumentParser, RawDescriptionHelpFormatter, _HelpAction
1112
from typing import TYPE_CHECKING
1213

@@ -318,6 +319,16 @@ def highlight_row(condition: bool, xs: list[str]) -> list[str]:
318319
return xs
319320

320321

322+
def _format_duration(seconds: float) -> str:
323+
if seconds < 60:
324+
return f'{seconds}s'
325+
minutes, secs = divmod(seconds, 60)
326+
if minutes < 60:
327+
return f'{int(minutes)}m {secs:.0f}s'
328+
hours, mins = divmod(minutes, 60)
329+
return f'{int(hours)}h {int(mins)}m'
330+
331+
321332
def list_package(
322333
package_name: str,
323334
show_inputs: bool,
@@ -496,6 +507,7 @@ def install_package(
496507
'has_overrides': len(package_overrides) > 0 if package_overrides else False,
497508
},
498509
)
510+
install_start_time = time.monotonic()
499511

500512
if not overrides and package.uri in pinned_package_cache:
501513
rich.print(f" ⌛ Fetching cached version of '[green]{package_name.pretty_name}[/]' ...")
@@ -541,19 +553,22 @@ def install_package(
541553
display_version = None
542554
display_version = f' ({display_version})' if display_version is not None else ''
543555

556+
duration_seconds = round(time.monotonic() - install_start_time, 1)
544557
emit_event(
545558
'kup_install_complete',
546559
{
547560
'package': package_name.base,
548561
'version': package_version or 'latest',
549562
'was_update': verb == 'updated',
550563
'from_cache': package.uri in pinned_package_cache and not overrides,
564+
'duration_seconds': duration_seconds,
551565
},
552566
)
553567

554568
rich.print(
555569
f" ✅ Successfully {verb} '[green]{package_name.base}[/]' version [blue]{package.uri}{display_version}[/]."
556570
)
571+
rich.print(f' ⏱️ Elapsed time [green]{_format_duration(duration_seconds)}[/].')
557572

558573

559574
def uninstall_package(package_name: str) -> None:

0 commit comments

Comments
 (0)