Problem/Motivation

timer_read() currently does not work after timer_stop().

Proposed resolution

Use $timers[$name]['time'] inside time_read() and timer_stop() instead of solely relying on $timers[$name]['start'];

Remaining tasks

Backport to d6

User interface changes

none

API changes

none

Original report by @plumbley

timer_read() currently does not work after timer_stop().

This has potential for confusion if by design, so perhaps the documentation should warn against it. E.g. instead of "Read the current timer value without stopping the timer." it could add "Important: the timer MUST be running when timer_read. is called".

To replicate, try e.g.

timer_start('test1'); 
echo timer_read('test1') . '<br />';
timer_stop('test1'); 
echo timer_read('test1');

which seems a reasonable thing to do, but you get e.g.
------
0.04
Notice: Undefined index: start in c:\program files\apache group\apache\htdocs\drupal\includes\bootstrap.inc on line 58
1158186408044.1
------
It looks like this is also true for CVS as well as 4.7.3. Mark.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

plumbley’s picture

Version: 4.7.3 » x.y.z
Status: Active » Needs review
FileSize
689 bytes

Actually this is due to $timers[$name]['start'] being unset by timer_stop(), breaking the logic of the following line lin timer_read():

  $diff = round(($stop - $timers[$name]['start']) * 1000, 2);

So its actually easy to change this to make it work whether the timer is running or not - simply set $diff here to 0 if $timers[$name]['start'] is not set, i.e.

  $diff = isset($timers[$name]['start']) ? round(($stop - $timers[$name]['start']) * 1000, 2) : 0;

Patch attached fixes this for CVS of 14 Sept 2006 (Similar applies to 4.7.3, but I realized it may be a bit pointless for 4.7.3 now with something so minor). Mark.

drumm’s picture

Version: x.y.z » 6.x-dev
Status: Needs review » Needs work

patching file includes/bootstrap.inc
Hunk #1 FAILED at 59.
1 out of 1 hunk FAILED -- saving rejects to file includes/bootstrap.inc.rej

plumbley’s picture

Title: timer_read() returns garbage after timer_stop() » timer_read() returns NULL (no value) after timer_stop()
Category: bug » feature
Status: Needs work » Active

Patch no longer applies, since a version was fixed by chx a couple of months later (version 1.129, Sun Nov 12 00:21:15 2006 UTC).

However, the new implementation of timer_read() does not return a value (i.e. implicitly returns NULL) after timer_stop() instead. This seems strange logic to me - why not allow the timers to be read after timer_stop()?

I have marked it as a "feature request" now instead with updated title (since it would be added functionality), but please mark as closed if this is the intended behaviour.

Best wishes, Mark.

Pasqualle’s picture

Version: 6.x-dev » 7.x-dev
Priority: Minor » Normal
lilou’s picture

Status: Active » Fixed
lilou’s picture

Category: feature » bug
Status: Fixed » Needs review
FileSize
749 bytes

oups ...

Status: Needs review » Needs work

The last submitted patch failed testing.

EvanDonovan’s picture

This is a very confusing situation (that timer_read() doesn't work if timer_stop() has been called). Could someone please re-roll the patch?

Also, could the documentation be updated for 6.x, or, even better, could the fix be backported after it's been applied to 7.x?

lambic’s picture

Status: Needs work » Needs review
FileSize
698 bytes

patch looks ok, re-submitting for the bot

deviantintegral’s picture

Status: Needs review » Needs work

As-is, this patch appears to be a no-op; I'm still getting NULL after timer_stop().

deviantintegral’s picture

Status: Needs work » Needs review
FileSize
2.46 KB

The documentation makes it seem as if the total running time for a single timer will cause the total time to be accumulated. DX frustration ensues :). The attached patch changes the timer_* functions as well as adds tests for them. The tests are still "needs work", but I'd like feedback as to if this is appropriate for D7 or if it should go to D8.

moshe weitzman’s picture

As long as this is a bug report, it can be committed to D7 before the release or afterwars. There is no freeze for bugs.

Dries’s picture

Yep, I'd consider this a bug.

deviantintegral’s picture

Great. Here is an updated patch which includes function comments, and changes the test to be >= instead of >.

I've chosen 3 seconds for the sleep(), but that was just me choosing a number. Could we reduce it to 1?

Status: Needs review » Needs work

The last submitted patch failed testing.

deviantintegral’s picture

Status: Needs work » Needs review
FileSize
2.61 KB

Not sure why the above didn't apply, but I've updated from CVS and it passes tests fine locally.

Dries’s picture

We should probably use a smaller value, yes. I'd think 1 second ought to be enough, if not, use 2 seconds.

deviantintegral’s picture

Here's an update with sleep(1) instead.

Dries’s picture

Looks good to me. Committed to CVS HEAD. Thanks!

Dries’s picture

Status: Needs review » Fixed
deviantintegral’s picture

Version: 7.x-dev » 6.x-dev
Status: Fixed » Needs review
FileSize
834 bytes

I think this should be backported to D6 and D5 as a bug fix. The attached patch is identical to Drupal 7, minus the simpletest. It applies cleanly to Drupal 5 and Drupal 6.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community
mikeryan’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
1002 bytes
kenorb’s picture

similar issue.
subscribe.

My case:

function hook_custom() {
  timer_start('visitorpath');
  // some code
  var_dump(timer_stop('visitorpath'));
}
function hook_exit() {
   var_dump('stop', timer_stop('visitorpath')); exit; // or timer_read - whatever
}

It returns:

array
  'count' => int 1
  'time' => float 82.75
array
  'count' => int 2
  'time' => float 160.29
array
  'count' => int 2
  'time' => float NULL

Workaround is to always start timer before you stop it:

timer_start('visitorpath'); // ad this line
$timer = timer_stop('visitorpath');
var_dump('stop', $timer); exit;

Status: Needs review » Needs work

The last submitted patch, 84008_accumulate_timer_value_5.x_6.x.patch, failed testing.

EvanDonovan’s picture

Status: Needs work » Needs review

Patches for versions of Drupal prior to D7 will always fail testing.

thedavidmeister’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs reroll, +Novice

patch does not apply anymore:

tdm:d6 thedavidmeister$ git apply timer_stop_D6.patch 
error: bootstrap.inc: No such file or directory

but this should be RTBC with a straight re-roll.

jacob.embree’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
812 bytes

Reroll

thedavidmeister’s picture

Status: Needs review » Reviewed & tested by the community

The last submitted patch, 23: timer_stop_D6.patch, failed testing.

Status: Reviewed & tested by the community » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.