如何使用 guzzle promise 改善响应时间?

How to improve response time using guzzle promise?

我在这里寻求帮助,以了解我的代码是否正确。因为我看不到响应时间有多大改善。我使用 guzzle promise 降低了我的整体性能。

现有代码:

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. 
              }
        }
    }

使用 guzzle promise 回调的新代码:


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;
    }

这个新代码试图调用 NewClass::ClassSampleFunction($image->path, $this->samplePoint); 这个昂贵的函数调用,使用 guzzle promise 没有外部端点。但是在此之后还期望响应时间有所减少但没有变化,而有时它比旧代码更多。

提到函数调用 NewClass::ClassSampleFunction($image->path, $this->samplePoint); 这个 运行 假设我有第一个 for 循环 4 范围,第二个有 10 个每个第一个 for 循环,然后总共调用了 40 次这个函数。如果我使用异步调用,那么第一次调用会在没有完成的情况下完成,反之亦然。这有助于在旧代码中使用等待时间

我做错了什么?有什么帮助或建议吗?

最后,我能够通过使用 guzzle/promise 中的 eachPromise 在一定程度上提高性能。

修改代码:

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;
    }

参考文章 : https://medium.com/@ardanirohman/how-to-handle-async-request-concurrency-with-promise-in-guzzle-6-cac10d76220e