3

The question is simple, I would like to check a database to serve customised content to a site visitor, but failover and serve a generic page if this function takes more then 800ms to execute. (Target time for the server response is 1000ms).

I've seen the set_time_limit function, however this takes an integer in seconds as the argument.

My question: is there something similar that can be used with values of less than 1 second?

I'm looking for something like:

void set_time_limit_ms ( int $milliseconds )

set_time_limit_ms (800)
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
James Scott
  • 1,032
  • 1
  • 10
  • 17
  • so the problem is a long running database query? See http://stackoverflow.com/questions/4794747/mysql-can-i-limit-the-maximum-time-allowed-for-a-query-to-run – bitWorking Jul 25 '13 at 22:05
  • potentially yes, although I'll also be looking to do some mathematical comparisons in PHP that will also take up some processing time, or possibly call a script in another language such as R. Basically I want to start the clock when the function begins and kill it at 800ms, with the freedom to put whatever I want in the function (I'll be doing a lot of experiments to optimise the speed at the beginning), knowing that it'll kill the query and serve a generic page at 800ms. Thanks for taking an interest. – James Scott Jul 26 '13 at 11:35

3 Answers3

3

doesn't exist. you just could emulate it with a tick function:

declare(ticks=1); // or more if 1 takes too much time
$start = microtime(1);
register_tick_function(function () use ($start) {
    (microtime(1) - $start < 0.8) or die();
});
bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • but that's not a solution to a long running query!? – bitWorking Jul 25 '13 at 22:04
  • Nor can you interrupt long running queries with set_time_limit(). If time limit is exceeded during a long running query, the program just will abort after the execution of the query. You can't do anything from the PHP side against long running queries. (and if I'm not wrong, nor from mysql side) – bwoebi Jul 25 '13 at 22:06
  • Hi bwoebi, thanks for the input. I'll code this up and test it over the weekend. I'll post back with the results. – James Scott Jul 26 '13 at 11:36
1

You will not be able to use this function to prevent a query that is running longer than you expected. This only measures the actual script execution time. Here is an bit from the manual.

The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. 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 when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.

DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • Hi DevZer0, although this didn't specifically answer the question, its a really welcome pointer that I hadn't thought of and need to take into consideration. Thanks a lot!! – James Scott Jul 26 '13 at 11:28
0

Yes there is, I often use microtime. see

http://php.net/manual/en/function.microtime.php

<?php
$time_start = microtime(true);

// Sleep for a while
usleep(100);

$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Did nothing in $time seconds\n";
?>
Robert Dickey
  • 816
  • 14
  • 35
  • 1
    this doesn't really answer the question which was: `How can I stop the program's execution after a certain time in milliseconds?` And not: `How many milliseconds did my program take?` – bwoebi Jul 25 '13 at 22:03