在 Guzzle 传输中获得 bytes/sec

Getting bytes/sec on a Guzzle transfer

是否可以计算 Guzzle 4 或 5 创建的 GET 请求的 bytes/sec?

如果是这样,这是怎么做到的?

回答你的问题,是的,可以计算数据速率。

假设使用 Guzzle 5,并且来自 ProgressEvent 上的 Guzzle Documentation on Events

You can access the emitted progress values using the corresponding public properties of the event object:

$downloadSize: The number of bytes that will be downloaded (if known)
$downloaded: The number of bytes that have been downloaded
$uploadSize: The number of bytes that will be uploaded (if known)
$uploaded: The number of bytes that have been uploaded

理论上,使用 EventSubscriber(整洁、推荐)或将闭包传递给事件发射器(不那么整洁)是:

  1. 在 BeforeEvent 开始计时
  2. 使用计时器值和 ProgressEvent::downloaded 计算数据速率。
  3. 在 CompleteEvent 中停止计时器