Subversion Repositories Web Services

Rev

Rev 118259 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
82190 hhl 1
<?php
2
/**
3
 *
4
 * This file is part of Open Library System.
5
 * Copyright © 2009, Dansk Bibliotekscenter a/s,
6
 * Tempovej 7-11, DK-2750 Ballerup, Denmark. CVR: 15149043
7
 *
8
 * Open Library System is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Open Library System is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with Open Library System.  If not, see <http://www.gnu.org/licenses/>.
118246 fvs 20
 */
82190 hhl 21
 
22
 
23
/** \brief Stopwatch for code timing
118246 fvs 24
 *
25
 * $watch = new stopwatch();
26
 * $watch->format('perl');
27
 *
28
 * $watch->start('a');
29
 * $watch->start('w');
30
 *
31
 * $watch->stop('a');
32
 * #$watch->start('a');
33
 *
34
 * echo $watch->dump();
35
 * echo "...\n";
36
 * $watch->format('screen');
37
 * echo $watch->dump();
38
 * echo "...\n";
39
 *
40
 * $watch->log("foo.log");
41
 *
42
 */
82190 hhl 43
class stopwatch {
118246 fvs 44
  var $timers;        ///< Currently running timers
45
  var $sums;        ///< Sums of completed timers
46
  var $prefix;        ///< Prefix of Output
47
  var $delim;        ///< Delimitor of Output
48
  var $postfix;        ///< Postfix of Output
49
  var $format;        ///< Format of Output
82190 hhl 50
 
51
  /**
118246 fvs 52
   * \brief stopwatch constructor
53
   * @param mixed $prefix Output prefix
54
   * @param mixed $delim Output delimitor
55
   * @param mixed $postfix Output postfix remember newline
56
   * @param mixed $format Output format ("%s => %01.6f")
82190 hhl 57
   *************/
100106 fvs 58
  function __construct($prefix = null, $delim = null, $postfix = null, $format = null) {
118246 fvs 59
    $this->prefix = $prefix;
60
    $this->delim = $delim;
82190 hhl 61
    $this->postfix = $postfix;
118246 fvs 62
    $this->format = $format;
118259 fvs 63
    $this->timers = array();
64
    $this->sums = array();
82190 hhl 65
    $this->start('Total');
66
  }
67
 
68
  /**
118246 fvs 69
   *  \brief start a timer
70
   * @param string $s - Name of timer to start
71
   * @param boolean $ignore - Ignore already started timer (default true)
72
   */
95182 fvs 73
  function start($s, $ignore = TRUE) {
74
    if (!isset($this->timers[$s])) {
75
      $this->timers[$s] = microtime();
76
      if (!isset($this->sums[$s])) $this->sums[$s] = 0;
77
    }
78
    elseif (!$ignore) {
82190 hhl 79
      die("FATAL: Cannot start timer $s... already running");
95182 fvs 80
    }
82190 hhl 81
  }
82
 
83
  /**
118246 fvs 84
   * \brief stop a timer
85
   * @param string $s - Name of timer to stop
86
   * @param boolean $ignore - Ignore not running timer (default true)
87
   */
95182 fvs 88
  function stop($s, $ignore = TRUE) {
89
    if (isset($this->timers[$s])) {
118246 fvs 90
      list($usec_stop, $sec_stop) = explode(" ", microtime());
95182 fvs 91
      list($usec_start, $sec_start) = explode(" ", $this->timers[$s]);
92
      $this->timers[$s] = null;
93
      $this->sums[$s] += ((float)$usec_stop - (float)$usec_start) + (float)($sec_stop - $sec_start);
94
    }
95
    elseif (!$ignore) {
82190 hhl 96
      die("FATAL: Cannot stop timer $s... not running");
95182 fvs 97
    }
82190 hhl 98
  }
99
 
100
 
101
  /**
118246 fvs 102
   * \brief splittime
103
   * @param string $s - Name of timer
104
   * @return mixed splittime
105
   */
82190 hhl 106
  function splittime($s) {
107
    $add = 0;
108
    if ($this->timers[$s]) {
118246 fvs 109
      list($usec_stop, $sec_stop) = explode(" ", microtime());
82190 hhl 110
      list($usec_start, $sec_start) = explode(" ", $this->timers[$s]);
111
      $add = ((float)$usec_stop - (float)$usec_start) + (float)($sec_stop - $sec_start);
112
    }
118246 fvs 113
    return ($this->sums[$s] + $add);
82190 hhl 114
  }
115
 
116
  /**
117
   * \brief format
118246 fvs 118
   * @param string $format - name of default format (file, screen or perl);
119
   */
82190 hhl 120
  function format($format) {
121
    if ($format == "perl") {
118246 fvs 122
      $this->prefix = "{ 'url' => '" . urlencode($_SERVER["PHP_SELF"]) . "', 'ts' => " . time() . ", ";
123
      $this->delim = ", ";
82190 hhl 124
      $this->postfix = " }";
118246 fvs 125
      $this->format = "'%s' => %0.6f";
82190 hhl 126
    }
127
    else if ($format == "file") {
128
      if (isset($_SERVER["REQUEST_URI"])) {
118246 fvs 129
        $this->prefix = urlencode($_SERVER["REQUEST_URI"]) . ": ";
82190 hhl 130
      }
118246 fvs 131
      else {
132
        $this->prefix = $_SERVER["PHP_SELF"] . ": ";
133
      }
134
      $this->delim = " ";
82190 hhl 135
      $this->postfix = "";
118246 fvs 136
      $this->format = "%s => %0.6f";
82190 hhl 137
    }
138
    else if ($format == "screen") {
139
      if (isset($_SERVER["REQUEST_URI"])) {
118246 fvs 140
        $this->prefix = "<pre>\nTimings for: " . urlencode($_SERVER["REQUEST_URI"]) . ":\n";
82190 hhl 141
      }
118246 fvs 142
      else {
143
        $this->prefix = "<pre>\nTimings for: " . $_SERVER["PHP_SELF"] . ":\n";
144
      }
145
      $this->delim = "\n";
82190 hhl 146
      $this->postfix = "\n</pre>";
118246 fvs 147
      $this->format = "%20s => %0.6f";
82190 hhl 148
    }
149
    else {
150
      die("FATAL: Unknown format in stopwatch");
151
    }
152
  }
153
 
120132 fvs 154
  function get_timers() {
155
    $ret = $this->sums;
156
    foreach ($this->timers as $k => $v) {
157
      if (!is_null($v))
158
        $ret[$k] = self::splittime($k);
159
    }
160
    return $ret;
161
  }
162
 
82190 hhl 163
  /**
118246 fvs 164
   * \brief dump all stoptimers
165
   * @param mixed $delim delimitor
166
   * @return string Dump of timers;
167
   */
82190 hhl 168
  function dump($delim = null) {
118246 fvs 169
    foreach ($this->timers as $k => $v) {
82190 hhl 170
      if (!is_null($v))
171
        $this->stop($k);
172
    }
173
 
118246 fvs 174
    $prefix = $this->prefix;
82190 hhl 175
    $postfix = $this->postfix;
118246 fvs 176
    $format = $this->format;
177
    if (is_null($delim)) $delim = $this->delim;        // Get delimitor or constructor delimitor
82190 hhl 178
    // If unset: get defalut values
118246 fvs 179
    if (is_null($delim)) $delim = "\n\t";
180
    if (is_null($format)) $format = "%s => %01.6f";
181
    if (is_null($prefix)) $prefix = "Timings for: " . $_SERVER['REQUEST_URI'] . (preg_match("/\n/", $delim) ? $delim : " ");
82190 hhl 182
    if (is_null($postfix)) $postfix = "\n";
118246 fvs 183
    if (!preg_match("/\n\$/", $postfix)) $postfix .= "\n";        // Make sure postfix ends in a newline
82190 hhl 184
 
118259 fvs 185
    $ret = array();
82190 hhl 186
    //natcasesort($keys = array_keys($this->sums));
187
    $keys = array_keys($this->sums);
118246 fvs 188
    foreach ($keys as $k) {
82190 hhl 189
      array_push($ret, sprintf($format, $k, $this->sums[$k]));
190
    }
191
    return $prefix . join($delim, $ret) . $postfix;
192
  }
193
 
194
  /**
195
   * \brief log
118246 fvs 196
   * @param string $file filename to log in
197
   * @param string $logformat format to use for log
198
   * @return  boolean
199
   */
82190 hhl 200
  function log($file, $logformat = "perl") {
118246 fvs 201
    $prefix = $this->prefix;              // Backup format
82190 hhl 202
    $postfix = $this->postfix;
118246 fvs 203
    $format = $this->format;
204
    $delim = $this->delim;
82190 hhl 205
 
118246 fvs 206
    if (!is_null($logformat))
82190 hhl 207
      $this->format($logformat);
208
    if ($fd = fopen($file, "a")) {
209
      fwrite($fd, $this->dump());
210
      fclose($fd);
211
    }
212
 
118246 fvs 213
    $this->prefix = $prefix;              // Restore format
82190 hhl 214
    $this->postfix = $postfix;
118246 fvs 215
    $this->format = $format;
216
    $this->delim = $delim;
217
    return !!$fd;                // Boolize $fd
82190 hhl 218
  }
118246 fvs 219
}
82190 hhl 220
 
118246 fvs 221
;