I am looking for help here to understand whether my code is right approach or not. Because I can't see much improvement in response time. I used guzzle promise to reduce my overall performance.
Existing code :
protected function sampleFunction(&$outputArray){
foreach($outputArray as $key => $images){
foreach($images as $index => $image){
$result = NewClass::ClassSampleFunction($image->path, $this->samplePoint); // This is expensive function calls in current code.
}
}
}
New code using guzzle promise callback :
protected function sampleFunction(&$outputArray){
foreach($outputArray as $key => $images){
$promises = array();
foreach($images as $index => $image){
$promises[$index] = $this->promiseFunction($image,$index);
}
$results = \GuzzleHttp\Promise\settle($promises)->wait();
foreach ($results as $resultkey => $value) {
$search_point_image_xy_array = $value['value']->toArray();
// some logic to use result and put in another response array.
}
}
}
protected function promiseFunction($image,$index){
$promise = new Promise(
function () use ($image,$index,&$promise) {
$response = NewClass::ClassSampleFunction($image->path, $this->samplePoint);
$promise->resolve($response);
},
function () {
// do something that will cancel the promise computation (e.g., close
// a socket, cancel a database query, etc...)
}
);
return $promise;
}
This new code trying to call NewClass::ClassSampleFunction($image->path, $this->samplePoint);
this expensive function call using guzzle promise without external endpoint. But after this also expected some reduction in response time but not changes whereas sometime its more than old code.
Mention function call NewClass::ClassSampleFunction($image->path, $this->samplePoint);
this run suppose I have first for loop 4 range and second has 10 per first for loop then total 40 times calls goes to this function. If I used async call then First call goes without it finish second goes and vice versa. This can help to use waiting time in old code
What I am doing wrongly ? Any help or recommendation ?
Finally I able to improve performance by some level by using eachPromise
from guzzle/promise
.
Revise code :
protected function sampleFunction(&$outputArray){
foreach($outputArray as $key => $images){
$promises = array();
foreach($images as $index => $image){
$promises[$index] = $this->promiseFunction($image,$index);
}
$eachPromise = new EachPromise($promises, [
// how many concurrency we are use
'concurrency' => count($promises),
'fulfilled' => function ($response,$index) use (&$images, &$outputArray)
{
//logic for other operation.
},
'rejected' => function ($reason) {
}
]);
}
}
protected function promiseFunction($image,$index){
$promise = new Promise(
function () use ($image,$index,&$promise) {
$response = NewClass::ClassSampleFunction($image->path, $this->samplePoint);
$promise->resolve($response);
},
function () {
// do something that will cancel the promise computation (e.g., close
// a socket, cancel a database query, etc...)
}
);
return $promise;
}
Reference article : https://medium.com/@ardanirohman/how-to-handle-async-request-concurrency-with-promise-in-guzzle-6-cac10d76220e