FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
TimingLogger.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Util;
20 
21 use Monolog\Logger;
22 
24 {
25  const DEFAULT_WATCH = 'default';
26 
28  private $logger;
29 
31  private $watchTimes;
32 
33  private $startTime;
34 
35  public function __construct(Logger $logger)
36  {
37  $this->logger = $logger;
38 
39  $this->startTime = $this->getTimestamp();
40  $this->watchTimes = array(self::DEFAULT_WATCH => $this->startTime);
41  }
42 
48  public function tic($watch = self::DEFAULT_WATCH)
49  {
50  $this->watchTimes[$watch] = $this->getTimestamp();
51  }
52 
57  public function toc($text, $watch = self::DEFAULT_WATCH)
58  {
59  if (! array_key_exists($watch, $this->watchTimes)) {
60  $watch = self::DEFAULT_WATCH;
61  $text .= " using watch '$watch'";
62  } else if (empty($text)) {
63  $text = "Using watch '$watch'";
64  }
65  $this->logWithStartAndEndTime($text, $this->watchTimes[$watch], $this->getTimestamp());
66  }
67 
72  public function logWithStartTime($text, $startTime)
73  {
74  $endTime = $this->getTimestamp();
75  $this->logWithStartAndEndTime($text, $startTime, $endTime);
76  }
77 
83  public function logWithStartAndEndTime($text, $startTime, $endTime)
84  {
85  $this->logger->debug(sprintf("%s (%.3fms)", $text, ($endTime - $startTime) * 1000));
86  $this->startTime = $endTime;
87  }
88 
89  protected function getTimestamp()
90  {
91  return microtime(true);
92  }
93 }
logWithStartTime($text, $startTime)
tic($watch=self::DEFAULT_WATCH)
start stopwatch timer
logWithStartAndEndTime($text, $startTime, $endTime)
toc($text, $watch=self::DEFAULT_WATCH)