/** * Make HTTP call and check that pattern exists in result. * * @param $pattern string Pattern to check * @return $this * @see url() * @throws phpRack_Exception If this method is called before url() * @throws phpRack_Exception If can't connect */ public function regex($pattern = '/.*/') { if (empty($this->_url)) { throw new phpRack_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; }
/** * 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; }