Skip to content

Commit d988767

Browse files
committed
Improve hook duration timing
1 parent ac42dc5 commit d988767

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

pre_commit/commands/run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def _run_single_hook(
187187

188188
if not hook.pass_filenames:
189189
filenames = ()
190-
time_before = time.time()
190+
time_before = time.monotonic()
191191
language = languages[hook.language]
192192
with language.in_env(hook.prefix, hook.language_version):
193193
retcode, out = language.run_hook(
@@ -199,7 +199,7 @@ def _run_single_hook(
199199
require_serial=hook.require_serial,
200200
color=use_color,
201201
)
202-
duration = round(time.time() - time_before, 2) or 0
202+
duration = round(time.monotonic() - time_before, 2) or 0
203203
diff_after = _get_diff()
204204

205205
# if the hook makes changes, fail the commit

tests/commands/run_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def test_verbose_duration(cap_out, store, in_git_dir, t1, t2, expected):
293293
write_config('.', {'repo': 'meta', 'hooks': [{'id': 'identity'}]})
294294
cmd_output('git', 'add', '.')
295295
opts = run_opts(verbose=True)
296-
with mock.patch.object(time, 'time', side_effect=(t1, t2)):
296+
with mock.patch.object(time, 'monotonic', side_effect=(t1, t2)):
297297
ret, printed = _do_run(cap_out, store, str(in_git_dir), opts)
298298
assert ret == 0
299299
assert expected in printed

tests/commands/try_repo_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _run_try_repo(tempdir_factory, **kwargs):
4343

4444

4545
def test_try_repo_repo_only(cap_out, tempdir_factory):
46-
with mock.patch.object(time, 'time', return_value=0.0):
46+
with mock.patch.object(time, 'monotonic', return_value=0.0):
4747
_run_try_repo(tempdir_factory, verbose=True)
4848
start, config, rest = _get_out(cap_out)
4949
assert start == ''

0 commit comments

Comments
 (0)