/** * 计时器停止. * * @param name * 计时器名字. * @return * 返回计时器数组. */ function timer_stop($name) { global $timers; $timers[$name]['time'] = timer_read($name); unset($timers[$name]['start']); return $timers[$name]; }
/** * @file * lib-common.php * Common library of functions for the applications */ function firelogmsg($message) { global $firephp, $mytimer; $exectime = timer_read('filedepot_timer'); if (function_exists('dfb')) { // Calls the firephp fb() function to log message to the firebug console dfb("{$message} - time:{$exectime}"); } }
the end of the request. <?php // Queue up the request. $urls = array('http://www.google.com/', 'http://www.bing.com/', 'http://www.yahoo.com/', 'http://duckduckgo.com/', 'http://drupal.org/', 'http://drupal.org/robots.txt', 'http://drupal.org/CHANGELOG.txt', 'http://drupal.org/MAINTAINERS.txt'); $options = array('stall_fread' => TRUE, 'headers' => array('Range' => 'bytes=-128', 'Accept-Encoding' => 'gzip, deflate'), 'chunk_size_read' => 524288, 'domain_connections' => 4, 'connect_timeout' => 0.5, 'dns_timeout' => 0.5); httprl_request($urls, $options); // Execute request. echo round(timer_read('page') / 1000, 3) . " - Time taken to get requests ready.<br> \n"; $request = httprl_send_request(); echo strtoupper(var_export($request, TRUE)) . " - Output from first httprl_send_request() call<br> \n"; echo round(timer_read('page') / 1000, 3) . " - Time taken to send out all fwrites().<br> \n"; sleep(2); echo round(timer_read('page') / 1000, 3) . " - Time taken for sleep(2).<br> \n"; $request = httprl_send_request(); echo round(timer_read('page') / 1000, 3) . " - Time taken for all freads().<br> \n"; echo "Output from second httprl_send_request() below:<br> \n<br> \n"; echo httprl_pr($request); ?> **Threading Examples** Use 2 threads to load up 4 different nodes. <?php // Bail out here if background callbacks are disabled. if (!httprl_is_background_callback_capable()) { return FALSE; } // List of nodes to load; 241-244.
/** * Overrides Drupal core HTTP request function with Guzzle. * * @see drupal_http_request() */ function bangpound_drupal_http_request($url, array $options = array()) { $result = new stdClass(); // Parse the URL and make sure we can handle the schema. $uri = @parse_url($url); if ($uri == FALSE) { $result->error = 'unable to parse URL'; $result->code = -1001; return $result; } if (!isset($uri['scheme'])) { $result->error = 'missing schema'; $result->code = -1002; return $result; } timer_start(__FUNCTION__); // Merge the default options. $options += array('headers' => array(), 'method' => 'GET', 'data' => NULL, 'max_redirects' => 3, 'timeout' => 30.0, 'context' => NULL); // Merge the default headers. $options['headers'] += array('User-Agent' => 'Drupal (+http://drupal.org/)'); // Concessions to Guzzle. if (isset($options['data'])) { $options['body'] = $options['data']; } if (!$options['max_redirects']) { $options['allow_redirects'] = FALSE; } // Use a proxy if one is defined and the host is not on the excluded list. $proxy_server = variable_get('proxy_server', ''); if ($proxy_server && _drupal_http_use_proxy($uri['host'])) { // Set the scheme so we open a socket to the proxy server. $uri['scheme'] = 'proxy'; // Set the path to be the full URL. $uri['path'] = $url; // Since the URL is passed as the path, we won't use the parsed query. unset($uri['query']); // Add in username and password to Proxy-Authorization header if needed. if ($proxy_username = variable_get('proxy_username', '')) { $proxy_password = variable_get('proxy_password', ''); $options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : '')); } // Some proxies reject requests with any User-Agent headers, while others // require a specific one. $proxy_user_agent = variable_get('proxy_user_agent', ''); // The default value matches neither condition. if ($proxy_user_agent === NULL) { unset($options['headers']['User-Agent']); } elseif ($proxy_user_agent) { $options['headers']['User-Agent'] = $proxy_user_agent; } } // Make sure the socket opened properly. // @todo Migrate error checking. // If the server URL has a user then attempt to use basic authentication. // @todo Migrate authentication. // If the database prefix is being used by SimpleTest to run the tests in a copied // database then set the user-agent header to the database prefix so that any // calls to other Drupal pages will run the SimpleTest prefixed database. The // user-agent is used to ensure that multiple testing sessions running at the // same time won't interfere with each other as they would if the database // prefix were stored statically in a file or database variable. $test_info =& $GLOBALS['drupal_test_info']; if (!empty($test_info['test_run_id'])) { $options['headers']['User-Agent'] = drupal_generate_test_ua($test_info['test_run_id']); } // Calculate how much time is left of the original timeout value. $timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000; if ($timeout > 0) { /** @see \Guzzle\Http\StaticClient::request() */ static $client; if (!$client) { $client = new \Guzzle\Http\Client(); } $request = $client->createRequest($options['method'], $url, null, null, $options); if ($options['max_redirects']) { $client->getConfig()->set('redirect.max', $options['max_redirects']); } if (isset($options['stream'])) { if ($options['stream'] instanceof \Guzzle\Stream\StreamRequestFactoryInterface) { $response = $options['stream']->fromRequest($request); } elseif ($options['stream'] == true) { $streamFactory = new \Guzzle\Stream\PhpStreamRequestFactory(); $response = $streamFactory->fromRequest($request); } } else { $response = $request->send(); } $result->request = $request->__toString(); } else { $result->code = HTTP_REQUEST_TIMEOUT; $result->error = 'request timed out'; return $result; } if (isset($response)) { // Parse response headers from the response body. // Be tolerant of malformed HTTP responses that separate header and body with // \n\n or \r\r instead of \r\n\r\n. $result->data = $response->getBody(true); // Parse the response status line. $result->protocol = $response->getProtocol() . '/' . $response->getProtocolVersion(); $result->status_message = $response->getReasonPhrase(); $result->headers = array_map(function ($input) { return (string) $input; }, $response->getHeaders()->getAll()); $result->code = $response->getStatusCode(); switch ($result->code) { case 200: // OK // OK case 304: // Not modified break; case 301: // Moved permanently // Moved permanently case 302: // Moved temporarily // Moved temporarily case 307: // Moved temporarily // $result->redirect_code = $code; // $result->redirect_url = $location; break; default: $result->error = $response->getReasonPhrase(); } } return $result; }
function filedepot_displaySearchListing($query_terms) { $output = ''; if (!empty($query_terms)) { $filedepot = filedepot_filedepot(); $args = array(); $fields = array('a.title', 'a.description', 'b.name', 'b.description'); $keywords = explode(' ', $query_terms); $sql = ''; $select = "SELECT a.fid, a.cid, a.title, a.fname, a.date, a.version, a.submitter, " . "a.status, a.description, a.size, b.pid, b.nid, b.name as foldername, b.description as folderdesc " . "FROM filedepot_files a JOIN filedepot_categories b ON b.cid=a.cid "; if (!empty($filedepot->allowableViewFoldersSql)) { $where = 'WHERE a.cid IN (' . $filedepot->allowableViewFoldersSql . ') AND '; } else { $where = 'WHERE '; } /* Build the AND clause of the SQL Query * Find files that match all the search terms in any of the defined fields * So for each field name, description folder, that field text must have * all the keywords */ $field_id = 1; $where_and = ''; foreach ($fields as $field) { $term_id = 1; $where_and .= $field_id == 1 ? '(' : '1=1) OR ('; foreach ($keywords as $keyword) { $where_and .= "{$field} like :afield{$field_id}_{$term_id} AND "; $args = array_merge($args, array(":afield{$field_id}_{$term_id}" => "%{$keyword}%")); $term_id++; } $field_id++; } $where_and .= empty($where_and) ? '' : '1=1) '; $sql .= "{$select} {$where} {$where_and} UNION {$select} "; /* Build the second query - UNION used so that we return first the * files that match all the keywords * Second query will find files that match ANY of the search terms in any of the defined fields */ if (!empty($filedepot->allowableViewFoldersSql)) { $sql .= ' WHERE a.cid IN (' . $filedepot->allowableViewFoldersSql . ') AND ('; } else { $sql .= ' WHERE ('; } /* Build the second query OR clause - loop over the fields and terms */ $field_id = 1; $where_or = ''; foreach ($fields as $field) { $term_id = 1; $where_or .= $field_id > 1 ? ' OR ' : ''; foreach ($keywords as $keyword) { $where_or .= $term_id > 1 ? ' OR ' : ''; $where_or .= "{$field} like :bfield{$field_id}_{$term_id}"; $args = array_merge($args, array(":bfield{$field_id}_{$term_id}" => "%{$keyword}%")); $term_id++; } $field_id++; } $where_or .= !empty($where_or) ? ') ' : ' '; $sql .= $where_or; $result = db_query($sql, $args); $time = timer_read('get_time'); drupal_set_message($time); $filedepot->recordCount = $result->rowCount(); while ($A = $result->fetchAssoc()) { $output .= theme('filedepot_filelisting', array('listingrec' => $A)); } } return $output; }
/** * Perform updates for one second or until finished. * * @return * An array indicating the status after doing updates. The first element is * the overall percentage finished. The second element is a status message. */ function update_do_updates() { while (isset($_SESSION['update_remaining']) && ($update = reset($_SESSION['update_remaining']))) { $update_finished = update_data($update['module'], $update['version']); if ($update_finished == 1) { // Dequeue the completed update. unset($_SESSION['update_remaining'][key($_SESSION['update_remaining'])]); $update_finished = 0; // Make sure this step isn't counted double } if (timer_read('page') > 1000) { break; } } if ($_SESSION['update_total']) { $percentage = floor(($_SESSION['update_total'] - count($_SESSION['update_remaining']) + $update_finished) / $_SESSION['update_total'] * 100); } else { $percentage = 100; } // When no updates remain, clear the caches in case the data has been updated. if (!isset($update['module'])) { cache_clear_all('*', 'cache', TRUE); cache_clear_all('*', 'cache_page', TRUE); cache_clear_all('*', 'cache_menu', TRUE); cache_clear_all('*', 'cache_filter', TRUE); drupal_clear_css_cache(); } return array($percentage, isset($update['module']) ? 'Updating ' . $update['module'] . ' module' : 'Updating complete'); }
function boost_stats_add_access_log() { global $title, $q, $referer, $session_id, $uid; db_query("INSERT INTO {accesslog} (title, path, url, hostname, uid, sid, timer, timestamp) values('%s', '%s', '%s', '%s', %d, '%s', %d, %d)", $title, $q, $referer, ip_address(), $uid, $session_id, timer_read('page'), time()); }
/** * Performs an HTTP request. * * This is a flexible and powerful HTTP client implementation. Correctly * handles GET, POST, PUT or any other HTTP requests. Handles redirects. * * @param $url * A string containing a fully qualified URI. * @param array $options * (optional) An array that can have one or more of the following elements: * - headers: An array containing request headers to send as name/value pairs. * - method: A string containing the request method. Defaults to 'GET'. * - data: A string containing the request body, formatted as * 'param=value¶m=value&...'. Defaults to NULL. * - max_redirects: An integer representing how many times a redirect * may be followed. Defaults to 3. * - timeout: A float representing the maximum number of seconds the function * call may take. The default is 30 seconds. If a timeout occurs, the error * code is set to the HTTP_REQUEST_TIMEOUT constant. * - context: A context resource created with stream_context_create(). * * @return object * An object that can have one or more of the following components: * - request: A string containing the request body that was sent. * - code: An integer containing the response status code, or the error code * if an error occurred. * - protocol: The response protocol (e.g. HTTP/1.1 or HTTP/1.0). * - status_message: The status message from the response, if a response was * received. * - redirect_code: If redirected, an integer containing the initial response * status code. * - redirect_url: If redirected, a string containing the URL of the redirect * target. * - error: If an error occurred, the error message. Otherwise not set. * - headers: An array containing the response headers as name/value pairs. * HTTP header names are case-insensitive (RFC 2616, section 4.2), so for * easy access the array keys are returned in lower case. * - data: A string containing the response body that was received. */ public static function _bim_http_request($url, array $options = array()) { $result = new stdClass(); // Parse the URL and make sure we can handle the schema. $uri = @parse_url($url); if ($uri == FALSE) { $result->error = 'unable to parse URL'; $result->code = -1001; return $result; } if (!isset($uri['scheme'])) { $result->error = 'missing schema'; $result->code = -1002; return $result; } bimserverJsonConnector::timer_start(__FUNCTION__); // Merge the default options. $options += array('headers' => array(), 'method' => 'GET', 'data' => NULL, 'max_redirects' => 3, 'timeout' => 30.0, 'context' => NULL); // Merge the default headers. $options['headers'] += array('User-Agent' => 'Drupal (+http://drupal.org/)'); // stream_socket_client() requires timeout to be a float. $options['timeout'] = (double) $options['timeout']; // Use a proxy if one is defined and the host is not on the excluded list. $proxy_server = variable_get('proxy_server', ''); if ($proxy_server && _drupal_http_use_proxy($uri['host'])) { // Set the scheme so we open a socket to the proxy server. $uri['scheme'] = 'proxy'; // Set the path to be the full URL. $uri['path'] = $url; // Since the URL is passed as the path, we won't use the parsed query. unset($uri['query']); // Add in username and password to Proxy-Authorization header if needed. if ($proxy_username = variable_get('proxy_username', '')) { $proxy_password = variable_get('proxy_password', ''); $options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : '')); } // Some proxies reject requests with any User-Agent headers, while others // require a specific one. $proxy_user_agent = variable_get('proxy_user_agent', ''); // The default value matches neither condition. if ($proxy_user_agent === NULL) { unset($options['headers']['User-Agent']); } elseif ($proxy_user_agent) { $options['headers']['User-Agent'] = $proxy_user_agent; } } switch ($uri['scheme']) { case 'proxy': // Make the socket connection to a proxy server. $socket = 'tcp://' . $proxy_server . ':' . variable_get('proxy_port', 8080); // The Host header still needs to match the real request. $options['headers']['Host'] = $uri['host']; $options['headers']['Host'] .= isset($uri['port']) && $uri['port'] != 80 ? ':' . $uri['port'] : ''; break; case 'http': case 'feed': $port = isset($uri['port']) ? $uri['port'] : 80; $socket = 'tcp://' . $uri['host'] . ':' . $port; // RFC 2616: "non-standard ports MUST, default ports MAY be included". // We don't add the standard port to prevent from breaking rewrite rules // checking the host that do not take into account the port number. $options['headers']['Host'] = $uri['host'] . ($port != 80 ? ':' . $port : ''); break; case 'https': // Note: Only works when PHP is compiled with OpenSSL support. $port = isset($uri['port']) ? $uri['port'] : 443; $socket = 'ssl://' . $uri['host'] . ':' . $port; $options['headers']['Host'] = $uri['host'] . ($port != 443 ? ':' . $port : ''); break; default: $result->error = 'invalid schema ' . $uri['scheme']; $result->code = -1003; return $result; } if (empty($options['context'])) { $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout']); } else { // Create a stream with context. Allows verification of a SSL certificate. $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $options['context']); } // Make sure the socket opened properly. if (!$fp) { // When a network error occurs, we use a negative number so it does not // clash with the HTTP status codes. $result->code = -$errno; $result->error = trim($errstr) ? trim($errstr) : t('Error opening socket @socket', array('@socket' => $socket)); // Mark that this request failed. This will trigger a check of the web // server's ability to make outgoing HTTP requests the next time that // requirements checking is performed. // See system_requirements(). //variable_set('_bim_http_request_fails', TRUE); return $result; } // Construct the path to act on. $path = isset($uri['path']) ? $uri['path'] : '/'; if (isset($uri['query'])) { $path .= '?' . $uri['query']; } // Only add Content-Length if we actually have any content or if it is a POST // or PUT request. Some non-standard servers get confused by Content-Length in // at least HEAD/GET requests, and Squid always requires Content-Length in // POST/PUT requests. $content_length = strlen($options['data']); if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') { $options['headers']['Content-Length'] = $content_length; } // If the server URL has a user then attempt to use basic authentication. if (isset($uri['user'])) { $options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . (isset($uri['pass']) ? ':' . $uri['pass'] : '******')); } // If the database prefix is being used by SimpleTest to run the tests in a copied // database then set the user-agent header to the database prefix so that any // calls to other Drupal pages will run the SimpleTest prefixed database. The // user-agent is used to ensure that multiple testing sessions running at the // same time won't interfere with each other as they would if the database // prefix were stored statically in a file or database variable. $test_info =& $GLOBALS['drupal_test_info']; if (!empty($test_info['test_run_id'])) { $options['headers']['User-Agent'] = drupal_generate_test_ua($test_info['test_run_id']); } $request = $options['method'] . ' ' . $path . " HTTP/1.0\r\n"; foreach ($options['headers'] as $name => $value) { $request .= $name . ': ' . trim($value) . "\r\n"; } $request .= "\r\n" . $options['data']; $result->request = $request; // Calculate how much time is left of the original timeout value. $timeout = $options['timeout'] - bimserverJsonConnector::timer_read(__FUNCTION__) / 1000; if ($timeout > 0) { stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1))); fwrite($fp, $request); } // Fetch response. Due to PHP bugs like http://bugs.php.net/bug.php?id=43782 // and http://bugs.php.net/bug.php?id=46049 we can't rely on feof(), but // instead must invoke stream_get_meta_data() each iteration. $info = stream_get_meta_data($fp); $alive = !$info['eof'] && !$info['timed_out']; $response = ''; while ($alive) { // Calculate how much time is left of the original timeout value. $timeout = $options['timeout'] - bimserverJsonConnector::timer_read(__FUNCTION__) / 1000; if ($timeout <= 0) { $info['timed_out'] = TRUE; break; } stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1))); $chunk = fread($fp, 1024); $response .= $chunk; $info = stream_get_meta_data($fp); $alive = !$info['eof'] && !$info['timed_out'] && $chunk; } fclose($fp); $HTTP_REQUEST_TIMEOUT = 408; if ($info['timed_out']) { $result->code = $HTTP_REQUEST_TIMEOUT; $result->error = 'request timed out'; return $result; } // Parse response headers from the response body. // Be tolerant of malformed HTTP responses that separate header and body with // \n\n or \r\r instead of \r\n\r\n. list($response, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2); $response = preg_split("/\r\n|\n|\r/", $response); // Parse the response status line. $response_status_array = bimserverJsonConnector::_bimserver_parse_response_status(trim(array_shift($response))); $result->protocol = $response_status_array['http_version']; $result->status_message = $response_status_array['reason_phrase']; $code = $response_status_array['response_code']; $result->headers = array(); // Parse the response headers. while ($line = trim(array_shift($response))) { list($name, $value) = explode(':', $line, 2); $name = strtolower($name); if (isset($result->headers[$name]) && $name == 'set-cookie') { // RFC 2109: the Set-Cookie response header comprises the token Set- // Cookie:, followed by a comma-separated list of one or more cookies. $result->headers[$name] .= ',' . trim($value); } else { $result->headers[$name] = trim($value); } } $responses = array(100 => 'Continue', 101 => 'Switching Protocols', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported'); // RFC 2616 states that all unknown HTTP codes must be treated the same as the // base code in their class. if (!isset($responses[$code])) { $code = floor($code / 100) * 100; } $result->code = $code; switch ($code) { case 200: // OK // OK case 304: // Not modified break; case 301: // Moved permanently // Moved permanently case 302: // Moved temporarily // Moved temporarily case 307: // Moved temporarily $location = $result->headers['location']; $options['timeout'] -= timer_read(__FUNCTION__) / 1000; if ($options['timeout'] <= 0) { $result->code = HTTP_REQUEST_TIMEOUT; $result->error = 'request timed out'; } elseif ($options['max_redirects']) { // Redirect to the new location. $options['max_redirects']--; $result = _bim_http_request($location, $options); $result->redirect_code = $code; } if (!isset($result->redirect_url)) { $result->redirect_url = $location; } break; default: $result->error = $result->status_message; } return $result; }
/** * Perform updates for one second or until finished. * * @return * An array indicating the status after doing updates. The first element is * the overall percentage finished. The second element is a status message. */ function update_do_updates() { while ($update = reset($_SESSION['update_remaining'])) { $update_finished = update_data($update['module'], $update['version']); if ($update_finished == 1) { // Dequeue the completed update. unset($_SESSION['update_remaining'][key($_SESSION['update_remaining'])]); $update_finished = 0; // Make sure this step isn't counted double } if (timer_read('page') > 1000) { break; } } if ($_SESSION['update_total']) { $percentage = floor(($_SESSION['update_total'] - count($_SESSION['update_remaining']) + $update_finished) / $_SESSION['update_total'] * 100); } else { $percentage = 100; } // When no updates remain, clear the cache. if (!isset($update['module'])) { db_query('DELETE FROM {cache}'); } return array($percentage, isset($update['module']) ? 'Updating ' . $update['module'] . ' module' : 'Updating complete'); }
/** * Database benchmarks for Authcache Ajax phase */ function _authcache_dev_query() { global $queries; if(!$queries) return; $time_query = 0; foreach($queries as $q) { $time_query += $q[1]; } $time_query = round($time_query * 1000, 2); // Convert seconds to milliseconds $percent_query = round(($time_query / timer_read('page')) * 100); return count($queries) . " queries @ {$time_query} ms"; }
/** * MySQL tables optimizing handler. * * @param int $opt * Operation flag. */ public static function cleanerMysqlOptimizing($opt = 0) { $db_type = db_driver(); // Make sure the db type hasn't changed. if ($db_type == 'mysql') { // Gathering tables list. $list = array(); foreach (db_query("SHOW TABLE STATUS") as $table) { if ($table->Data_free) { $list[] = $table->Name; } } if (!empty($list)) { // Run optimization timer. timer_start('cleaner_db_optimization'); // Execute optimize query. $query = 'OPTIMIZE ' . ($opt == 2 ? 'LOCAL ' : ''); $query .= 'TABLE {' . implode('}, {', $list) . '}'; db_query($query); // Write a log about successful optimization into the watchdog. self::cleanerLog('Optimized tables: !opts. This required !time seconds.', array('!opts' => implode(', ', $list), '!time' => number_format(timer_read('cleaner_db_optimization') / 1000, 3))); } else { // Write a log about thing that optimization process is // no tables which can to be optimized. self::cleanerLog('There is no tables which can to be optimized.', array(), WATCHDOG_NOTICE); } } else { // Write a log about thing that optimization process isn't allowed // for non-MySQL databases into the watchdog. self::cleanerLog('Database type (!type) not allowed to be optimized.', array('!type' => $db_type), WATCHDOG_ERROR); } }