Example #1
0
File: Url.php Project: tpc2/phprack
 /**
  * Checks for http response code.
  * @var string regexp pattern of the valid http response code.
  * @return  boolean.
  * @throws phpRack_Exception If this method is called before url()
  */
 public function responseCode($pattern)
 {
     if (empty($this->_url)) {
         throw new phpRack_Exception('url() function must be called before');
     }
     $code = $this->_adapter->getResponseCode();
     if (preg_match($pattern, $code)) {
         $this->_success("Valid response code {$code} was returned by the link '{$this->_url}'");
     } else {
         $this->_failure("Invalid response code {$code} was returned by the link '{$this->_url}'");
     }
 }
Example #2
0
 /**
  * Make HTTP call and check that pattern exists in result
  *
  * @param string Pattern to check
  * @return $this
  * @see url()
  * @throws Exception If this method is called before url()
  * @throws Exception If can't connect
  */
 public function regex($pattern)
 {
     if (empty($this->_url)) {
         throw new Exception('url() function must be called before');
     }
     $content = $this->_adapter->getContent();
     $found = @preg_match($pattern, $content);
     // If regexp pattern is invalid, try to use it as string pattern
     if ($found === false) {
         $this->_log('Regex is not valid, try to use it as string');
         $found = strpos($content, $pattern) !== false;
     }
     // If pattern was found in content
     if ($found) {
         $this->_success("Pattern '{$pattern}' was found on '{$this->_url}'");
     } else {
         $this->_failure("Pattern '{$pattern}' was NOT found on '{$this->_url}'");
     }
     return $this;
 }
Example #3
0
 /**
  * @expectedException phpRack_Exception
  */
 public function testFactoryWithInvalidOptions()
 {
     $options = array('invalidOption' => false);
     phpRack_Adapters_Url::factory('http://www.google.com', $options);
 }
Example #4
0
File: Qos.php Project: tpc2/phprack
 /**
  * Check latency for the given URL(s).
  *
  * <p>Expected options are:
  *
  * <code>
  * array(
  *   'scenario' => array(
  *     'http://www.google.com/',
  *     'http://www.amazon.com/',
  *   ),
  *   'testsTotal' => 7,
  *   'peakMs' => 5000,
  *   'averageMs' => 2000,
  * )
  * </code>
  *
  * <p>In this example, in total, there will be seven HTTP requests made. Every next
  * HTTP request will get a URL from the 'scenario' array. When all requests
  * are finished their performance will be compared to <code>peakMs</code>
  * and <code>averageMs</code>.
  *
  * @param $options string|array of options
  * @return phpRack_Package_Qos This object
  * @throws phpRack_Exception if invalid option was passed
  * @throws phpRack_Exception if no url was passed
  */
 public function latency($options)
 {
     $options = $this->_prepareLatencyOptions($options);
     /**
      * @see phpRack_Adapters_Url
      */
     require_once PHPRACK_PATH . '/Adapters/Url.php';
     $totalRequestsTime = 0;
     $requestsCompleted = 0;
     reset($options['scenario']);
     while ($requestsCompleted < $options['testsTotal']) {
         $url = current($options['scenario']);
         if ($url === false) {
             reset($options['scenario']);
             continue;
         }
         // make request and measure latency
         $start = microtime(true);
         $urlAdapter = new phpRack_Adapters_Url($url);
         $content = $urlAdapter->getContent();
         $requestTime = microtime(true) - $start;
         $requestTimeInMs = intval($requestTime * 1000);
         $this->_log("HTTP to {$url}: {$requestTimeInMs}ms, " . strlen($content) . ' bytes');
         // check single query time meets limit
         if ($requestTimeInMs > $options['peakMs']) {
             $this->_failure("Peak latency is {$requestTimeInMs}ms, but value below {$options['peakMs']}ms was expected");
             return $this;
         }
         $totalRequestsTime += $requestTime;
         $requestsCompleted++;
         next($options['scenario']);
     }
     // check average queries time meets limit
     $averageMs = intval($totalRequestsTime / $options['testsTotal'] * 1000);
     if ($averageMs < $options['averageMs']) {
         $this->_success("Average latency {$averageMs}ms");
     } else {
         $this->_failure("Average latency is {$averageMs}ms, but {$options['averageMs']}ms was expected");
     }
     return $this;
 }