Last active
October 10, 2015 19:59
-
-
Save mrkmg/3743467 to your computer and use it in GitHub Desktop.
Tick Per Minute Class. You can add a tick at anytime, and then later calculate the average ticks per min
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TPM { | |
public $useCtime = true; //If true, use current time for tpm calculation. If false, use last tick. | |
public $purgeOlderThan = 15; //Time in minutes to purge old data | |
private $ticks = array(); | |
//Add a tick | |
public function tick(){ | |
$this->ticks[] = time(); | |
} | |
//Get the Ticks per Min | |
public function getTPM(){ | |
$this->clean(); | |
sort($this->ticks,SORT_NUMERIC); | |
$count = count($this->ticks); | |
if($count < 2) return 0; | |
$lowest = $this->ticks[0]; | |
$highest = $this->useCtime?time():$this->ticks[$count-1]; | |
if($lowest == $highest) return 0; | |
$min = ($highest-$lowest)/60; | |
return round($count/$min,2); | |
} | |
private function clean(){ | |
$before = time()-($this->purgeOlderThan*60); | |
$this->ticks = array_filter($this->ticks,create_function('$a','return $a>'.$before.';')); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment