├── composer.json ├── .travis.yml ├── test └── Timer.php ├── src └── Timer.php ├── README.md └── Usage.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nsa-yoda/phpbenchtime", 3 | "description": "A light benchmark timer class for PHP.", 4 | "keywords":[ 5 | "benchmark", 6 | "timer", 7 | "benchtime", 8 | "profiler", 9 | "stopwatch" 10 | ], 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Juan Sanchez", 15 | "email": "juan.sanchez@juanleonardosanchez.com" 16 | } 17 | ], 18 | "autoload": { 19 | "psr-4": { 20 | "PHPBenchTime\\": "src/" 21 | } 22 | } 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | - 5.6 8 | - 7.0 9 | - 7.1 10 | - 7.2 11 | - hhvm 12 | 13 | # set up allowed failures in the matrix 14 | matrix: 15 | allow_failures: 16 | - php: 5.3 17 | - php: 5.4 18 | - php: 5.5 19 | - php: 7.0 20 | - php: 7.1 21 | - php: hhvm 22 | fast_finish: true 23 | 24 | cache: 25 | directories: 26 | - $HOME/.composer/cache/files 27 | 28 | before_install: 29 | - sudo apt-get update -qq 30 | 31 | install: 32 | - curl -sS https://getcomposer.org/installer | php 33 | - composer self-update 34 | - composer install --prefer-source --no-interaction --dev 35 | # - if [[ $HHVM == true ]]; then composer require "phpunit/phpunit:5.7"; fi 36 | 37 | before_script: 38 | - php --version 39 | - php composer.phar composer --version 40 | - php composer.phar install 41 | - travis_retry composer install --dev --prefer-dist 42 | 43 | script: 44 | - phpunit . 45 | 46 | after_failure: 47 | - cat MSBuild_*.failure.txt 48 | -------------------------------------------------------------------------------- /test/Timer.php: -------------------------------------------------------------------------------- 1 | timer = new \PHPBenchTime\Timer; 13 | } 14 | 15 | public function testClassAttributes() { 16 | $this->assertClassHasAttribute( 'startTime', 'PHPBenchTime\Timer' ); 17 | $this->assertClassHasAttribute( 'endTime', 'PHPBenchTime\Timer' ); 18 | $this->assertClassHasAttribute( 'pauseTime', 'PHPBenchTime\Timer' ); 19 | $this->assertClassHasAttribute( 'laps', 'PHPBenchTime\Timer' ); 20 | $this->assertClassHasAttribute( 'lapCount', 'PHPBenchTime\Timer' ); 21 | } 22 | 23 | public function testStart() { 24 | $this->timer->start('test_start'); 25 | $this->assertEquals(\PHPBenchTime\Timer::RUNNING, $this->timer->state); 26 | $this->assertGreaterThanOrEqual(microtime(true) - 3, $this->timer->startTime); 27 | $this->assertLessThanOrEqual(microtime(true) + 3, $this->timer->startTime); 28 | } 29 | 30 | public function testLap() { 31 | $this->timer->lap(); 32 | $this->assertGreaterThan(0, $this->timer->lapCount); 33 | $this->assertGreaterThan(0, count($this->timer->laps)); 34 | } 35 | 36 | public function testSummary() { 37 | $summary = $this->timer->summary(); 38 | $this->assertArrayHasKey('running', $summary); 39 | $this->assertArrayHasKey('start', $summary); 40 | $this->assertArrayHasKey('end', $summary); 41 | $this->assertArrayHasKey('total', $summary); 42 | $this->assertArrayHasKey('paused', $summary); 43 | $this->assertArrayHasKey('laps', $summary); 44 | $this->assertEquals(0, $summary['total']); 45 | } 46 | 47 | public function testPause() { 48 | $this->timer->pause(); 49 | $this->assertEquals(\PHPBenchTime\Timer::PAUSED, $this->timer->state); 50 | } 51 | 52 | public function testUnpause() { 53 | $this->timer->unpause(); 54 | $this->assertEquals(\PHPBenchTime\Timer::RUNNING, $this->timer->state); 55 | $this->assertGreaterThan(0, $this->timer->totalPauseTime); 56 | $this->assertEquals(0, $this->timer->pauseTime); 57 | } 58 | 59 | public function endLap() { 60 | $this->timer->endLap(); 61 | $this->assertGreaterThan(0, count($this->timer->laps)); 62 | } 63 | 64 | public function getCurrentTime() { 65 | $this->assertGreaterThanOrEqual(microtime(true) - 3, $this->timer->getCurrentTime()); 66 | $this->assertLessThanOrEqual(microtime(true) + 3, $this->timer->getCurrentTime()); 67 | } 68 | 69 | public function testEnd() { 70 | $this->timer->end(); 71 | $this->assertEquals(\PHPBenchTime\Timer::STOPPED, $this->timer->state); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Timer.php: -------------------------------------------------------------------------------- 1 | 7 | * @license MIT 8 | * @version 2.1.0 9 | * @internal 07.23.2014 10 | */ 11 | 12 | namespace PHPBenchTime; 13 | 14 | class Timer { 15 | /** 16 | * Handle the running state of the timer 17 | */ 18 | const RUNNING = 1; 19 | const PAUSED = 0; 20 | const STOPPED = -1; 21 | 22 | /** 23 | * Maintains the state of the timer (RUNNING, PAUSED, STOPPED) 24 | * @var int 25 | */ 26 | public int $state = self::STOPPED; 27 | 28 | /** 29 | * Time that $this->start() was called 30 | * @var int 31 | */ 32 | public int $startTime = 0; 33 | 34 | /** 35 | * Time that $this->end() was called 36 | * @var int 37 | */ 38 | public int $endTime = 0; 39 | 40 | /** 41 | * Total time spent in pause 42 | * @var int 43 | */ 44 | public int $totalPauseTime = 0; 45 | 46 | /** 47 | * Time spent in pause 48 | * @var int 49 | */ 50 | public int $pauseTime = 0; 51 | 52 | /** 53 | * All laps 54 | * @var array 55 | */ 56 | public array $laps = array(); 57 | 58 | /** 59 | * Total lap count, inclusive of the current lap 60 | * @var int 61 | */ 62 | public int $lapCount = 0; 63 | 64 | /** 65 | * Class constructor 66 | */ 67 | public function __construct() { 68 | $this->reset(); 69 | } 70 | 71 | /** 72 | * Resets the timers, laps and summary 73 | */ 74 | public function reset() { 75 | $this->startTime = 0; 76 | $this->endTime = 0; 77 | $this->pauseTime = 0; 78 | $this->laps = []; 79 | $this->lapCount = 0; 80 | } 81 | 82 | /** 83 | * Starts the timer 84 | * @param string $name 85 | */ 86 | public function start( $name = "start" ) { 87 | $this->state = self::RUNNING; 88 | 89 | # Set the start time 90 | $this->startTime = $this->getCurrentTime(); 91 | 92 | # Create a lap with this start time 93 | $this->lap( $name ); 94 | } 95 | 96 | /** 97 | * Ends the timer 98 | */ 99 | public function end() { 100 | $this->state = self::STOPPED; 101 | 102 | # Set the end time 103 | $this->endTime = $this->getCurrentTime(); 104 | 105 | # end the last lap 106 | $this->endLap(); 107 | } 108 | 109 | /** 110 | * Creates a new lap in lap array property 111 | * @param null $name 112 | */ 113 | public function lap( $name = null ) { 114 | $this->endLap(); # end the last lap 115 | 116 | # Create new lap 117 | $this->laps[] = array( 118 | "name" => ( $name ? $name : $this->lapCount ), 119 | "start" => $this->getCurrentTime(), 120 | "end" => -1, 121 | "total" => -1, 122 | ); 123 | 124 | $this->lapCount += 1; 125 | } 126 | 127 | /** 128 | * Assign end and total times to the previous lap 129 | */ 130 | public function endLap() { 131 | $lapCount = count( $this->laps ) - 1; 132 | if ( count( $this->laps ) > 0 ) { 133 | $this->laps[$lapCount]['end'] = $this->getCurrentTime(); 134 | $this->laps[$lapCount]['total'] = $this->laps[$lapCount]['end'] - $this->laps[$lapCount]['start']; 135 | } 136 | } 137 | 138 | /** 139 | * Returns a summary of all timer activity so far 140 | * @return array 141 | */ 142 | public function summary() { 143 | return array( 144 | 'running' => $this->state, 145 | 'start' => $this->startTime, 146 | 'end' => $this->endTime, 147 | 'total' => $this->endTime - $this->startTime, 148 | 'paused' => $this->totalPauseTime, 149 | 'laps' => $this->laps 150 | ); 151 | } 152 | 153 | /** 154 | * Initiates a pause in the timer 155 | */ 156 | public function pause() { 157 | $this->state = self::PAUSED; 158 | $this->pauseTime = $this->getCurrentTime(); 159 | } 160 | 161 | /** 162 | * Cancels the pause previously set 163 | */ 164 | public function unpause() { 165 | $this->state = self::RUNNING; 166 | $this->totalPauseTime += $this->getCurrentTime() - $this->pauseTime; 167 | $this->pauseTime = 0; 168 | } 169 | 170 | /** 171 | * Returns the current time 172 | * @return float 173 | */ 174 | public function getCurrentTime() { 175 | return microtime( true ); 176 | } 177 | } 178 | 179 | 180 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PHPBenchTime v2.1.0 2 | =================== 3 | 4 | [](https://packagist.org/packages/nsa-yoda/phpbenchtime) 5 | [](https://packagist.org/packages/nsa-yoda/phpbenchtime) 6 | [](https://packagist.org/packages/nsa-yoda/phpbenchtime) 7 | [](https://packagist.org/packages/nsa-yoda/phpbenchtime) 8 | [](https://travis-ci.org/nsa-yoda/PHPBenchTime) 9 | 10 | A light benchmark timer class for PHP. PHPBenchTime is quite simple to use and is loaded with functionality - including detailed summary data, easily readable source, a robust lap system and pause/unpause functionality. 11 | 12 | Also, please check out my Python version of this package: [PyBenchTime Python Package](https://github.com/nsa-yoda/PyBenchTime) 13 | 14 | On Packagist 15 | ============ 16 | https://packagist.org/packages/nsa-yoda/phpbenchtime 17 | 18 | Methods 19 | ======= 20 | ``` 21 | public start() 22 | public end() 23 | public reset() 24 | public lap() 25 | public summary() 26 | public pause() 27 | public unPause() 28 | private endLap() 29 | private getCurrentTime() 30 | ``` 31 | 32 | Properties 33 | ========== 34 | ``` 35 | private startTime 36 | private endTime 37 | private pauseTime 38 | private laps 39 | private lapCount 40 | ``` 41 | 42 | 43 | Usage 44 | ===== 45 | 46 | You should see the Usage.php file in source for the best how to documentation. However, here's an overview: 47 | 48 | Load and initiate the PHPBenchTime Timer: 49 | ``` 50 | require('PHPBenchTime.php'); 51 | use PHPBenchTime\Timer; 52 | $T = new Timer; 53 | ``` 54 | 55 | 56 | That was easy! Now lets start a new timer: 57 | 58 | ``` 59 | $T->start(); 60 | ``` 61 | 62 | Then lets just sleep for 3 seconds: 63 | ``` 64 | sleep(3); 65 | ``` 66 | 67 | Now, lets end the timer, and put results in $time: 68 | ``` 69 | $time = $T->end(); 70 | ``` 71 | 72 | When we end a timer, we receive an array back, containing the start time, 73 | end time and difference between start and end times: 74 | ``` 75 | Array ( 76 | [running] => false 77 | [start] => 1406146951.9998 78 | [end] => 1406146952.0638 79 | [total] => 0.0019998550415039 80 | [paused] => 0 81 | [laps] => Array ( 82 | [0] => Array ( 83 | [name] => start 84 | [start] => 1406146951.9998 85 | [end] => 1406146952.0018 86 | [total] => 0.0019998550415039 87 | ) 88 | ) 89 | ) 90 | ``` 91 | 92 | Advanced Usage : Laps 93 | ===================== 94 | 95 | PHPBenchTime also allows you to set laps between code execution, which allows 96 | you to determine what part of your code is causing a bottleneck. 97 | 98 | Let's sleep for a couple of seconds between laps: 99 | ``` 100 | sleep(1); 101 | $T->lap(); 102 | sleep(2); 103 | $T->lap(); 104 | ``` 105 | 106 | Now, let's end the timer: 107 | ``` 108 | $time = $T->end(); 109 | ``` 110 | 111 | Let's see the results: 112 | ``` 113 | Array ( 114 | [running] => false 115 | [start] => 1406146951.9998 116 | [end] => 1406146952.0638 117 | [total] => 0.063999891281128 118 | [paused] => 0.041000127792358 119 | [laps] => Array ( 120 | [0] => Array ( 121 | [name] => start 122 | [start] => 1406146951.9998 123 | [end] => 1406146952.0018 124 | [total] => 0.0019998550415039 125 | ) 126 | [1] => Array ( 127 | [name] => 1 128 | [start] => 1406146952.0018 129 | [end] => 1406146952.0028 130 | [total] => 0.0010001659393311 131 | ) 132 | [2] => Array ( 133 | [name] => 2 134 | [start] => 1406146952.0028 135 | [end] => 1406146952.0128 136 | [total] => 0.0099999904632568 137 | ) 138 | ) 139 | ) 140 | ``` 141 | 142 | Advanced Usage 143 | ============== 144 | PHPBenchTime allows you to do named laps, as well as to pause and unpause the timer (say you want to make a network 145 | call or a call out to the database, but don't want to include that time in your benchmark - pause and then unpause after 146 | you receive the network/database data). 147 | 148 | HISTORY 149 | ======= 150 | 151 | * v1.0.0: Static Birth! 152 | * v1.1.0: Static Namespaces! 153 | * v1.2.0: Non-Static Namespaces! 154 | * v1.3.0: Laps! Laps! Laps! 155 | * v2.0.0: Complete rewrite, adds pause, unpause, central lap system and more detailed summary 156 | * v2.1.0: Performance enhancements, unit tests, etc 157 | -------------------------------------------------------------------------------- /Usage.php: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |# Require PHPBenchTime
56 | include("src/Timer.php");
57 |
58 | # Namespace
59 | use PHPBenchTime\Timer;
60 | $T = new Timer();
64 | sleep(1);
65 | $end = $T->End(); # Returns an array into $end
66 |
67 | Array (
71 | [running] => false
72 | [start] => 1406146951.9998
73 | [end] => 1406146952.0638
74 | [total] => 0.063999891281128
75 | [paused] => 0.041000127792358
76 | [laps] => Array (
77 | [0] => Array (
78 | [name] => Start
79 | [start] => 1406146951.9998
80 | [end] => 1406146952.0018
81 | [total] => 0.0019998550415039
82 | )
83 | [1] => Array (
84 | [name] => Second lap
85 | [start] => 1406146952.0018
86 | [end] => 1406146952.0028
87 | [total] => 0.0010001659393311
88 | )
89 | [2] => Array (
90 | [name] => Third lap
91 | [start] => 1406146952.0028
92 | [end] => 1406146952.0128
93 | [total] => 0.0099999904632568
94 | )
95 | [3] => Array (
96 | [name] => Tiny lap between pauses
97 | [start] => 1406146952.0128
98 | [end] => 1406146952.0638
99 | [total] => 0.050999879837036
100 | )
101 | [4] => Array (
102 | [name] => Last lap
103 | [start] => 1406146952.0638
104 | [end] => 1406146952.0639
105 | [total] => 0.0000999999999999
106 | )
107 | )
108 | )
109 |
110 | $Benchmark = new Timer();
114 | $T->Start();
115 | sleep(1);
116 | $T->Lap();
117 | sleep(1);
118 | $T->Lap();
119 | sleep(1);
120 | $T->Lap();
121 | $end = $T->End(); # Returns an array into $end
122 |
123 | $T = new Timer();
127 | $T->Start("Start Timer");
128 | sleep(1);
129 | $T->Lap("Slept for 1 second");
130 | sleep(1);
131 | $T->Lap("Slept for 1 more second");
132 | sleep(1);
133 | $T->Lap("Slept for another second");
134 | $end = $T->End(); # Returns an array into $end
135 |
136 | $T = new Timer();
140 | $T->Start("Start Timer");
141 | sleep(1);
142 | $T->Lap("Slept for 1 second");
143 | sleep(1);
144 | $T->Pause();
145 | sleep(3);
146 | $T->Unpause();
147 | $T->Lap("Slept for another second");
148 | $end = $T->End(); # Returns an array into $end
149 |
150 |