online job

Monday 24 March 2014

Improving Application Performance

Improving Application Performance


API call isn’t executed until after batch_end() is called. At that point, the references are
filled in with the actual results. In local tests, batching the API calls was about 5–6 times
faster than individually executing the calls.
Listing 16.1 Batching API Calls
$users = array_slice( $facebook->api_client->friends_list, 0, 10 );
separateCalls( $facebook, $users );
batchedCalls( $facebook, $users );
function separateCalls( $facebook, $users ) {
$results = array();
foreach ( $users as $userID ) {
$results[] = $facebook->api_client->profile_setFBML( NULL, $userID,
"Some FBML", NULL, NULL, NULL );
}
}
function batchedCalls( $facebook, $users ){
$results = array();
$facebook->api_client->begin_batch();
foreach ( $users as $userID ) {
$results[] = & $facebook->api_client->profile_setFBML( NULL, $userID,
"Some FBML", NULL, NULL, NULL );
}
$facebook->api_client->end_batch();
}
Note
Currently, there is a bug in the Facebook PHP client library with the profile_setFBML()
declaration. It doesn’t return its value as a reference, so the results array in the
batchedCalls() function won’t be set. You can fix this by changing the declaration to include
an & in facebookapi_php5_restlib.php:
function &profile_setFBML($markup, ...
There are a few constraints with batching. First, you can only batch 20 API calls at a
time. Second, you cannot use the results of one API call in another API call in the same
batch (for example, calling the friends_getAppUsers() function and then using the IDs
returned as parameters in a call to the users_getInfo() function).The solution to this
problem is to use FQL, which the next section covers.You can, however, force the batched
calls to be executed sequentially instead of in parallel, which is the default. Do this by setting
the batch_mode variable like this:
$facebook->api_client->batch_mode =
FacebookRestClient::BATCH_MODE_SERIAL_ONLY;

No comments: