This tutorial is for people who want to further optimize and speed up their CloudFusion-based web applications by executing certain methods in parallel.
CloudFusion leverages a class called RequestCore under the hood to manage all of the HTTP requests and responses. RequestCore is built on top of PHP's cURL extension, and therefore has access to the curl_multi_exec() function.
Every CloudFusion method that is capable of making a request to the AWS service has the option of setting a special setting called returnCurlHandle. If you set this setting to true, then instead of executing the HTTP request, CloudFusion will return something called the cURL handle. You can collect the cURL handles of multiple requests, and fire them all at once with the send_multi_request() method.
<?php
require_once 'cloudfusion.class.php';
// Create an array to hold the cURL handles
$handles = array();
/************************************************************/
// Collect cURL handles
$s3 = new AmazonS3();
$handles[] = $s3->list_buckets(true); // set returnCurlHandle to true
$sdb = new AmazonSDB();
$handles[] = $sdb->list_domains(array(
'returnCurlHandle' => true // set returnCurlHandle to true
));
$sqs = new AmazonSQS();
$handles[] = $sqs->list_queues(null, true); // set returnCurlHandle to true
/************************************************************/
// Instantiate a new RequestCore object
$http = new RequestCore();
// Fire all of the handles we collected in parallel.
// Returns an indexed array of all responses.
$all_responses = $http->send_multi_request($handles);
// Loop through the array of responses
foreach ($all_responses as $response)
{
// Parse the response bodies with SimpleXML
$response->body = new SimpleXMLElement($response->body);
}
/************************************************************/
// View the indexed array of responses
header('Content-type: text/plain; charset=utf-8');
print_r($all_responses);
?>
If you're building a web application, you'll want to take a look at your application flow and determine which requests can be run in parallel. For example:
get_object_acl().Firing off linear requests for 200 objects (for example) at 300ms per request would take 60 seconds (200 x 300ms). However, firing them off in parallel only takes as long as the slowest request (e.g. 300ms).
Add in caching (another topic for another tutorial), and you could reduce the load time (for subsequent requests) by nearly 10x (e.g. 30ms). Properly using parallel requests and caching can substantially improve your application performance.
CloudFusion™ is © 2007–2010 Ryan Parman, Foleeo Inc., and contributors. The CloudFusion name and logo are trademarks of Ryan Parman. WarpShare™ is a trademark of Foleeo Inc. The source code is provided under the New BSD License. Documentation and tutorial text is provided under the [CC] BY-NC-SA license. All other rights are reserved.