Skip to content

Commit

Permalink
Improve hook duration timing
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz committed Oct 13, 2023
1 parent ac42dc5 commit b304cd3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions pre_commit/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def _run_single_hook(

if not hook.pass_filenames:
filenames = ()
time_before = time.time()
time_before = time.monotonic()
language = languages[hook.language]
with language.in_env(hook.prefix, hook.language_version):
retcode, out = language.run_hook(
Expand All @@ -199,7 +199,7 @@ def _run_single_hook(
require_serial=hook.require_serial,
color=use_color,
)
duration = round(time.time() - time_before, 2) or 0
duration = round(time.monotonic() - time_before, 2)
diff_after = _get_diff()

# if the hook makes changes, fail the commit
Expand All @@ -218,7 +218,7 @@ def _run_single_hook(
_subtle_line(f'- hook id: {hook.id}', use_color)

if (verbose or hook.verbose) and duration is not None:
_subtle_line(f'- duration: {duration}s', use_color)
_subtle_line(f'- duration: {duration:.2f}s', use_color)

if retcode:
_subtle_line(f'- exit code: {retcode}', use_color)
Expand Down
4 changes: 2 additions & 2 deletions tests/commands/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,14 @@ def test_global_files(cap_out, store, in_git_dir):
('t1', 't2', 'expected'),
(
(1.234, 2., b'\n- duration: 0.77s\n'),
(1., 1., b'\n- duration: 0s\n'),
(1., 1., b'\n- duration: 0.00s\n'),
),
)
def test_verbose_duration(cap_out, store, in_git_dir, t1, t2, expected):
write_config('.', {'repo': 'meta', 'hooks': [{'id': 'identity'}]})
cmd_output('git', 'add', '.')
opts = run_opts(verbose=True)
with mock.patch.object(time, 'time', side_effect=(t1, t2)):
with mock.patch.object(time, 'monotonic', side_effect=(t1, t2)):
ret, printed = _do_run(cap_out, store, str(in_git_dir), opts)
assert ret == 0
assert expected in printed
Expand Down
4 changes: 2 additions & 2 deletions tests/commands/try_repo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _run_try_repo(tempdir_factory, **kwargs):


def test_try_repo_repo_only(cap_out, tempdir_factory):
with mock.patch.object(time, 'time', return_value=0.0):
with mock.patch.object(time, 'monotonic', return_value=0.0):
_run_try_repo(tempdir_factory, verbose=True)
start, config, rest = _get_out(cap_out)
assert start == ''
Expand All @@ -62,7 +62,7 @@ def test_try_repo_repo_only(cap_out, tempdir_factory):
- hook id: bash_hook
Bash hook................................................................Passed
- hook id: bash_hook2
- duration: 0s
- duration: 0.00s
test-file
Expand Down

0 comments on commit b304cd3

Please sign in to comment.