Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CurlMutlirunner burn CPU #55

Open
jderusse opened this issue Apr 9, 2019 · 5 comments
Open

CurlMutlirunner burn CPU #55

jderusse opened this issue Apr 9, 2019 · 5 comments
Assignees

Comments

@jderusse
Copy link

jderusse commented Apr 9, 2019

given the following snippet

#!/usr/bin/env php
<?php

require_once "vendor/autoload.php";

use Http\Client\Curl\Client;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
$messageFactory = MessageFactoryDiscovery::find();
$client = new Client($messageFactory, StreamFactoryDiscovery::find());

$client->sendAsyncRequest(
    $messageFactory->createRequest('GET', 'https://httpstat.us/200?sleep=3000')
)->wait(true)->getBody()->getContents();

the service" https://httpstat.us/200?sleep=3000 simulate a slow request waiting for 3 seconds.

Where running the script, my application consume A LOT of CPU while waiting for the request

$ time ./index.php                                                                                                                                                                                                                                                                                
real    0m3.190s
user    0m1.638s
sys     0m1.523s

Here CPU is used 1.638s + 1.523s = 3.161s to process this 3.190s script

when applying this durty patch

diff --git a/src/MultiRunner.php b/src/MultiRunner.php
index 545f39a..5872804 100644
--- a/src/MultiRunner.php
+++ b/src/MultiRunner.php
@@ -82,6 +82,7 @@ class MultiRunner
     public function wait(PromiseCore $targetCore = null)
     {
         do {
+            usleep(1);
             $status = curl_multi_exec($this->multiHandle, $active);
             $info = curl_multi_info_read($this->multiHandle);
             if (false !== $info) {

I get

$ time ./index.php
real    0m3.163s
user    0m0.234s
sys     0m0.211s

now, the CPU is used 0.445s to process the same request.

@mekras mekras self-assigned this Apr 9, 2019
@mekras
Copy link
Collaborator

mekras commented Apr 9, 2019

I'll check this today.

@joelwurtz
Copy link
Member

joelwurtz commented Apr 9, 2019

Why there is no curl_multi_select ? Burning cpu seems to be normal here since we never wait for any activity and loop until something happens.

I think adding a simple curl_multi_select($this->multiHandle); after curl_multi_exec will do the trick

mekras added a commit that referenced this issue Apr 10, 2019
@mekras
Copy link
Collaborator

mekras commented Apr 10, 2019

@jderusse, can you try changed version from branch https://github.com/php-http/curl-client/tree/issue-55-CurlMultirunner_burn_CPU ?

mekras added a commit that referenced this issue Apr 10, 2019
@mekras
Copy link
Collaborator

mekras commented Apr 10, 2019

@jderusse, I've update branch. Can you check it?

@jderusse
Copy link
Author

Would you mind to open a PR @mekras ? It would be easier to review :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants