Skip to content

Commit 5e18784

Browse files
Switch to timer class and time actions separately
1 parent c75d0bf commit 5e18784

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

src/Runner/Hook.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public function shouldSkipActions(?bool $shouldSkip = null): bool
249249
private function executeActions(array $actions): void
250250
{
251251
$status = self::HOOK_SUCCEEDED;
252-
$start = microtime(true);
252+
$timer = Timer::createAndStart();
253253
try {
254254
if ($this->config->failOnFirstError()) {
255255
$this->executeFailOnFirstError($actions);
@@ -262,8 +262,7 @@ private function executeActions(array $actions): void
262262
$this->dispatcher->dispatch('onHookFailure');
263263
throw $e;
264264
} finally {
265-
$duration = microtime(true) - $start;
266-
$this->printer->hookEnded($status, $this->hookLog, $duration);
265+
$this->printer->hookEnded($status, $this->hookLog, $timer->stop());
267266
}
268267
}
269268

@@ -319,6 +318,7 @@ private function handleAction(Config\Action $action): void
319318

320319
$io = new IO\CollectorIO($this->io);
321320
$status = ActionLog::ACTION_SUCCEEDED;
321+
$timer = Timer::createAndStart();
322322

323323
try {
324324
if (!$this->doConditionsApply($action->getConditions(), $io)) {
@@ -336,13 +336,13 @@ private function handleAction(Config\Action $action): void
336336

337337
$runner = $this->createActionRunner(Util::getExecType($action->getAction()));
338338
$runner->execute($this->config, $io, $this->repository, $action);
339-
$this->printer->actionSucceeded($action);
339+
$this->printer->actionSucceeded($action, $timer->stop());
340340
} catch (ActionNotApplicable $e) {
341341
$this->printer->actionSkipped($action);
342342
return;
343343
} catch (Exception $e) {
344344
$status = ActionLog::ACTION_FAILED;
345-
$this->printer->actionFailed($action);
345+
$this->printer->actionFailed($action, $timer->stop());
346346
if (!$action->isFailureAllowed($this->config->isFailureAllowed())) {
347347
throw $e;
348348
}

src/Runner/Hook/Printer.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ public function __construct(IO $io)
6262
/**
6363
* Prints the action success line
6464
*
65-
* @param \CaptainHook\App\Config\Action $action
65+
* @param \CaptainHook\App\Config\Action $action
66+
* @param float $duration
6667
* @return void
6768
*/
68-
public function actionSucceeded(Action $action): void
69+
public function actionSucceeded(Action $action, float $duration): void
6970
{
70-
$this->io->write($this->actionHeadline($action) . '<info>done</info>');
71+
$time = $this->verbosity === IO::VERBOSE ? sprintf(' (%01.2fs)', $duration) : '';
72+
$this->io->write($this->actionHeadline($action) . '<info>done</info>' . $time);
7173
}
7274

7375
/**
@@ -84,12 +86,14 @@ public function actionSkipped(Action $action): void
8486
/**
8587
* Prints the action failed line
8688
*
87-
* @param \CaptainHook\App\Config\Action $action
89+
* @param \CaptainHook\App\Config\Action $action
90+
* @param float $duration
8891
* @return void
8992
*/
90-
public function actionFailed(Action $action): void
93+
public function actionFailed(Action $action, float $duration): void
9194
{
92-
$this->io->write($this->actionHeadline($action) . '<fg=red>failed</>');
95+
$time = $this->verbosity === IO::VERBOSE ? sprintf(' (%01.2fs)', $duration) : '';
96+
$this->io->write($this->actionHeadline($action) . '<fg=red>failed</>' . $time);
9397
}
9498

9599
/**

0 commit comments

Comments
 (0)