Advertisement
Guest User

Benchmark function set inspired from

a guest
Dec 14th, 2010
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.38 KB | None | 0 0
  1. <?php
  2. // Inspired from http://stackoverflow.com/questions/555523/file-get-contents-vs-curl-what-has-better-performance
  3. $memory = array();
  4. $errors = array("FGC" => 0, "cURL" => 0);
  5.  
  6. declare(ticks=1);
  7. register_tick_function(function()
  8. {
  9.     global $memory;
  10.  
  11.     $mem = memory_get_usage() - $memory["over"];
  12.     $memory["high"] = max($memory["high"], $mem);
  13.     $memory["low"] = min($memory["low"], $mem);
  14.     $memory["avg"] = ($memory["avg"] * $memory["iterations"] + $mem) / ($memory["iterations"] + 1);
  15.     $memory["iterations"]++;
  16. });
  17.  
  18. function resetMemoryInfo()
  19. {
  20.     global $memory;
  21.  
  22.     $memory = array("over" => memory_get_usage(),"high" => 0, "low" => 0, "iterations" => 0, "avg" => 0);
  23. }
  24.  
  25. function Benchmark($function, $arguments = null, $iterations = 10000, $report = TRUE)
  26. {
  27.     set_time_limit(0);
  28.     global $memory;
  29.     $start = array();
  30.  
  31.     print "\r\n!!! Benchmarking function $function for $iterations iteration (args:".json_encode($arguments).")...\r\n";
  32.     resetMemoryInfo();
  33.  
  34.     if (is_callable($function) === true)
  35.     {
  36.         for ($i = 1; $i <= $iterations; ++$i)
  37.         {
  38.             $start["time"] = microtime(true);
  39.             resetMemoryInfo();
  40.             call_user_func_array($function, (array) $arguments);
  41.             $end["memory"][] = $memory["high"];
  42.             resetMemoryInfo();
  43.             $end["time"][] = round(microtime(true) - $start["time"], 8);
  44.  
  45.             if($i % 1000 == 0 && $iterations > 1000) print "--- Done $i iterations...\r\n";
  46.             else if($i % 100 == 0 && $iterations > 100 && $iterations < 1001) print "--- Done $i iterations...\r\n";
  47.         }
  48.  
  49.         foreach(array("time", "memory") as $label)
  50.         {
  51.             $ret[$label]["total"] = array_sum($end[$label]);
  52.             $ret[$label]["min"] = min($end[$label]);
  53.             $ret[$label]["max"] = max($end[$label]);
  54.             $ret[$label]["avg"] = $ret[$label]["total"] / $iterations;
  55.         }
  56.  
  57.         if($report)
  58.         {
  59.             print "===================\r\n";
  60.             print "Results:\r\n";
  61.             print "===================\r\n";
  62.  
  63.             foreach($ret as $label => $data)
  64.             {
  65.                 foreach($data as $subLabel => $subData)
  66.                     print "$label $subLabel:\t\t$subData\r\n";
  67.             }
  68.         }
  69.  
  70.         "Benchmark completed!\r\n";
  71.         return $ret;
  72.     }
  73.  
  74.     return false;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement