1: <?php
2: /**
3: * An instance of this class is returned by
4: * Horde_View_Helper_Benchmark::benchmark().
5: *
6: * Copyright 2007-2008 Maintainable Software, LLC
7: * Copyright 2006-2012 Horde LLC (http://www.horde.org/)
8: *
9: * @author Mike Naberezny <mike@maintainable.com>
10: * @author Derek DeVries <derek@maintainable.com>
11: * @author Chuck Hagenbuch <chuck@horde.org>
12: * @license http://www.horde.org/licenses/bsd
13: * @category Horde
14: * @package View
15: * @subpackage Helper
16: */
17:
18: /**
19: * An instance of this class is returned by
20: * Horde_View_Helper_Benchmark::benchmark().
21: *
22: * @author Mike Naberezny <mike@maintainable.com>
23: * @author Derek DeVries <derek@maintainable.com>
24: * @author Chuck Hagenbuch <chuck@horde.org>
25: * @license http://www.horde.org/licenses/bsd
26: * @category Horde
27: * @package View
28: * @subpackage Helper
29: */
30: class Horde_View_Helper_Benchmark_Timer
31: {
32: /**
33: * (Micro-)time that the benchmark was started.
34: *
35: * @var float
36: */
37: protected $_start;
38:
39: /**
40: * Logger instance that will be used to record the time after the benchmark
41: * has ended.
42: *
43: * @var Horde_Log_Logger
44: */
45: protected $_logger;
46:
47: /**
48: * Message to log after the benchmark has ended
49: *
50: * @var string
51: */
52: protected $_message;
53:
54: /**
55: * Log level to log after the benchmark has ended.
56: *
57: * @var string|integer
58: */
59: protected $_level;
60:
61: /**
62: * Starts a new benchmark.
63: *
64: * @param string $message Message to log after the benchmark has
65: * ended.
66: * @param string|integer $level Log level to log after the benchmark
67: * has ended.
68: * @param Horde_Log_Logger $logger Logger instance.
69: */
70: public function __construct($message, $level = 'info', $logger = null)
71: {
72: $this->_message = $message;
73: $this->_level = $level;
74: $this->_logger = $logger;
75: $this->_start = microtime(true);
76: }
77:
78: /**
79: * Ends the benchmark and log the result.
80: */
81: public function end()
82: {
83: if ($this->_logger) {
84: // Compute elapsed time and build message.
85: $elapsed = microtime(true) - $this->_start;
86: $message = sprintf('%s (%.5f)', $this->_message, $elapsed);
87:
88: // Log message (level may be specified as integer or string).
89: if (is_integer($this->_level)) {
90: $this->_logger->log($message, $this->_level);
91: } else {
92: $this->_logger->{$this->_level}($message);
93: }
94: }
95: }
96: }
97: