swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.42k stars 3.16k forks source link

Curl performance (with and without CURL support installation option) #5047

Closed santossoares closed 1 year ago

santossoares commented 1 year ago

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.

I'm testing the curl performance with the "--enable-swoole-curl" option. I installed swoole via pecl with the curl support.

I run this script:


Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL);

run(function() {
    for ($i = 0; $i < 500; $i++) {
        go(function() {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'https://httpbin.org/get');
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $result = curl_exec($ch);
            curl_close($ch);
            var_dump($result);
        });
    }
});

If i ran the same script with the curl support disabled or the with the option to use the old curl hook, Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_CURL); the scripts runs much faster. It looks like the old hook creates multiple processes/threads to handle the concurrency.

Is this supposed to happen?

  1. What did you expect to see? I expected the newer curl support (SWOOLE_HOOK_NATIVE_CURL) to run faster than the old one.

  2. What did you see instead? The old curl hook runs faster.

  3. What version of Swoole are you using (show your php --ri swoole)? swoole

Swoole => enabled Author => Swoole Team team@swoole.com Version => 5.0.2 Built => May 7 2023 19:24:11 coroutine => enabled with boost asm context epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled openssl => OpenSSL 3.0.5 5 Jul 2022 dtls => enabled http2 => enabled json => enabled curl-native => enabled mutex_timedlock => enabled pthread_barrier => enabled futex => enabled mysqlnd => enabled async_redis => enabled

Directive => Local Value => Master Value swoole.enable_coroutine => On => On swoole.enable_library => On => On swoole.enable_preemptive_scheduler => Off => Off swoole.display_errors => On => On swoole.use_shortname => On => On swoole.unixsock_buffer_size => 8388608 => 8388608

  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ?

Linux swoole 5.19.0-23-generic #24-Ubuntu SMP PREEMPT_DYNAMIC Fri Oct 14 15:39:57 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

PHP 8.1.7-1ubuntu3.3 (cli) (built: Feb 22 2023 22:55:33) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.7, Copyright (c) Zend Technologies with Zend OPcache v8.1.7-1ubuntu3.3, Copyright (c), by Zend Technologies

sing built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/12/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 12.2.0-3ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-12/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-12 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-12-U8K4Qv/gcc-12-12.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-12-U8K4Qv/gcc-12-12.2.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 12.2.0 (Ubuntu 12.2.0-3ubuntu1)

matyhtf commented 1 year ago

No, curl-native is also an event-driven non-blocking call, and will not create a thread/process.

The performance of curl-native hook is indeed lower than the curl hook, but the bottom layer of curl-native directly uses libcurl, which has higher compatibility and more complete functions

0utc0ntr0l2021 commented 1 year ago

u can make ur curl request with coroutine by use Swoole\Coroutine\Http\Client u can make multi connection in same time

leocavalcante commented 1 year ago

To complement @0utc0ntr0l2021:

Swoole's cURL Native helps us a lot to still have asynchronous calls inside coroutines using AWS SDK and maintain compatibility, while for our own HTTP clients we can benefit from Swoole\Coroutine\Http\Client performance.

santossoares commented 1 year ago

Thanks guys!