PHP Classes

MycroBench: Measure the time that passed since a past moment

Recommend this page to a friend!
     
  Info   Demos   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 33 This week: 1All time: 11,057 This week: 41Up
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: 6 packages by
Country: Germany Germany
Age: 44
All time rank: 4120220 in Germany Germany
Week rank: 3 Up1 in Germany Germany Up
Innovation award
Innovation award
Nominee: 2x

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 ./public/index.php for examples or check the livedemo on Vercel.

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 (8)  
File Role Description
Files folder imageapi (1 file)
Files folder imagepublic (2 files)
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
Accessible without login Plain text file vercel.json Data Auxiliary data

  Files folder image Files (8)  /  api  
File Role Description
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files (8)  /  public  
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 (8)  /  src  
File Role Description
  Plain text file MycroBench.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:33
This week:1
All time:11,057
This week:41Up
User Comments (1)