PHP Classes

MycroBench: Measure the time that passed since a past moment

Recommend this page to a friend!
  Info   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-10-10 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 26 This week: 1All time: 10,692 This week: 491Up
Version License PHP version Categories
mycrobench 1.0.3MIT/X Consortium ...7.4Time and Date, Performance and optimi..., P...
Description Author

This class can measure the time that has passed since a past moment.

It can get the current time and calculate the difference between the time that passed since the initial moment the class started keeping track of the time or since the last moment the course took note of the time.

The class can return an array that specifies the current time, the reference moment time, the timestamp for the present time, and the number of seconds that have passed since the reference moment.

Picture of Engin Ypsilon
Name: Engin Ypsilon <contact>
Classes: 4 packages by
Country: Germany Germany
Age: 42
All time rank: 4236225 in Germany Germany
Week rank: 587 Up32 in Germany Germany Up
Innovation award
Innovation award
Nominee: 1x

Details

MycroBench | Lightweight time measure tool for PHP

This Package uses $_SERVER['REQUEST_TIME_FLOAT'] and microtime() to calculate the difference that has passed between both times, when instantiated. It also provides a basic implementation of hrtime(), which allows measuring processing times with higher precision.

See ./examples/mycrobench for examples.

composer require eypsilon/MycroBench

use Many\MycroBench;

/
 * @return array basic usage
 */
MycroBench::get()

// Array
// (
//     [start] => 2022-10-10 16:07:00.172129
//     [ended] => 2022-10-10 16:07:00.302257
//     [took] => 00.130128
//     [mem_usage] => 376.21 KB
//     [mem_peak] => 16.37 MB
//     [included_files_total] => 37
// )

Consecutive requests

Get the processing time that has passed between consecutive requests with internally staid start times.

The first request will use $_SERVER['REQUEST_TIME_FLOAT'] as start time, subsequent requests will use the ended time from previous requests.

Set the initial start time for benches manually to now. This is optional, but the first high resolution time will be wrong, if it's not instantiated.

/ Sets the start time for @method getBench() and @method hrBench() to now */
MycroBench::initBench()

/ @param bool true returns additional high resolution times */
MycroBench::getBench(true)

// [start] => 2022-10-10 16:07:00.176997
// [ended] => 2022-10-10 16:07:00.217398
// [took] => 00.040401
// [h_res] => 0.040415356

MycroBench::getBench(true)

// [start] => 2022-10-10 16:07:00.217398
// [ended] => 2022-10-10 16:07:00.239846
// [took] => 00.022448
// [h_res] => 0.022439554

MycroBench::getBench(true)

// [start] => 2022-10-10 16:07:00.239846
// [ended] => 2022-10-10 16:07:00.265409
// [took] => 00.025563
// [h_res] => 0.025560282

High resolution times

Get microtimes with the system's high resolution time using hrtime().

Microsec: 00.019045 High Reso: 0.019044332

// start bench
MycroBench::hrStart()

// do stuff here

// end bench, set first param true to get the result in return
MycroBench::hrEnd(false)

/ @return float|null '0.019044332' */
MycroBench::hrGet()

Get high resolution times with internally staid start times.

/ @return float|null '0.019044332' */
MycroBench::hrBench()

Misc

/
 * microtimestamp to datetime
 *
 * @return string '2022-09-01 17:14:48.5000'
 */
(new MycroBench)->getDate('1662045288.5000') # 'U.u', 'Y-m-d H:i:s.u'

/
 * datetime to microtimestamp
 *
 * @return string '1662045288.5000'
 */
(new MycroBench)->getDateToMicro('2022-09-01 17:14:48.5000') #'Y-m-d H:i:s.u', 'U.u'

/
 * Get microtimestamp for the last request. Only filled
 * when @method getBench() has been called
 *
 * @return string|null '1662623500.7135'
 */
MycroBench::getLastDate()

/
 * Readable bytes
 *
 * @return string '379.67 KB'
 */
MycroBench::readableBytes(memory_get_usage())

/
 * Default getter
 *
 * start: $_SERVER['REQUEST_TIME_FLOAT']
 * ended: microtime(true)
 *
 * @static alias MycroBench::get()
 *
 * @param bool   enable (array) get_included_files()
 * @param string set a needle to shorten the paths returned in get_included_files()
 * @return array
 */
(new MycroBench)->getAll(true, '/remove/from/included_files/paths')

/
 * Consecutive getter
 *
 * @static alias MycroBench::getBench()
 *
 * @param bool get additionally higher resolution times
 * @return array
 */
(new MycroBench)->getBenchDiff(true)
  Files folder image Files  
File Role Description
Files folder imageexamples (1 directory)
Files folder imagesrc (1 file)
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. Documentation

  Files folder image Files  /  examples  
File Role Description
Files folder imagemycrobench (2 files)

  Files folder image Files  /  examples  /  mycrobench  
File Role Description
  Accessible without login Image file favicon.ico Icon Icon image
  Plain text file index.php Class Class source

  Files folder image Files  /  src  
File Role Description
  Plain text file MycroBench.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:26
This week:1
All time:10,692
This week:491Up
User Comments (1)