PHP Classes

PHP Time Limit Manager: Check if the PHP execution time limit was reached

Recommend this page to a friend!
  Info   View files Example   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 73 This week: 1All time: 10,197 This week: 560Up
Version License PHP version Categories
php_component_time_l 1.0.0GNU Lesser Genera...5PHP 5, Time and Date, Language
Description 

Author

This class was used to check if the PHP execution time limit was reached.

It can take note of the current time when a class object is created.

The class can also check later if the time that passed since the script was started was less than a given limit, as long as that limit value is lower that the time limit set in the max_execution_time option of php.ini PHP configuration file.

Innovation Award
PHP Programming Innovation award nominee
November 2020
Number 2
PHP provides a feature that is used to limit the time that a script can run.

This is useful for instance to prevent that bugs may cause excessive load on the server due to scripts that execute tasks that take too long to finish.

Sometimes it is better to split the tasks in multiple executions of the same PHP script to avoid that each execution exceeds the PHP execution time limit, which by default is 30 seconds.

This class can help determining when a script that runs a long task is close to exceed the PHP execution time limit.

This way, PHP applications can pause the execution of each task and redirect the Web page access that runs that long task, so the next request handled by the same PHP script can resume the execution of that long task where it was paused.

Manuel Lemos
Picture of nvb
  Performance   Level  
Name: nvb <contact>
Classes: 20 packages by
Country: Germany Germany
Age: ???
All time rank: 150195 in Germany Germany
Week rank: 106 Up5 in Germany Germany Up
Innovation award
Innovation award
Nominee: 12x

Winner: 1x

Example

<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2014-07-26
 */

require_once __DIR__ . '/../../vendor/autoload.php';

echo
'starting example' . PHP_EOL;

$startTime = time();

$manager = new Net\Bazzline\Component\TimeLimitManager\TimeLimitManager();
$manager->setBufferInSeconds(1);
$manager->setLimitInSeconds(9);

$scriptRunTimeInSeconds = 9;

for (
$iterator = 0; $iterator < $scriptRunTimeInSeconds; ++$iterator) {
    if (
$manager->isLimitReached()) {
        echo
'error - runtime of ' . $manager->getLimitInSeconds() . ' seconds limit reached' . PHP_EOL;
        exit(
1);
    }

   
$iteratorIsEven = (($iterator % 2) === 0);

    if (
$iteratorIsEven) {
        echo
'Tick ' . PHP_EOL;
    } else {
        echo
'Tack ' . PHP_EOL;
    }

   
sleep(1);
}

$runTimeInSeconds = (time() - $startTime);

echo
'finished example in ' . $runTimeInSeconds . ' seconds' . PHP_EOL;


Details

Time Limit Manager Component for PHP

This component helps you to validate if your script reaches the allowed maximum runtime.

Furthermore, you can set your own runtime limit (as long as it is below the limit in you php.ini).

The build status of the current master branch is tracked by Travis CI: build status Latest stable

The scrutinizer status are: code quality

The versioneye status is: dependencies

Downloads: Downloads this Month

It is also available at openhub.net.

Benefits

  • provides easy setting of runtime limit
  • gives you the advantage to add a buffer before reaching the limit to easy up reacting when limit is reached
  • helps you to set the limit in seconds, minutes or hours (same for the buffer)
  • comes with DependentInterface and AwareInterface

Examples

Install

By Hand

mkdir -p vendor/net_bazzline/php_component_time_limit_manager
cd vendor/net_bazzline/php_component_time_limit_manager
git clone https://github.com/bazzline/php_component_time_limit_manager

With Packagist

composer require net_bazzline/php_component_time_limit_manager:dev-master

Usage

$manager = new Net\Bazzline\Component\TimeLimitManager\TimeLimitManager();
$manager->setBufferInSeconds(1);
$manager->setLimitInSeconds(4);

while (!empty($dataSet)) {
    if ($manager->isLimitReached()) {
        //exit while loop, shutdown process
    } else {
        $data = array_shift($dataSet);
        //work on data set
    }
}

API

API is available at bazzline.net

History

  • upcomming * @todo * implement way of measure/calculate the amount of time for next iteration
  • 1.0.11 - released at 10.08.2016 * updated phpunit (developer dependency)
  • 1.0.10 - released at 30.05.2016 * added dedicated travis integration test for php 7.0 * relaxed mockery dependency * removed dedicated travis integration test for php 5.3.3
  • 1.0.9 - released at 07.02.2016 * moved to psr-7 autoloading * updated dependencies
  • 1.0.8 - released at 11.01.2016 fixed dependency handling for phpunit 4.8.
  • 1.0.7 - released at 11.12.2015 * updated dependencies
  • 1.0.6 - released at 18.11.2015 * updated dependencies
  • 1.0.5 - released at 28.08.2015 * updated dependencies
  • 1.0.4 - released at 04.07.2015 * removed local apigen documentation * removed useless code coverage image * updated dependencies
  • 1.0.3 - released at 22.05.2015 * updated dependencies
  • 1.0.2 - released at 08.02.2015 * updated dependencies * removed dependency to apigen
  • 1.0.1 - released at 31.08.2014 * add getRuntimeIn[Seconds|Minutes|Hours] * extended unit tests by covering setting of the buffer * added getter for minutes and hours * updated dependencies
  • 1.0.0 - released at 27.07.2014 * added examples, unit tests and api
  • 0.0.1 - released at 27.07.2014 * initial commit with stable api

Future Improvements

  • if you have one, create a feature request, fork it (and push it back :-))

Final Words

Star it if you like it :-). Add issues if you need it. Pull patches if you enjoy it. Write a blog entry if you use it. Donate something if you love it :-].


  Files folder image Files  
File Role Description
Files folder imageexample (1 directory)
Files folder imagesource (4 files)
Files folder imagetest (2 files)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file generate_api Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  example  
File Role Description
Files folder imageExample (2 files)

  Files folder image Files  /  example  /  Example  
File Role Description
  Accessible without login Plain text file withoutReachingLimit.php Example Example script
  Accessible without login Plain text file withReachingLimit.php Example Example script

  Files folder image Files  /  source  
File Role Description
  Plain text file InvalidArgumentException.php Class Class source
  Plain text file TimeLimitManager.php Class Class source
  Plain text file TimeLimitManagerAwareInterface.php Class Class source
  Plain text file TimeLimitManagerDependentInterface.php Class Class source

  Files folder image Files  /  test  
File Role Description
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file TimeLimitManagerTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:73
This week:1
All time:10,197
This week:560Up