]> granicus.if.org Git - php/commitdiff
Use hrtime() for timing tests
authorAnatol Belski <ab@php.net>
Thu, 31 May 2018 13:30:32 +0000 (15:30 +0200)
committerAnatol Belski <ab@php.net>
Thu, 31 May 2018 13:32:47 +0000 (15:32 +0200)
Zend/bench.php
Zend/micro_bench.php
run-tests.php

index 818ce5fbafbc2fee3f5838c63aacab70d809f908..f76bb3f2bdf534a958c2d71fec9dd2f80e4cab95 100644 (file)
@@ -345,22 +345,22 @@ function strcat($n) {
 
 /*****/
 
-function getmicrotime()
+function gethrtime()
 {
-  $t = gettimeofday();
-  return ($t['sec'] + $t['usec'] / 1000000);
+  $hrtime = hrtime();
+  return (($hrtime[0]*1000000000 + $hrtime[1]) / 1000000000);
 }
 
 function start_test()
 {
        ob_start();
-  return getmicrotime();
+  return gethrtime();
 }
 
 function end_test($start, $name)
 {
   global $total;
-  $end = getmicrotime();
+  $end = gethrtime();
   ob_end_clean();
   $total += $end-$start;
   $num = number_format($end-$start,3);
@@ -368,7 +368,7 @@ function end_test($start, $name)
 
   echo $name.$pad.$num."\n";
        ob_start();
-  return getmicrotime();
+  return gethrtime();
 }
 
 function total()
index 70525882ebad93a9731a96faf6d2bc741209f4ca..c9ea19e263eb09d502ecf8b6888590b8b364060a 100644 (file)
@@ -238,23 +238,23 @@ function empty_loop($n) {
        }
 }
 
-function getmicrotime()
+function gethrtime()
 {
-  $t = gettimeofday();
-  return ($t['sec'] + $t['usec'] / 1000000);
+  $hrtime = hrtime();
+  return (($hrtime[0]*1000000000 + $hrtime[1]) / 1000000000);
 }
 
 function start_test()
 {
   ob_start();
-  return getmicrotime();
+  return gethrtime();
 }
 
 function end_test($start, $name, $overhead = null)
 {
   global $total;
   global $last_time;
-  $end = getmicrotime();
+  $end = gethrtime();
   ob_end_clean();
   $last_time = $end-$start;
   $total += $last_time;
@@ -267,7 +267,7 @@ function end_test($start, $name, $overhead = null)
     echo $name.$pad.$num."    ".$num2."\n";
   }
   ob_start();
-  return getmicrotime();
+  return gethrtime();
 }
 
 function total()
index 59f878dc39fa8884576ba652bbf4cd4feee1e262..87bd8daa1f37a1f892f4edfc31864af4c46ad110 100644 (file)
@@ -183,6 +183,20 @@ if (getenv('TEST_PHPDBG_EXECUTABLE')) {
        $environment['TEST_PHPDBG_EXECUTABLE'] = $phpdbg;
 }
 
+if (!function_exists("hrtime")) {
+       function hrtime(bool $as_num = false)
+       {
+               $t = microtime(true);
+
+               if ($as_num) {
+                       return $t*1000000000;
+               }
+
+               $s = floor($t);
+               return array(0 => $s, 1 => ($t - $s)*1000000000);
+       }
+}
+
 function verify_config()
 {
        global $php;
@@ -1923,19 +1937,21 @@ COMMAND $cmd
 ";
 
        junit_start_timer($shortname);
-       $startTime = microtime(true);
+       $hrtime = hrtime();
+       $startTime = $hrtime[0]*1000000000 + $hrtime[1];
 
        $out = system_with_timeout($cmd, $env, isset($section_text['STDIN']) ? $section_text['STDIN'] : null, $captureStdIn, $captureStdOut, $captureStdErr);
 
        junit_finish_timer($shortname);
-       $time = microtime(true) - $startTime;
-       if ($time * 1000 >= $slow_min_ms) {
+       $hrtime = hrtime();
+       $time = $hrtime[0]*1000000000 + $hrtime[1] - $startTime;
+       if ($time >= $slow_min_ms * 1000000) {
                $PHP_FAILED_TESTS['SLOW'][] = array(
                        'name'      => $file,
                        'test_name' => (is_array($IN_REDIRECT) ? $IN_REDIRECT['via'] : '') . $tested . " [$tested_file]",
                        'output'    => '',
                        'diff'      => '',
-                       'info'      => $time,
+                       'info'      => $time / 1000000000,
                );
        }