PHP_Fork
[ class tree: PHP_Fork ] [ index: PHP_Fork ] [ all elements ]

Source for file browser_pool.php

Documentation is available at browser_pool.php

  1. <?php
  2. /**
  3. * PHP_Fork class usage examples
  4. * ==================================================================================
  5. * NOTE: In real world you surely want to keep each class into
  6. * a separate file, then include() it into your application.
  7. * For this examples is more useful to keep all_code_into_one_file,
  8. * so that each example shows a unique feature of the PHP_Fork framework.
  9. * ==================================================================================
  10. * browser_pool.php
  11. *
  12. * This example show a "real" usage example: a command line interactive
  13. * tool that sends cuncurrent requests to a given URL, using a dynamic
  14. * process pool.
  15. * The pool start size is defined into the constant NUM_START_THREAD
  16. * The application show a simple CLI prompt, user can type an URL
  17. * and the number of requests to perform.
  18. * The application takes care of forking processes (when needed),
  19. * sending requests and collect results.
  20. * Min, max and average response times are reported.
  21. *
  22. * This example requires the Snoopy class (http://sourceforge.net/project/?group_id=2091)
  23. *
  24. * ==================================================================================
  25. *
  26. */
  27.  
  28. // Import of base class
  29. require_once ("PHP/Fork.php");
  30. require_once ("Snoopy.class.php");
  31.  
  32. // number of executeThreads we want at start
  33. define ("NUM_START_THREAD"2);
  34.  
  35. // this is needed as PHP 4.3 in order to use pcntl_signal()
  36. declare (ticks = 1);
  37.  
  38. /**
  39. * Classes definition. In real world you surely want to keep each class into
  40. * a separate file, then include() it into your application.
  41. * For this examples is more useful to keep all_code_into_one_file
  42. *
  43. * executeThread class inherit from PHP_Fork and must redefine the run() method
  44. * all the code contained into the run() method will be executed only by the child
  45. * process; all other methods that you define will be accessible both to the parent
  46. * and to the child (and will be executed into the relative process)
  47. */
  48.  
  49. class executeThread extends PHP_Fork {
  50.         var $request;
  51.  
  52.         function executeThread($name)
  53.         {
  54.                 $this->PHP_Fork($name);
  55.         }
  56.  
  57.         function run()
  58.         {
  59.                 while (true{
  60.                         sleep(1);
  61.                 }
  62.         }
  63.  
  64.         function sendRequest($val)
  65.         {
  66.                 if ($this->_isChild{
  67.                         $snoopy = new Snoopy;
  68.                         $time_start $this->microtime_float();
  69.  
  70.                         $snoopy->fetchtext($val[0]);
  71.                         return ($this->microtime_float($time_start)*1000;
  72.                 }
  73.                 /**
  74.                 * Never change this line, it requires no adjustments...
  75.                 */
  76.                 else return $this->register_callback_func(func_get_args()__FUNCTION__);
  77.         }
  78.         function microtime_float()
  79.         {
  80.                 list($usec$secexplode(" "microtime());
  81.                 return ((float)$usec + (float)$sec);
  82.         }
  83.  
  84.         function getCounter($params)
  85.         {
  86.                 if ($this->_isChild{
  87.                         return $GLOBALS["counter"];
  88.                 else return $this->register_callback_func(func_get_args()__FUNCTION__);
  89.         }
  90. }
  91.  
  92. function showHelpMsg()
  93. {
  94.         print "Type an URL followed by a space and the number of cuncurrent requests you wish to perform, or type [X] to terminate.\n";
  95. }
  96.  
  97. /**
  98. * Main program. Bring up two instances of the executeThread class that
  99. * runs concurrently. It's a multi-thread app with a few lines of code!!!
  100. * executeThread does nothing interesting, it simply has a counter and increment
  101. * this counter each second... (see class definition at top of this file)
  102. */
  103. for ($i = 0;$i NUM_START_THREAD;$i++{
  104.         $executeThread[$i&new executeThread ("httpClient-" $i);
  105.         $executeThread[$i]->start();
  106.         echo "Started " $executeThread[$i]->getName(" with PID " $executeThread[$i]->getPid("...\n";
  107. }
  108. print "This is the main process.\n";
  109. /**
  110. * Console simple listener
  111. */
  112. $fp fopen("php://stdin","r");
  113. while (true{
  114.         echo ">";
  115.         $cmdline trim(fgets($fp,1024));
  116.         if ($cmdline == ""{
  117.                 showHelpMsg();
  118.                 continue;
  119.         }
  120.  
  121.         list ($url$numreqexplode (" ",$cmdline);
  122.         // on user request exit
  123.         if empty($numreq&& $url == 'X' || $url == 'x'))
  124.         {
  125.                 foreach ($executeThread as $thread{
  126.                         $thread->stop();
  127.                         echo "Stopped " $thread->getName("\n";
  128.                 }
  129.                 exit;
  130.         }
  131.  
  132.  
  133.         // spawn so many childs to reach $numreq
  134.         while (count($executeThread$numreq)
  135.         {
  136.                 $i count($executeThread);
  137.                 $executeThread[$i&new executeThread ("httpClient-" $i);
  138.                 $executeThread[$i]->start();
  139.                 print time("- New instance successfully spawned (NAME=".$executeThread[$i]->getName().",PID=" $executeThread[$i]->getPid(")\n";
  140.  
  141.         }
  142.         echo "Please wait .";
  143.         $i=0;
  144.         while($i<5)
  145.         {
  146.                 echo ".";
  147.                 $i++;
  148.                 sleep(1);
  149.         }
  150.         echo "\nSending requests...\n";
  151.         $responeTimes = array();
  152.  
  153.         foreach ($executeThread as $idx=>$thread{
  154.                 $responeTimes[=$executeThread[$idx]->sendRequest($url,PHP_FORK_RETURN_METHOD);
  155.         }
  156.         sort ($responeTimesSORT_NUMERIC);
  157.         echo "Fastest request time (msecs): ".$responeTimes[0]."\n";
  158.         echo "Longest request time (msecs): ".$responeTimes[count($responeTimes)-1]."\n";
  159.         echo "Average request time (msecs): ".(array_sum($responeTimes)/count($responeTimes))."\n";
  160.  
  161.  
  162.  
  163. }
  164.  
  165. fclose($fp);

Documentation generated on Mon, 11 Mar 2019 15:41:27 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.