FOSSology  4.4.0
Open Source License Compliance by Open Source Software
TimingLogger.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 
9 namespace Fossology\Lib\Util;
10 
11 use Monolog\Logger;
12 
14 {
15  const DEFAULT_WATCH = 'default';
16 
18  private $logger;
19 
21  private $watchTimes;
22 
23  private $startTime;
24 
25  public function __construct(Logger $logger)
26  {
27  $this->logger = $logger;
28 
29  $this->startTime = $this->getTimestamp();
30  $this->watchTimes = array(self::DEFAULT_WATCH => $this->startTime);
31  }
32 
38  public function tic($watch = self::DEFAULT_WATCH)
39  {
40  $this->watchTimes[$watch] = $this->getTimestamp();
41  }
42 
47  public function toc($text, $watch = self::DEFAULT_WATCH)
48  {
49  if (! array_key_exists($watch, $this->watchTimes)) {
50  $watch = self::DEFAULT_WATCH;
51  $text .= " using watch '$watch'";
52  } else if (empty($text)) {
53  $text = "Using watch '$watch'";
54  }
55  $this->logWithStartAndEndTime($text, $this->watchTimes[$watch], $this->getTimestamp());
56  }
57 
62  public function logWithStartTime($text, $startTime)
63  {
64  $endTime = $this->getTimestamp();
65  $this->logWithStartAndEndTime($text, $startTime, $endTime);
66  }
67 
73  public function logWithStartAndEndTime($text, $startTime, $endTime)
74  {
75  $this->logger->debug(sprintf("%s (%.3fms)", $text, ($endTime - $startTime) * 1000));
76  $this->startTime = $endTime;
77  }
78 
79  protected function getTimestamp()
80  {
81  return microtime(true);
82  }
83 }
tic($watch=self::DEFAULT_WATCH)
start stopwatch timer
toc($text, $watch=self::DEFAULT_WATCH)
logWithStartTime($text, $startTime)
logWithStartAndEndTime($text, $startTime, $endTime)