PHP Classes
elePHPant
Icontem

Kronos: Calculate the time PHP code takes to execute

Recommend this page to a friend!
  Info   View files Documentation   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-03-16 (5 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 41 All time: 9,295 This week: 497Up
Version License PHP version Categories
emoretti_kronos 1.0MIT/X Consortium ...5PHP 5, Time and Date, Statistics, Debug, P...
Description Author

This package can calculate the time PHP code takes to execute.

It takes note of the time that each code segment starts and ends so it can calculate in the end how long it passed since the beginning of all check points with the accuracy of 1 microsecond.

The package outputs a report to a HTML page showing the times that passed between different code segments.

  Performance   Level  
Name: Ettore Moretti <contact>
Classes: 12 packages by
Country: Italy Italy
Innovation award
Innovation award
Nominee: 3x

Details

Kronos - calculate the execution time of a page and code blocks for PHP

Kronos is a PHP library, which allows to calculate the execution time of a page and code blocks (called CheckPoint), with an accuracy up to 1 ?s

Installation

Install the latest version with

$ composer require emoretti/kronos ^dev-master

Report Example

alt text

Configuration

no configuration needed

Basic Usage

Create an instance of Kronos() class in the start of your page, passing the page "alias" name and optionally the initial float timestamp (If not passed kronos calculate for you the timestamp).<br /> At the end of page call the method setMainEnd() optionally passing the ending float timestamp (If not passed kronos calculate for you the timestamp).<br /> Finally you can call the method getReport() to render under your page a final report, or call the method getReportRaw() to get the array with all the data.<br /> The getReport() accept in input a dateFormat in PHP style,

<?php
	
	use emoretti\kronos\Kronos;

	$myKronos = new Kronos("pageAlias"); 


	[... YOUR CODE ...]


	[... YOUR CODE ...]

	
	$myKronos->setMainEnd(microtime(true));


	$myKronos->getReport(); // OR $myKronos->getReportRaw();
?>

Use of checkpoints

After declare the instance of Kronos()<br /><br />

In the middle of your code (where you want), you can insert a checkpoint, which allows you to create other statistics.<br /> To create a checkpoint use the methods startCheckPoints() and stopCheckPoints() passing the checkpoint alias (it will be the key of your checkpoint), and optionally the float timestamp (If not passed kronos calculate for you the timestamp).<br />

<?php
	
	use emoretti\kronos\Kronos;

	$myKronos = new Kronos("pageAlias"); 


	[... YOUR CODE ...]
	
	$myKronos->startCheckPoints("FirstCheckPoint",microtime(true));

	usleep(mt_rand(500, 3000));

	$myKronos->stopCheckPoints("FirstCheckPoint",microtime(true));

	[... YOUR CODE ...]

	
	$myKronos->setMainEnd(microtime(true));


	$myKronos->getReport(); // OR $myKronos->getReportRaw();

?>

Kronos Results

Kronos results can be:

1. Rendered in bottom of the examined page

		$myKronos->getReport();

2. Returned in array structure (or json if you pass the first argument true) `php \\Array structure output

	$myKronos->getReportRaw();
	\\Json structure output
	$myKronos->getReportRaw(true);
```

3. Saved in a file (the file will have a json inside)

		$myKronos->saveReportData(__DIR__ . "/report.json");
<br /><br />If you save data for get the report later, you can render that use the static method `Kronos::renderReport()` passing $reportName and $reportData	
```php
	use emoretti\kronos\Kronos;
	require_once("src/Kronos.php"); 
	Kronos::renderReport("TestReport", file_get_contents(__DIR__."/report.json"));
```

Kronos Render

Kronos will render the data through is template engine KronosTemplate(), by default the template is: src/template/KronosTemplate.php (it use Bootstrap.min.css) , you can modify it as you want ( Be careful not to change the variable names in the template)

You can specify your prefered date format for those methods:

  1. getReport ( $dateFormat = "d/m/Y H:i:s:u" )
  2. getReportRaw ( $json= false , $dateFormat = "d/m/Y H:i:s:u" )
  3. renderReport( $name , $data , $dateFormat='d/m/Y H:i:s:u' )

Kronos time execution

Kronos will try automatically to determine his execution time, it will be presented automatically in the final report, may be usefull to determine how time it is used by the class itself.<br /> N.B. Kronos attempts to calculate its execution time. But these times are to be considered as APPROXIMATE. Remember that : 0.001 ms == 1 µs (In 1 ?s the light runs exactly 299.792458 meters).

Author

Ettore Moretti - <info@ettoremoretti.com> - <https://twitter.com/emoretticom> - <https://www.facebook.com/emoretticom/>

License

href-count is licensed under the MIT License - see the LICENSE file for details

  Files folder image Files  
File Role Description
Files folder imagesrc (3 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Example script

  Files folder image Files  /  src  
File Role Description
Files folder imagetemplate (2 files)
  Plain text file Kronos.php Class Class source
  Plain text file KronosReport.php Class Class source
  Plain text file KronosTemplate.php Class Class source

  Files folder image Files  /  src  /  template  
File Role Description
  Accessible without login Plain text file bootstrap.min.css Data Auxiliary data
  Accessible without login Plain text file KronosTemplate.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:41
This week:0
All time:9,295
This week:497Up