Skip to content

Instantly share code, notes, and snippets.

@Mwni
Created June 19, 2023 21:04
Show Gist options
  • Save Mwni/b84d64034e5724b63d01b116a4b5276a to your computer and use it in GitHub Desktop.
Save Mwni/b84d64034e5724b63d01b116a4b5276a to your computer and use it in GitHub Desktop.
Simple PHP script for logging page load times. Will create a text file named "loadtimes.txt" in the working directory. Only use this for testing.
<?php
$starttime = microtime(true);
// PAGE CODE GOES HERE
try{
$endtime = microtime(true);
$duration = $endtime - $starttime;
$date = new DateTime();
$timestamp = $date->format('Y-m-d H:i:s.u');
$entry = sprintf("[%s] %.5f seconds for %s\n", $timestamp, $duration, $_SERVER['REQUEST_URI']);
$handle = fopen('loadtimes.txt', 'a');
if(flock($handle, LOCK_EX)){
fwrite($handle, $entry);
fflush($handle);
flock($handle, LOCK_UN);
}
fclose($handle);
}catch(Exception $e){}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment