public
Created — forked from bxt/Profile.php

  • Download Gist
Profile.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
<?php
/**
* A simple class for tracking execution times of PHP scripts and logging them into CVS files
* @author Bernhard Häussner
* @see https://gist.github.com/846504
*/
class Profile {
 
private $_start = array();
private $_subdata = '';
private $_file=false;
 
/**
* Initialise, configure
* @param String CSV-file to log times to
* @param boolean If or not to measure time from construction on
*/
public function __construct($file=false,$autostart=true) {
$this->_file=$file;
if($autostart) $this->start();
}
 
/**
* Start profiling and timekeeping
* @return self this (chainable)
*/
public function start() {
// use a stack of start times to enable nesting
$this->_start[] = microtime(true);
return $this;
}
 
/**
* End profiling and dump log into CSV file, implicit info()
* @param String Appears in log after final time
* @return float Seconds passed since last call of start()
*/
public function end( $message ) {
if(count($this->_start)==0) return false;
$returnV=$this->info($message);
array_pop($this->_start);
if($this->_file && count($this->_start)==0) {
$fd = fopen($this->_file, "a");
fwrite($fd, $this->_subdata );
fclose($fd);
}
if(count($this->_start)==0) {
$this->_subdata='';
}
return $returnV;
}
 
/**
* Write current execution time to log
* @param String Appears in log after current time
* @return float Seconds passed since last call of start()
*/
public function info( $message ) {
$lvl=count($this->_start);
if($lvl==0) return false;
$time=microtime(true)-$this->_start[$lvl-1];
$this->_subdata .= $time;
$this->_subdata .= ",\"" . str_repeat("-",$lvl) . " " . str_replace("\"","\"\"",$message) . "\"\n";
return $time;
}
 
}
?>
ProfileTest.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
<?php
 
require 'Profile.php';
 
class ProfileTest extends PHPUnit_Framework_TestCase {
const TESTFILE="unitTestProfile.csv";
/**
* The negative logarithm (base 10) of seconds shortest sleep interval will take
*
* change this to highter value to shorten test time,
* but test wil more often fail depending on system performance
*/
const TESTRESOLUTION=2;
protected $profiler;
protected function setUp() {
if (file_exists(self::TESTFILE)) throw new Exception("found artefact profiling file");
if (!touch(self::TESTFILE)) throw new Exception("couldn't create profiling file");
}
protected function tearDown() {
unlink(self::TESTFILE);
}
/**
* @dataProvider sleepTimes
*/
function testDefaultConstruct($s1,$s2,$s3) {
$p=new Profile();
usleep($s1*pow(10,6-self::TESTRESOLUTION));
$this->assertEquals($s1,floor($p->info("msg")*pow(10,self::TESTRESOLUTION)),"returned time");
usleep($s2*pow(10,6-self::TESTRESOLUTION));
$this->assertEquals($s1+$s2,floor($p->info("msg")*pow(10,self::TESTRESOLUTION)),"returned time");
usleep($s3*pow(10,6-self::TESTRESOLUTION));
$this->assertEquals($s1+$s2+$s3,floor($p->end("msg")*pow(10,self::TESTRESOLUTION)),"returned time");
$this->assertEquals('',file_get_contents(self::TESTFILE));
}
 
/**
* @dataProvider sleepTimes
*/
function testFileConstruct($s1,$s2,$s3) {
$p=new Profile(self::TESTFILE);
usleep($s1*pow(10,6-self::TESTRESOLUTION));
$endtime=$p->end("msg");
$this->assertEquals($s1,floor($endtime*pow(10,self::TESTRESOLUTION)),"returned time");
$expectedFileContents= "{$endtime},\"- msg\"\n";
$fileContents=file_get_contents(self::TESTFILE);
$this->assertEquals(
$expectedFileContents,$fileContents,
"result time $endtime did not math file content: ".$fileContents
);
}
/**
* @dataProvider trickyMessages
*/
function testCsvEscaping($m,$m_esc) {
$s1=2;
$p=new Profile(self::TESTFILE);
usleep($s1*pow(10,6-self::TESTRESOLUTION));
$endtime=$p->end($m);
$this->assertEquals($s1,floor($endtime*pow(10,self::TESTRESOLUTION)),"returned time");
$expectedFileContents= "{$endtime},\"- ".$m_esc."\"\n";
$fileContents=file_get_contents(self::TESTFILE);
$this->assertEquals(
$expectedFileContents,$fileContents,
"result time $endtime, message $m_esc did not math file content: ".$fileContents
);
}
 
/**
* @dataProvider sleepTimes
*/
function testNostartConstruct($s1,$s2,$s3) {
$p=new Profile(false,false);
$this->assertFalse($p->info("bah"));
$this->assertEquals($p,$p->start(),"start chainability");
usleep($s1*pow(10,6-self::TESTRESOLUTION));
$this->assertEquals($s1,floor($p->end("msg")*pow(10,self::TESTRESOLUTION)),"returned time");
$this->assertEquals('',file_get_contents(self::TESTFILE));
}
 
/**
* @dataProvider sleepTimes
*/
function testFileNostartConstruct($s1,$s2,$s3) {
$p=new Profile(self::TESTFILE,false);
$this->assertFalse($p->info("bah"));
$this->assertEquals($p,$p->start(),"start chainability");
usleep($s1*pow(10,6-self::TESTRESOLUTION));
$endtime=$p->end("msg");
$this->assertEquals($s1,floor($endtime*pow(10,self::TESTRESOLUTION)),"returned time");
$expectedFileContents= "{$endtime},\"- msg\"\n";
$fileContents=file_get_contents(self::TESTFILE);
$this->assertEquals(
$expectedFileContents,$fileContents,
"bad file content: ".$fileContents
);
}
 
/**
* @dataProvider sleepTimes
*/
function testCheckpointProfiles($s1,$s2,$s3) {
$p=new Profile(self::TESTFILE);
usleep($s1*pow(10,6-self::TESTRESOLUTION));
$infotime1=$p->info("msg1");
$this->assertEquals($s1,floor($infotime1*pow(10,self::TESTRESOLUTION)),"returned time");
usleep($s2*pow(10,6-self::TESTRESOLUTION));
$infotime2=$p->info("msg2");
$this->assertEquals($s1+$s2,floor($infotime2*pow(10,self::TESTRESOLUTION)),"returned time");
$this->assertEquals('',file_get_contents(self::TESTFILE));
usleep($s3*pow(10,6-self::TESTRESOLUTION));
$endtime=$p->end("msg3");
$this->assertEquals($s1+$s2+$s3,floor($endtime*pow(10,self::TESTRESOLUTION)),"returned time");
$expectedFileContents= "{$infotime1},\"- msg1\"\n{$infotime2},\"- msg2\"\n{$endtime},\"- msg3\"\n";
$fileContents=file_get_contents(self::TESTFILE);
$this->assertEquals(
$expectedFileContents,$fileContents,
"bad for eg {$infotime2} file content: ".$fileContents
);
}
/**
* @dataProvider sleepTimes
*/
function testRestarting($s1,$s2,$s3) {
$p=new Profile(self::TESTFILE);
usleep($s1*pow(10,6-self::TESTRESOLUTION));
$endtime1=$p->end("msg1");
$this->assertEquals($s1,floor($endtime1*pow(10,self::TESTRESOLUTION)),"returned time");
$expectedFileContents= "{$endtime1},\"- msg1\"\n";
$fileContents=file_get_contents(self::TESTFILE);
$this->assertEquals(
$expectedFileContents,$fileContents,
"bad file content: ".$fileContents
);
$this->assertEquals($p,$p->start(),"start chainability");
usleep($s2*pow(10,6-self::TESTRESOLUTION));
$endtime2=$p->end("msg2");
$this->assertEquals($s2,floor($endtime2*pow(10,self::TESTRESOLUTION)),"returned time");
$expectedFileContents= "{$endtime1},\"- msg1\"\n{$endtime2},\"- msg2\"\n";
$fileContents=file_get_contents(self::TESTFILE);
$this->assertEquals(
$expectedFileContents,$fileContents,
"bad file content: ".$fileContents
);
$this->assertEquals($p,$p->start(),"start chainability");
usleep($s3*pow(10,6-self::TESTRESOLUTION));
$endtime3=$p->end("msg3");
$this->assertEquals($s3,floor($endtime3*pow(10,self::TESTRESOLUTION)),"returned time");
$expectedFileContents= "{$endtime1},\"- msg1\"\n{$endtime2},\"- msg2\"\n{$endtime3},\"- msg3\"\n";
$fileContents=file_get_contents(self::TESTFILE);
$this->assertEquals(
$expectedFileContents,$fileContents,
"bad file content: ".$fileContents
);
}
 
/**
* @dataProvider sleepTimes
*/
function testNestedProfiles($s1,$s2,$s3) {
$p=new Profile(self::TESTFILE);
usleep($s1*pow(10,6-self::TESTRESOLUTION));
$infotime1=$p->info("msg1");
$this->assertEquals($s1,floor($infotime1*pow(10,self::TESTRESOLUTION)),"returned time");
$this->assertEquals($p,$p->start(),"start chainability");
usleep($s2*pow(10,6-self::TESTRESOLUTION));
$endtime1=$p->end("msgI");
$this->assertEquals($s2,floor($endtime1*pow(10,self::TESTRESOLUTION)),"nested returned time");
$this->assertEquals('',file_get_contents(self::TESTFILE));
usleep($s3*pow(10,6-self::TESTRESOLUTION));
$endtime2=$p->end("msg3");
$this->assertEquals($s1+$s2+$s3,floor($endtime2*pow(10,self::TESTRESOLUTION)),"returned time");
$expectedFileContents= "{$infotime1},\"- msg1\"\n{$endtime1},\"-- msgI\"\n{$endtime2},\"- msg3\"\n";
$fileContents=file_get_contents(self::TESTFILE);
$this->assertEquals(
$expectedFileContents,$fileContents,
"bad for eg {$endtime1} file content: ".$fileContents
);
}
function sleepTimes() {
return array(
array(1,1,1),
array(1,2,1),
array(2,1,1),
array(1,1,2),
array(1,2,3),
array(2,1,2),
array(3,2,1),
);
}
function trickyMessages() {
return array(
array('untricky','untricky'),
array('containing space','containing space'),
array('containing more space','containing more space'),
array('containing "quotes" too','containing ""quotes"" too'),
array('containing \'quotes\' that dont matter','containing \'quotes\' that dont matter'),
array("containing \n two lines","containing \n two lines"),
array('"cont"aining "lotta" quotes"','""cont""aining ""lotta"" quotes""'),
);
}
}
profiler_usage.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php
 
require_once "Profile.php";
$profiler=new Profile("/tmp/profile.csv");
 
// ...
 
$profiler->start();
$q="SELECT * FROM table";
$query = mysql_query($q);
$profiler->end($q);
 
// ...
 
$profiler->info( "checkpoint" );
 
// ...
 
$profiler->end( "page" );
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.