Server benchmarking php performance - php

I have several hosting accounts with different companies, and I'm trying to evaluate which one will run a stock Wordpress installation the fastest (without having to install wordpress on each of them first).
I found a php benchmarking script to try and determine which one would work best and I'm having strange results.
<?php
/*
##########################################################################
# PHP Benchmark Performance Script #
# © 2010 Code24 BV #
# #
# Author : Alessandro Torrisi #
# Company : Code24 BV, The Netherlands #
# Date : July 31, 2010 #
# version : 1.0 #
# License : Creative Commons CC-BY license #
# Website : http://www.php-benchmark-script.com #
# #
##########################################################################
*/
function test_Math($count = 140000) {
$time_start = microtime(true);
$mathFunctions = array("abs", "acos", "asin", "atan", "bindec", "floor", "exp", "sin", "tan", "pi", "is_finite", "is_nan", "sqrt");
foreach ($mathFunctions as $key => $function) {
if (!function_exists($function)) unset($mathFunctions[$key]);
}
for ($i=0; $i < $count; $i++) {
foreach ($mathFunctions as $function) {
$r = call_user_func_array($function, array($i));
}
}
return number_format(microtime(true) - $time_start, 3);
}
function test_StringManipulation($count = 130000) {
$time_start = microtime(true);
$stringFunctions = array("addslashes", "chunk_split", "metaphone", "strip_tags", "md5", "sha1", "strtoupper", "strtolower", "strrev", "strlen", "soundex", "ord");
foreach ($stringFunctions as $key => $function) {
if (!function_exists($function)) unset($stringFunctions[$key]);
}
$string = "the quick brown fox jumps over the lazy dog";
for ($i=0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($string));
}
}
return number_format(microtime(true) - $time_start, 3);
}
function test_Loops($count = 19000000) {
$time_start = microtime(true);
for($i = 0; $i < $count; ++$i);
$i = 0; while($i < $count) ++$i;
return number_format(microtime(true) - $time_start, 3);
}
function test_IfElse($count = 9000000) {
$time_start = microtime(true);
for ($i=0; $i < $count; $i++) {
if ($i == -1) {
} elseif ($i == -2) {
} else if ($i == -3) {
}
}
return number_format(microtime(true) - $time_start, 3);
}
$total = 0;
$functions = get_defined_functions();
$line = str_pad("-",38,"-");
echo "<pre>$line\n|".str_pad("PHP BENCHMARK SCRIPT",36," ",STR_PAD_BOTH)."|\n$line\nStart : ".date("Y-m-d H:i:s")."\nServer : {$_SERVER['SERVER_NAME']}#{$_SERVER['SERVER_ADDR']}\nPHP version : ".PHP_VERSION."\nPlatform : ".PHP_OS. "\n$line\n";
foreach ($functions['user'] as $user) {
if (preg_match('/^test_/', $user)) {
$total += $result = $user();
echo str_pad($user, 25) . " : " . $result ." sec.\n";
}
}
echo str_pad("-", 38, "-") . "\n" . str_pad("Total time:", 25) . " : " . $total ." sec.</pre>";
?>
Now on one server I'm getting an average of about 10, and on another 15 (so far so good), but on a third, the average is like 45 seconds. It's strange because that server has a working Wordpress install that runs pretty quickly (about 1.5 second page load times).
My question is, why might this server be showing me such high results, but seems to be working fine? And second, does this mean that this isn't a good method to determine which host would ultimately run Wordpress the fastest (all other things being equal)? And if this isn't a good method, do you have any suggestions?

You need to actually install wordpress to know for sure.
I would just edit the index.php file of each and require this file.
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* #package WordPress
*/
require('benchmark.php');
/**
* Tells WordPress to load the WordPress theme and output it.
*
* #var bool
*/
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
I've done benchmarking for a while and this will give you good information about the current memory usage and processing speed of the server. You could even hit the server with httperf a few hundred times to get TRUE server ability.

You definitely have to install wordpress in order to analyze the performance in each one. In fact, you should have the complete wordpress environment installed in all three, with this I mean, all wordpress plugins should be installed, because they could have a big impact in performance. Also apache, mysql and php should be tuned the same in all three servers.
Once you do that, you can use jMeter (http://jmeter.apache.org/) to really test your server, and see how many requests can your server serve.

Related

Large processes in PhalconPHP

I have webapp that is logging application and I need backup/restore/import/export feature there. I did this successfully with laravel but have some complications with Phalcon. I don't see native functions in phalcon that would split on chunks execution of large php scripts.
The thing is that logs will be backed up and restored as well as imported by users in ADIF format (adif.org) I have parser for that format which converts file to array of arrays then every record should search through another table, containing 2000 regular expressions, and find 3-10 matches there and connect imported records in one table to those in another table (model relation hasMany) That means that every imported record should have quite some processing time. laravel did it somehow with 3500 records imported, I dont know how it will handle more. The average import will contain 10000 records and each of them need to be verified with 2000 regular expression.
The main issue is how to split this huge processing mount into smaller chunks so I wouldnt get timeouts?
Here is the function that could flawlessly do the job with adding 3862 records in one table and as a result of processing of every record add 8119 records in another table:
public function restoreAction()
{
$this->view->disable();
$user = Users::findFirst($this->session->auth['id']);
if ($this->request->isPost()) {
if ($this->request->isAjax()) {
$frontCache = new CacheData(array(
"lifetime" => 21600
));
$cache = new CacheFile($frontCache, array(
"cacheDir" => "../plc/app/cache/"
));
$cacheKey = $this->request->getPost('fileName').'.cache';
$records = $cache->get($cacheKey);
if ($records === null) {
$rowsPerChunk = 50;
$adifSource = AdifHelper::parseFile(BASE_URL.'/uploads/'.$user->getUsername().'/'.$this->request->getPost('fileName'));
$records = array_chunk($adifSource, $rowsPerChunk);
$key = array_keys($records);
$size = count($key);
}
for ($i = 0; $i < $size; $i++) {
if (!isset($records[$i])) {
break;
}
set_time_limit(50);
for ($j=0; $j < $rowsPerChunk; $j++) {
$result = $records[$i][$j];
if (!isset($result)) {
break;
}
if(isset($result['call'])) {
$p = new PrefixHelper($result['call']);
}
$bandId = (isset($result['band']) && (strlen($result['band']) > 2)) ? Bands::findFirstByName($result['band'])->getId() : null;
$infos = (isset($p)) ? $p->prefixInfo() : null;
if (is_array($infos)) {
if (isset($result['qsl_sent']) && ($result['qsl_sent'] == 'q')) {
$qsl_rcvd = 'R';
} else if (isset($result['eqsl_qsl_sent']) && ($result['eqsl_qsl_sent'] == 'c')) {
$qsl_rcvd = 'y';
} else if (isset($result['qsl_rcvd'])) {
$qsl_rcvd = $result['qsl_rcvd'];
} else {
$qsl_rcvd ='i';
}
$logRow = new Logs();
$logRow->setCall($result['call']);
$logRow->setDatetime(date('Y-m-d H:i:s',strtotime($result['qso_date'].' '.$result['time_on'])));
$logRow->setFreq(isset($result['freq']) ? $result['freq'] : 0);
$logRow->setRst($result['rst_sent']);
$logRow->setQslnote(isset($result['qslmsg']) ? $result['qslmsg'] : '');
$logRow->setComment(isset($result['comment']) ? $result['comment'] : '');
$logRow->setQslRcvd($qsl_rcvd);
$logRow->setQslVia(isset($result['qsl_sent_via']) ? $result['qsl_sent_via'] : 'e');
$logRow->band_id = $bandId;
$logRow->user_id = $this->session->auth['id'];
$success = $logRow->save();
if ($success) {
foreach ($infos as $info) {
if (is_object($info)) {
$inf = new Infos();
$inf->setLat($info->lat);
$inf->setLon($info->lon);
$inf->setCq($info->cq);
$inf->setItu($info->itu);
if (isset($result['iota'])) {
$inf->setIota($result['iota']);
}
if (isset($result['pfx'])) {
$inf->setPfx($result['pfx']);
}
if (isset($result['gridsquare'])) {
$inf->setGrid($result['gridsquare']);
} else if (isset($result['grid'])) {
$inf->setGrid($result['grid']);
}
$inf->qso_id = $logRow->getId();
$inf->prefix_id = $info->id;
$infSuccess[] = $inf->save();
}
}
}
}
}
sleep(1);
}
}
}
}
I know, the script needs a lot of improvement but for now the task was just to make it work.
I think that the good practice for large processing task in php is console applications, that doesn't have restrictions in execution time and can be setup with more memory for execution.
As for phalcon, it has builtin mechanism for running and processing cli tasks - Command Line Applications (this link will always point to the documentation of a phalcon latest version)

Why is bcmath in php 4.3 so much faster than php 5+?

Decided to run a quick test to see how bcmath operates on various versions of PHP, and noticed that the latest and greatest is lacking significantly in speed when compared to 4.3,
I am wondering if anyone knows what the reason behind this is, and/or how the speed can be improved on 5+ so that it is reasonably comparable to 4.3.
Also note, the memory consumption from 5.6+ is triple that which 4.3 requires for the same operation :
Performance Test Results (Waterflow)
It's not faster. The graphs you see include not only the bcmath call, but the startup & shutdown overhead as well.
$start = microtime(true);
for ($i = 0; $i < 1000; ++$i) {
bcdiv(40075036, 86164.098903691, 40);
}
echo microtime(true) - $start;
This snippet would measure the bcdiv performance: http://3v4l.org/unrRL
As you can see - the performance is pretty much the same.
A note: you can see that the numbers are really small, it means that you cannot completely trust them and you should understand that any additional load on the machine can affect the test results.
Just out of curiosity. I decided I don't need the absurd accuracy of bcmath but do want to save myself from IEEE 754 rounding anomalies and created my own very simple FixedNumber class:
class FixedNumber {
private static $radix = [
0 => 1,
1 => 10,
2 => 100,
3 => 1000,
4 => 10000,
];
private int $decimal_places;
public function __construct(
protected int $value,
int $decimal_places=2
)
{
$this->decimal_places = $decimal_places;
}
public function __toString() {
return sprintf('%.'.$this->decimal_places.'f', $this->value/self::$radix[$this->decimal_places]);
}
public function __toFloat() {
return $this->value/self::$radix[$this->decimal_places];
}
public function __add(FixedNumber $other) {
if ($this->decimal_places != $other->decimal_places) {
throw new Exception('Cannot add FixedNumber with different decimal places');
}
return new FixedNumber($this->value + $other->value, $this->decimal_places);
}
public function __sub(FixedNumber $other) {
if ($this->decimal_places != $other->decimal_places) {
throw new Exception('Cannot subtract FixedNumber with different decimal places');
}
return new FixedNumber($this->value - $other->value, $this->decimal_places);
}
public function __mul(FixedNumber $other) {
if ($this->decimal_places != $other->decimal_places) {
throw new Exception('Cannot multiply FixedNumber with different decimal places');
}
return new FixedNumber((int)round($this->value * $other->value / self::$radix[$this->decimal_places]), $this->decimal_places);
}
public function __div(FixedNumber $other) {
if ($this->decimal_places != $other->decimal_places) {
throw new Exception('Cannot divide FixedNumber with different decimal places');
}
return new FixedNumber((int)round(($this->value / $other->value) * self::$radix[$this->decimal_places]), $this->decimal_places);
}
}
and tried this little test setup:
define("SET_SIZE",1000,false);
$start = microtime(true);
$d = 0.0;
for ($i = 0; $i < SET_SIZE; ++$i) {
$d = 40075036+86164.098903691;
}
printf('%f<br>', microtime(true) - $start);
$start = microtime(true);
for ($i = 0; $i < SET_SIZE; ++$i) {
bcadd("40075036", "86164.098903691", 16);
}
printf('%f<br>', microtime(true) - $start);
$start = microtime(true);
for ($i = 0; $i < SET_SIZE; ++$i) {
$a = new \FixedNumber(40075036);
$b = new \FixedNumber(861600);
$c = $a->__add($b);
}
printf('%f<br>', microtime(true) - $start);
Just to find out that although bcmath is 20 orders slower than normal math, it still outperforms my simple FixedNumber class by 0.11 order. The numbers for a set size of 1.000.000 :
0.009088
0.224396
0.366447
It appears the overhead of creating objects and being in user space makes bcmath a better deal.
It would be nice if fixed point arithmatic would be included natively for money values, school grates and the like, where floating point numbers are just wrong.

Advanced PHP: Allow a unique URL to be clicked by the first three IPs only

I am selling a subscription viewing service. Once people have paid they get a unique URL e-mailed to them. The link is set to expire after a certain time but I'd like to only allow the first three IP addresses to use the link before it expires to stop piracy. I'm doing it like this to avoid having yet another database running holding thousands of logins. I assume I can write to a directory and have a filename as the suffix of the link (zFZpj4b2AkEFz%2B3O in this case) with up to three IPs listed in the file.
It all works well so far barring the IP address counting and the unique link the e-mail looks like this:
http://www.blah.com/download.php?file=zFZpj4b2AkEFz%2B3O
The file download.php looks like this:
<?
$time = time();
include('settings.php');
class RC4Crypt {
/**
* Encrypt the data.
* #param string private key.
* #param string data to be encrypted.
* #return string encrypted string.
*/
function encrypt ($pwd, $data)
{
$key[] = '';
$box[] = '';
$pwd_length = strlen($pwd);
$data_length = strlen($data);
for ($i = 0; $i < 256; $i++)
{
$key[$i] = ord($pwd[$i % $pwd_length]);
$box[$i] = $i;
}
for ($j = $i = 0; $i < 256; $i++)
{
$j = ($j + $box[$i] + $key[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
$cipher = '';
for ($a = $j = $i = 0; $i < $data_length; $i++)
{
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$k = $box[(($box[$a] + $box[$j]) % 256)];
$cipher .= chr(ord($data[$i]) ^ $k);
}
return ($cipher);
}
/**
* Decrypt the data.
* #param string private key.
* #param string cipher text (encrypted text).
* #return string plain text.
*/
function decrypt ($pwd, $data)
{
return RC4Crypt::encrypt($pwd, ($data));
}
}
if(!isset($_GET['file']) || empty($_GET['file'])) {
echo 'Invalid Request';
return;
}
$data = $_GET['file'];
$id_time = RC4Crypt::decrypt($secret,base64_decode(rawurldecode($data)));
list($product_id,$timestamp) = explode('|',$id_time);
if(!isset($products[$product_id])) {
echo 'Invalid Request';
return;
}
if ($timestamp < $time - ($download_life * 60 )) {
echo 'Link Expired';
return;
}
if(isset($products[$product_id])) {
print ("<html><head><meta http-equiv=Refresh content=\"0;URL=http://www.blah.com/view/\"></head><body></html>");
return;
}
?>
Can any kind soul take pity on someone who has spent far too long looking at this already please ? :) Thanks very much.
--EDIT --
A thought: Forgetting the 3 IPs what about storing a Server-side cookie when the link is pressed the first time and denying access if it exists ?
To do this, you have to create a table for each subscription.
table subscription: subId, subCode, subVisitTimes, subVisitedIP
subCode will be something like zFZpj4b2AkEFz%2B3O
for each visit, you get client's IP using $_SERVER['REMOTE_ADDR'].
If it does exist in subVisitedIP then allow access.
If it does not exist then check subVisitTimes value:
if subVisitTimes = 3 then deny access
If subVisitTimes < 3 then allow access and increase its value by one also add client's IP to subVisitedIP (you should use serialize function to store array of three IPs).
You're going to want to set up a simple database for this. You only need one row - the hash/id, the original IP, expired, etc and can simply set expired to 1 when access runs out. This way you're not running costly DELETE queries, and if need be you can simply delete those rows all at once a couple times a month to save space.
Otherwise it's going to get too complex and more error-prone using flatfiles.

Is there a way to tell Drupal not to cache a specific page?

I have a custom php page that processes a feed of images and makes albums out of it. However whenever i add pictures to my feed, the Drupal page doesn't change until I clear the caches.
Is there a way to tell Drupal not to cache that specific page?
Thanks,
Blake
Edit: Drupal v6.15
Not exactly sure what you mean oswald, team2648.com/media is hte page.
I used the php interpreter module. Here is the php code:
<?php
//////// CODE by Pikori Web Designs - pikori.org ///////////
//////// Please do not remove this title, ///////////
//////// feel free to modify or copy this software ///////////
$feedURL = 'http://picasaweb.google.com/data/feed/base/user/Techplex.Engineer?alt=rss&kind=album&hl=en_US';
$photoNodeNum = 4;
$galleryTitle = 'Breakaway Pictures';
$year = '2011';
?>
<?php
/////////////// DO NOT EDIT ANYTHING BELOW THIS LINE //////////////////
$album = $_GET['album'];
if($album != ""){
//GENERATE PICTURES
$feedURL= "http://".$album."&kind=photo&hl=en_US";
$feedURL = str_replace("entry","feed",$feedURL);
$sxml = simplexml_load_file($feedURL);
$column = 0;
$pix_count = count($sxml->channel->item);
//print '<h2>'.$sxml->channel->title.'</h2>';
print '<table cellspacing="0" cellpadding="0" style="font-size:10pt" width="100%"><tr>';
for($i = 0; $i < $pix_count; $i++) {
print '<td align="center">';
$entry = $sxml->channel->item[$i];
$picture_url = $entry->enclosure['url'];
$time = $entry->pubDate;
$time_ln = strlen($time)-14;
$time = substr($time,0,$time_ln);
$description = $entry->description;
$tn_beg = strpos($description, "src=");
$tn_end = strpos($description, "alt=");
$tn_length = $tn_end - $tn_beg;
$tn = substr($description, $tn_beg, $tn_length);
$tn_small = str_replace("s288","s128",$tn);
$picture_url = $tn;
$picture_beg = strpos($picture_url,"http:");
$picture_len = strlen($picture_url)-7;
$picture_url = substr($tn, $picture_beg, $picture_len);
$picture_url = str_replace("s288","s640",$picture_url);
print '<a rel="lightbox[group]" href="'.$picture_url.'">';
print '<img '.$tn_small.' style="border:1px solid #02293a"><br>';
print '</a></td> ';
if($column == 4){ print '</tr><tr>'; $column = 0;}
else $column++;
}
print '</table>';
print '<br><center>Return to album</center>';
} else {
//GENERATE ALBUMS
$sxml = simplexml_load_file($feedURL);
$column = 0;
$album_count = count($sxml->channel->item);
//print '<h2>'.$galleryTitle.'</h2>';
print '<table cellspacing="0" cellpadding="0" style="font-size:10pt" width="100%"><tr>';
for($i = 0; $i < $album_count; $i++) {
$entry = $sxml->channel->item[$i];
$time = $entry->pubDate;
$time_ln = strlen($time)-14;
$time = substr($time,0,$time_ln);
$description = $entry->description;
$tn_beg = strpos($description, "src=");
$tn_end = strpos($description, "alt=");
$tn_length = $tn_end - $tn_beg;
$tn = substr($description, $tn_beg, $tn_length);
$albumrss = $entry->guid;
$albumrsscount = strlen($albumrss) - 7;
$albumrss = substr($albumrss, 7, $albumrsscount);
$search = strstr($time, $year);
if($search != FALSE || $year == ''){
print '<td valign="top">';
print '<a href="/node/'.$photoNodeNum.'?album='.$albumrss.'">';
print '<center><img '.$tn.' style="border:3px double #cccccc"><br>';
print $entry->title.'<br>'.$time.'</center>';
print '</a><br></td> ';
if($column == 3){
print '</tr><tr>'; $column = 0;
} else {
$column++;
}
}
}
print '</table>';
}
?>
Thanks for your answer.
The site you linked gave me some new verbiage to search with, and thus I found this:
http://www.drupalcoder.com/story/365-disable-drupals-page-cache-for-some-pages
which then led me to this:
http://drupal.org/project/cacheexclude
which did exactly what I wanted.
Hope this helps.
That is for Drupal 6.
This doesn't answer your specific question about caching, but -- consider using Drupal-native solutions like the Picasa module for things like this.
When you use non-Drupal PHP applications in a Drupal environment like you have here, you get weird interactions with other Drupal components. Drupal modules are build with Drupal in mind, so things like sane caching usually come built in.
Write this code line at the top of the custom page you dont want to be cached:
$GLOBALS['conf']['cache'] = FALSE;
For Drupal 6,
If you just want to exclude a specific page from being cached then you can use Cache exclude module, where you just have to provide a url.
for drupal 7 also it is available but its in the development version.
Yes you can do it programmatically and the below code is valid for Drupal 6 and 7 both.
Reference : http://techrappers.com/post/27/how-prevent-javascript-and-css-render-some-drupal-pages
/**
* Implements hook_init().
*/
function MODULE_NAME_init() {
global $conf;
// current_path() is the path on which you want to turn of JS or CSS cache
if (current_path() == 'batch' || current_path() == 'library-admin') {
// If you want to force CSS or JS cache to turned off
$conf['preprocess_js'] = FALSE;
$conf['preprocess_css'] = FALSE;
// If you want normal caching to turned off
drupal_page_is_cacheable(FALSE);
}
}
Please Note that drupal_page_is_cacheable(FALSE); can only turn off normal caching, it will not force JS caching to be turned off automatically unless you use $conf['preprocess_js'] = FALSE.

Tracking the script execution time in PHP

PHP must track the amount of CPU time a particular script has used in order to enforce the max_execution_time limit.
Is there a way to get access to this inside of the script? I'd like to include some logging with my tests about how much CPU was burnt in the actual PHP (the time is not incremented when the script is sitting and waiting for the database).
I am using a Linux box.
If all you need is the wall-clock time, rather than the CPU execution time, then it is simple to calculate:
//place this before any script you want to calculate time
$time_start = microtime(true);
//sample script
for($i=0; $i<1000; $i++){
//do anything
}
$time_end = microtime(true);
//dividing with 60 will give the execution time in minutes otherwise seconds
$execution_time = ($time_end - $time_start)/60;
//execution time of the script
echo '<b>Total Execution Time:</b> '.$execution_time.' Mins';
// if you get weird results, use number_format((float) $execution_time, 10)
Note that this will include the time that PHP is sat waiting for external resources such as disks or databases, which is not used for max_execution_time.
On unixoid systems (and in php 7+ on Windows as well), you can use getrusage, like:
// Script start
$rustart = getrusage();
// Code ...
// Script end
function rutime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
$ru = getrusage();
echo "This process used " . rutime($ru, $rustart, "utime") .
" ms for its computations\n";
echo "It spent " . rutime($ru, $rustart, "stime") .
" ms in system calls\n";
Note that you don't need to calculate a difference if you are spawning a php instance for every test.
Shorter version of talal7860's answer
<?php
// At start of script
$time_start = microtime(true);
// Anywhere else in the script
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start);
As pointed out, this is 'wallclock time' not 'cpu time'
<?php
// Randomize sleeping time
usleep(mt_rand(100, 10000));
// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.
// It contains the timestamp of the start of the request with microsecond precision.
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Did nothing in $time seconds\n";
?>
I created an ExecutionTime class out of phihag answer that you can use out of box:
class ExecutionTime
{
private $startTime;
private $endTime;
public function start() {
$this->startTime = getrusage();
}
public function end() {
$this->endTime = getrusage();
}
private function runTime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
public function __toString() {
return "This process used " . $this->runTime($this->endTime, $this->startTime, "utime") .
" ms for its computations\nIt spent " . $this->runTime($this->endTime, $this->startTime, "stime") .
" ms in system calls\n";
}
}
Usage:
$executionTime = new ExecutionTime();
$executionTime->start();
// Code
$executionTime->end();
echo $executionTime;
Note: In PHP 5, the getrusage function only works in Unix-oid systems. Since PHP 7, it also works on Windows.
It is going to be prettier if you format the seconds output like:
echo "Process took ". number_format(microtime(true) - $start, 2). " seconds.";
will print
Process took 6.45 seconds.
This is much better than
Process took 6.4518549156189 seconds.
Gringod at developerfusion.com gives this good answer:
<!-- put this at the top of the page -->
<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
;?>
<!-- put other code and html in here -->
<!-- put this code at the bottom of the page -->
<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "This page was created in ".$totaltime." seconds";
;?>
From (http://www.developerfusion.com/code/2058/determine-execution-time-in-php/)
To show minutes and seconds you can use:
$startTime = microtime(true);
$endTime = microtime(true);
$diff = round($endTime - $startTime);
$minutes = floor($diff / 60); // Only minutes
$seconds = $diff % 60; // Remaining seconds, using modulo operator
echo "script execution time: minutes:$minutes, seconds:$seconds"; // Value in seconds
The cheapest and dirtiest way to do it is simply make microtime() calls at places in your code you want to benchmark. Do it right before and right after database queries and it's simple to remove those durations from the rest of your script execution time.
A hint: your PHP execution time is rarely going to be the thing that makes your script timeout. If a script times out it's almost always going to be a call to an external resource.
PHP microtime documentation:
http://us.php.net/microtime
I think you should look at xdebug. The profiling options will give you a head start toward knowing many process related items.
http://www.xdebug.org/
$_SERVER['REQUEST_TIME']
check out that too. i.e.
...
// your codes running
...
echo (time() - $_SERVER['REQUEST_TIME']);
when there is closure functionality in PHP, why not we get benefit out of it.
function startTime(){
$startTime = microtime(true);
return function () use ($startTime){
return microtime(true) - $startTime;
};
}
Now with the help of the above function, we can track time like this
$stopTime = startTime();
//some code block or line
$elapsedTime = $stopTime();
Every call to startTime function will initiate a separate time tracker. So you can initiate as many as you want and can stop them wherever you want them.
Small script that print, centered in bottom of the page, the script execution that started at server call with microsecond precision.
So as not to distort the result and to be 100% compatible with content in page, I used, to write the result on the page, a browser-side native javascript snippet.
//Uncomment the line below to test with 2 seconds
//usleep(2000000);
$prec = 5; // numbers after comma
$time = number_format(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], $prec, '.', '');
echo "<script>
if(!tI) {
var tI=document.createElement('div');
tI.style.fontSize='8px';
tI.style.marginBottom='5px';
tI.style.position='absolute';
tI.style.bottom='0px';
tI.style.textAlign='center';
tI.style.width='98%';
document.body.appendChild(tI);
}
tI.innerHTML='$time';
</script>";
Another approach is to make the snippet as small as possible, and style it with a class in your stylesheet.
Replace the echo ...; part with the following:
echo "<script>if(!tI){var tI=document.createElement('div');tI.className='ldtme';document.body.appendChild(tI);}tI.innerHTML='$time';</script>";
In your CSS create and fill the .ldtme{...} class.
I wrote a function that check remaining execution time.
Warning: Execution time counting is different on Windows and on Linux platform.
/**
* Check if more that `$miliseconds` ms remains
* to error `PHP Fatal error: Maximum execution time exceeded`
*
* #param int $miliseconds
* #return bool
*/
function isRemainingMaxExecutionTimeBiggerThan($miliseconds = 5000) {
$max_execution_time = ini_get('max_execution_time');
if ($max_execution_time === 0) {
// No script time limitation
return true;
}
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// On Windows: The real time is measured.
$spendMiliseconds = (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) * 1000;
} else {
// On Linux: Any time spent on activity that happens outside the execution
// of the script such as system calls using system(), stream operations
// database queries, etc. is not included.
// #see http://php.net/manual/en/function.set-time-limit.php
$resourceUsages = getrusage();
$spendMiliseconds = $resourceUsages['ru_utime.tv_sec'] * 1000 + $resourceUsages['ru_utime.tv_usec'] / 1000;
}
$remainingMiliseconds = $max_execution_time * 1000 - $spendMiliseconds;
return ($remainingMiliseconds >= $miliseconds);
}
Using:
while (true) {
// so something
if (!isRemainingMaxExecutionTimeBiggerThan(5000)) {
// Time to die.
// Safely close DB and done the iteration.
}
}
Further expanding on Hamid's answer, I wrote a helper class that can be started and stopped repeatedly (for profiling inside a loop).
class ExecutionTime
{
private $startTime;
private $endTime;
private $compTime = 0;
private $sysTime = 0;
public function Start() {
$this->startTime = getrusage();
}
public function End() {
$this->endTime = getrusage();
$this->compTime += $this->runTime($this->endTime, $this->startTime, "utime");
$this->systemTime += $this->runTime($this->endTime, $this->startTime, "stime");
}
private function runTime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000)) - ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
public function __toString() {
return "This process used " . $this->compTime . " ms for its computations\n" . "It spent " . $this->systemTime . " ms in system calls\n";
}
}
You may only want to know the execution time of parts of your script. The most flexible way to time parts or an entire script is to create 3 simple functions (procedural code given here but you could turn it into a class by putting class timer{} around it and making a couple of tweaks). This code works, just copy and paste and run:
$tstart = 0;
$tend = 0;
function timer_starts()
{
global $tstart;
$tstart=microtime(true); ;
}
function timer_ends()
{
global $tend;
$tend=microtime(true); ;
}
function timer_calc()
{
global $tstart,$tend;
return (round($tend - $tstart,2));
}
timer_starts();
file_get_contents('http://google.com');
timer_ends();
print('It took '.timer_calc().' seconds to retrieve the google page');
Just to contribute to this conversation:
what happens if the measurement targets two points A and B in different php files?
what if we need different measurements like time based, code execution duration, external resource access duration?
what if we need to organize our measurements in categories where every one has a different starting point?
As you suspect we need some global variables to be accessed by a class object or a static method: I choose the 2nd approach and here it is:
namespace g3;
class Utils {
public function __construct() {}
public static $UtilsDtStart = [];
public static $UtilsDtStats = [];
public static function dt() {
global $UtilsDtStart, $UtilsDtStats;
$obj = new \stdClass();
$obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
$UtilsDtStart[$ndx] = \microtime(true) * 1000;
};
$obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
$use = \getrusage();
$UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
};
$obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
$use = \getrusage();
$UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
};
$obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
$t = #$UtilsDtStart[$ndx];
if($t === null)
return false;
$end = \microtime(true) * 1000;
$dt = $end - $t;
$UtilsDtStats[$ndx][] = $dt;
return $dt;
};
$obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
$t = #$UtilsDtStart[$ndx];
if($t === null)
return false;
$use = \getrusage();
$dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
$UtilsDtStats[$ndx][] = $dt;
return $dt;
};
$obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
$t = #$UtilsDtStart[$ndx];
if($t === null)
return false;
$use = \getrusage();
$dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
$UtilsDtStats[$ndx][] = $dt;
return $dt;
};
$obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
$s = #$UtilsDtStats[$ndx];
if($s !== null)
$s = \array_slice($s, 0);
else
$s = false;
return $s;
};
$obj->statsLength = function() use (&$UtilsDtStats) {
return \count($UtilsDtStats);
};
return $obj;
}
}
Now all you have is to call the method that belongs to the specific category with the index that denotes it's unique group:
File A
------
\call_user_func_array(\g3\Utils::dt()->start, [0]); // point A
...
File B
------
$dt = \call_user_func_array(\g3\Utils::dt()->end, [0]); // point B
Value $dt contains the milliseconds of wall clock duration between points A and B.
To estimate the time took for php code to run:
File A
------
\call_user_func_array(\g3\Utils::dt()->codeStart, [1]); // point A
...
File B
------
$dt = \call_user_func_array(\g3\Utils::dt()->codeEnd, [1]); // point B
Notice how we changed the index that we pass at the methods.
The code is based on the closure effect that happens when we return an object/function from a function (see that \g3\Utils::dt() repeated at the examples).
I tested with php unit and between different test methods at the same test file it behaves fine so far!
Hope that helps someone!
As an alternative you can just put this line in your code blocks and check php logs, for really slow functions it's pretty useful:
trigger_error("Task done at ". strftime('%H:%m:%S', time()), E_USER_NOTICE);
For serious debugging use XDebug + Cachegrind, see https://blog.nexcess.net/2011/01/29/diagnosing-slow-php-execution-with-xdebug-and-kcachegrind/
There are several way of doing this, listed here. But each have their own pro's and con's. And (in my opinion) the readability of all longer answers are terrible.
So I decided to put it all together in one answer, that is easily usable and readable.
Usage
$start = get_timers();
for( $i = 0; $i < 100000; $i++ ){
// Code to check
}
$end = get_timers();
display_timer_statistics( $start, $end );
Function definitions
function display_timer_statistics( $start_timers, $end_timers ){
// Settings
$key_width = '100px';
$decimals = 4;
$decimals_wallclock = $decimals;
$decimals_request_time_float = $decimals;
// Variables
$start_resource_usage_timer = $start_timers[0];
$start_wallclock = $start_timers[1];
$end_resource_usage_timer = $end_timers[0];
$end_wallclock = $end_timers[1];
// # User time
// Add seconds and microseconds for the start/end, and subtract from another
$end_user_time_seconds = $end_resource_usage_timer["ru_utime.tv_sec"]*1000;
$end_user_time_microseconds = intval($end_resource_usage_timer["ru_utime.tv_usec"]/1000);
$start_user_time_seconds = $start_resource_usage_timer["ru_utime.tv_sec"]*1000;
$start_user_time_microseconds = intval($start_resource_usage_timer["ru_utime.tv_usec"]/1000);
$total_user_time = ($end_user_time_seconds + $end_user_time_microseconds) - ($start_user_time_seconds + $start_user_time_microseconds);
// # System time
// Add seconds and microseconds for the start/end, and subtract from another
$end_system_time_seconds = $end_resource_usage_timer["ru_stime.tv_sec"]*1000;
$end_system_time_microseconds = intval($end_resource_usage_timer["ru_stime.tv_usec"]/1000);
$start_system_time_seconds = $start_resource_usage_timer["ru_stime.tv_sec"]*1000;
$start_system_time_microseconds = intval($start_resource_usage_timer["ru_stime.tv_usec"]/1000);
$total_system_time = ($end_system_time_seconds + $end_system_time_microseconds) - ($start_system_time_seconds + $start_system_time_microseconds);
// Wallclock
$total_wallclock_time = number_format( ( $end_wallclock - $start_wallclock), $decimals_wallclock );
// Server request_time_float
$request_time_float = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
$request_time_float = number_format( $request_time_float, $decimals_request_time_float );
// Print
$span_start = "<span style='width: $key_width; display: inline-block;'>";
$span_end = "</span>";
$output = "# RUNTIME AND TIMERS " . PHP_EOL ;
$output .= PHP_EOL;
$output .= $span_start . $total_user_time . $span_end . " User time (utime)" . PHP_EOL;
$output .= $span_start . $total_system_time . $span_end . " System time (stime)" . PHP_EOL;
$output .= PHP_EOL;
$output .= $span_start . $total_wallclock_time . $span_end . " Wallclock" . PHP_EOL;
$output .= PHP_EOL;
$output .= $span_start . $request_time_float . $span_end . " REQUEST_TIME_FLOAT" . PHP_EOL . PHP_EOL . PHP_EOL;
echo nl2br( $output );
}
function get_timers(){
return [ getrusage(), microtime( true ) ];
}
Glossary
All gotten from PHP docs for getrusage
Wallclock = How long it takes
ru = Resource usage
utime = User time used
stime = System time used
tv_sec = In seconds.
tv_usec = In microseconds.
tv = ?? Dunno
return microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];

Categories