Skip to content

Instantly share code, notes, and snippets.

@mrkmg
Last active October 10, 2015 19:59
Show Gist options
  • Save mrkmg/3743467 to your computer and use it in GitHub Desktop.
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
<?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