cilogon /
service-lib
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace CILogon\Service; |
||||
| 4 | |||||
| 5 | /** |
||||
| 6 | * TimeIt |
||||
| 7 | * |
||||
| 8 | * This class allows for the timing of PHP code. |
||||
| 9 | * |
||||
| 10 | * Example usage: |
||||
| 11 | * require_once 'TimeIt.php'; |
||||
| 12 | * $timeit = new TimeIt('/tmp/myprogramtime.txt'); |
||||
| 13 | * $timeit->printTime('Program Start...'); |
||||
| 14 | * ... your program code here ... |
||||
| 15 | * $timeit->printTime('Program End ...'); |
||||
| 16 | * |
||||
| 17 | * Example output of '/tmp/myprogramtime.txt': |
||||
| 18 | * Program Start... 0.0004 0.0140 0.0136 |
||||
| 19 | * Program End ... 0.0141 0.0142 0.0001 |
||||
| 20 | * |
||||
| 21 | * First column is the label parameter of the printTime() method. |
||||
| 22 | * Second column the current 'program time'. |
||||
| 23 | * Third column is the time difference between the current line and the |
||||
| 24 | * next line (or the end of the script when the file is closed). |
||||
| 25 | * Fourth column is the difference between the second and third columns. |
||||
| 26 | */ |
||||
| 27 | class TimeIt |
||||
| 28 | { |
||||
| 29 | /** |
||||
| 30 | * @var string DEFAULTFILENAME Set the output file location to |
||||
| 31 | * correspond to your particular set up. |
||||
| 32 | */ |
||||
| 33 | public const DEFAULTFILENAME = "/tmp/timing.txt"; |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * @var string $timingfilename File name of the timing file |
||||
| 37 | */ |
||||
| 38 | protected $timingfilename; |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * @var resource $fh The file handle of the timing file |
||||
| 42 | */ |
||||
| 43 | protected $fh = null; |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * @var float $firsttime Time when object is constructed |
||||
| 47 | */ |
||||
| 48 | protected $firsttime; |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * @var float $lasttime Last time getTime() was called |
||||
| 52 | */ |
||||
| 53 | protected $lasttime = 0; |
||||
| 54 | |||||
| 55 | /** |
||||
| 56 | * @var bool $firstlineprinted Print the first line only once. |
||||
| 57 | */ |
||||
| 58 | protected $firstlineprinted = false; |
||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * __construct |
||||
| 62 | * |
||||
| 63 | * Default constructor. Sets the filename for the timing file, |
||||
| 64 | * opens the file with the appropriate mode (append or rewrite), |
||||
| 65 | * and sets the internal timing values for printout later. |
||||
| 66 | * |
||||
| 67 | * @param string $filename (Optional) The name of the file to write |
||||
| 68 | * timings to. Defaults to '/tmp/timing.txt'. |
||||
| 69 | * @param bool $append (Optional) If true, append to file (and create if |
||||
| 70 | * needed). If false, rewrite timing file from scratch. Defaults |
||||
| 71 | * to false. |
||||
| 72 | * @return TimeIt A new TimeIt object. |
||||
| 73 | */ |
||||
| 74 | public function __construct( |
||||
| 75 | $filename = self::DEFAULTFILENAME, |
||||
| 76 | $append = false |
||||
| 77 | ) { |
||||
| 78 | $this->setFilename($filename); |
||||
| 79 | $this->openFile($append); |
||||
| 80 | $this->firsttime = $this->getTime(); |
||||
| 81 | } |
||||
| 82 | |||||
| 83 | /** |
||||
| 84 | * __destruct |
||||
| 85 | * |
||||
| 86 | * Default destructor. Prints out the last timing value to the |
||||
| 87 | * timing file, and then closes the timing file. |
||||
| 88 | */ |
||||
| 89 | public function __destruct() |
||||
| 90 | { |
||||
| 91 | if ($this->firstlineprinted) { |
||||
| 92 | $currtime = $this->getTime() - $this->firsttime; |
||||
| 93 | fprintf( |
||||
| 94 | $this->fh, |
||||
| 95 | "%8.4f" . "\t" . "%8.4f" . "\n", |
||||
| 96 | $currtime, |
||||
| 97 | ($currtime - $this->lasttime) |
||||
| 98 | ); |
||||
| 99 | } |
||||
| 100 | $this->closeFile(); |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | /** |
||||
| 104 | * getFilename |
||||
| 105 | * |
||||
| 106 | * This function returns a string of the full path of the file to |
||||
| 107 | * which timing info is written. |
||||
| 108 | * |
||||
| 109 | * @return string The name of the file to write timings to. |
||||
| 110 | */ |
||||
| 111 | public function getFilename() |
||||
| 112 | { |
||||
| 113 | return $this->timingfilename; |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | /** |
||||
| 117 | * setFilename |
||||
| 118 | * |
||||
| 119 | * This function sets the string of the full path of the file to |
||||
| 120 | * which timing info is written. |
||||
| 121 | * |
||||
| 122 | * @param string $filename (Optional) The name of the timing file. |
||||
| 123 | * Defaults to '/tmp/timing.txt'. |
||||
| 124 | */ |
||||
| 125 | public function setFilename($filename = self::DEFAULTFILENAME) |
||||
| 126 | { |
||||
| 127 | $this->timingfilename = $filename; |
||||
| 128 | } |
||||
| 129 | |||||
| 130 | /** |
||||
| 131 | * openFile |
||||
| 132 | * |
||||
| 133 | * This function opens the timing file with the appropriate mode |
||||
| 134 | * (append or write from scratch). The file handle is stored in the |
||||
| 135 | * protected member variable $fh. If there is a problem opening |
||||
| 136 | * the file, the file handle is set to null and false is returned. |
||||
| 137 | * |
||||
| 138 | * @param bool $append (Optional) If true, append to file (and create if |
||||
| 139 | * needed). If false, rewrite timing file from scratch. Defaults |
||||
| 140 | * to false. |
||||
| 141 | * @return bool True if opened file successfully. False otherwise. |
||||
| 142 | */ |
||||
| 143 | public function openFile($append = false) |
||||
| 144 | { |
||||
| 145 | $retval = true; // Assume successfully opened file |
||||
| 146 | $this->fh = fopen($this->getFilename(), ($append ? 'a' : 'w')); |
||||
| 147 | if (!$this->fh) { |
||||
| 148 | $this->fh = null; |
||||
| 149 | $retval = false; |
||||
| 150 | } |
||||
| 151 | return $retval; |
||||
| 152 | } |
||||
| 153 | |||||
| 154 | /** |
||||
| 155 | * closeFile |
||||
| 156 | * |
||||
| 157 | * This function closes the timing file if it is open. |
||||
| 158 | */ |
||||
| 159 | public function closeFile() |
||||
| 160 | { |
||||
| 161 | if (!is_null($this->fh)) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 162 | fclose($this->fh); |
||||
| 163 | $this->fh = null; |
||||
| 164 | } |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | /** |
||||
| 168 | * getTime |
||||
| 169 | |||||
| 170 | * @param int $precision (Optional) The number of decimal places (i.e. |
||||
| 171 | * precision). Defaults to 4. |
||||
| 172 | * @return float The current Unix timestamp with microseconds, using |
||||
| 173 | * the specified number of decimal places. |
||||
| 174 | */ |
||||
| 175 | public function getTime($precision = 4) |
||||
| 176 | { |
||||
| 177 | return round(microtime(true), $precision); |
||||
|
0 ignored issues
–
show
It seems like
microtime(true) can also be of type string; however, parameter $num of round() does only seem to accept double|integer, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 178 | } |
||||
| 179 | |||||
| 180 | /** |
||||
| 181 | * printTime |
||||
| 182 | * |
||||
| 183 | * This is the main function of this class. It prints out a line |
||||
| 184 | * to the timing file. The first time this function is called, it |
||||
| 185 | * prints out the specified 'label' string to the file followed |
||||
| 186 | * by the current 'program time' (which is the difference between |
||||
| 187 | * the current clock time and the time the object was initialized) |
||||
| 188 | * which should be close to zero. Each subsequent time this method |
||||
| 189 | * is called will append two more numbers to the previous line: the |
||||
| 190 | * new current 'program time' and the difference between the new |
||||
| 191 | * and old program times. This gives the user an indication of |
||||
| 192 | * how long it took between to printTime()s. |
||||
| 193 | * |
||||
| 194 | * @param string $label A string to prepend to the timing line in the file. |
||||
| 195 | */ |
||||
| 196 | public function printTime($label) |
||||
| 197 | { |
||||
| 198 | if ($this->firstlineprinted) { |
||||
| 199 | $currtime = $this->getTime() - $this->firsttime; |
||||
| 200 | fprintf( |
||||
| 201 | $this->fh, |
||||
| 202 | "%8.4f" . "\t" . "%8.4f" . "\n", |
||||
| 203 | $currtime, |
||||
| 204 | ($currtime - $this->lasttime) |
||||
| 205 | ); |
||||
| 206 | } |
||||
| 207 | $this->lasttime = $this->getTime() - $this->firsttime; |
||||
| 208 | fprintf($this->fh, $label . "\t" . "%8.4f" . "\t", $this->lasttime); |
||||
| 209 | $this->firstlineprinted = true; |
||||
| 210 | } |
||||
| 211 | } |
||||
| 212 |