Location>code7788 >text

What makes PHP's high-performance framework, Workerman, stand up to Swoole?

Popularity:852 ℃/2024-07-22 09:30:52

Hi everyone, I'm Codemaster Pioneer.

Once I happened to see the performance test ranking of PHP peripheral ecological frameworks and extensions by a foreign organization, I saw that Workerman was way ahead of Swoole, which was actually lagging behind the performance of extensions based on the C/C++ language in the existing knowledge of us PHP programmers. When I first saw the result, I couldn't calm down for a long time, and my mind wondered, "Is C/C++ worse than PHP? I thought to myself, "Is C/C++ worse than PHP?

Speaking of Workerman and Swoole reminds me of the poorly executed PHP-FPM, which for years has had nothing to do with asynchronous communication, but I'll tell you why it's so great that Workerman has made it to the top of the testing charts, so let me tell you why. I'll tell you why. Before we get into Workerman, let's talk about the current state of PHP-FPM.

PHP-FPM is a PHP process manager based on a multi-process model, where each process is single-threaded and can only handle one request at a time, making it impossible to take full advantage of concurrent multi-core CPU processing. Moreover, the process model is still in the form of IO synchronous blocking, and you have to wait for IO operations. PHP as an interpreted language requires initializing the environment, calling MINIT of each extension module, parsing the compiled code, and connecting to the database resources for each request, and then releasing the resources, destroying all defined classes, instances, and symbol lists, etc., and then calling the RSHUTDOWN of each extension module in order. After the request is processed, it releases resources, destroys all defined classes, instances, symbol tables, and so on, and then calls RSHUTDOWN of each extension module in sequence, and finally returns the result generated by the request to the proxy service, such as Nginx, Apache, etc. This operation mode of PHP-FPM, with its frequent creation and destruction of resources, will lead to high memory usage and low execution efficiency, which will be fatal when the system is in the situation of high concurrency and high load.

After looking at the current state of PHP-FPM from time to time to sigh Workerman really hate each other ah, PHP programmers have been suffering PHP-FPM for a long time. A lot of people say that Workerman is high performance, and the official claim is that under AB stress tests the QPS exceeds that of Nginx alone, but how many people know why it's high performance? How is it better than PHP-FPM? You may not be able to tell, I'll explain, but before we do, we need to understand the IO multiplexing technology.

multiplexed

IO multiplexing is a mechanism for monitoring multiple IO streams at the same time. The core idea is to monitor multiple IO operations at the same time through a single system call. Specifically, IO multiplexing allows a process to monitor multiple file descriptors, such as socket sockets, at the same time, and only perform real IO operations when at least one of the file descriptors is ready to be read, written, or an exception is thrown, etc. IO multiplexing allows a program to read and write to or from a file without blocking the execution of the entire process when it encounters an IO operation such as a MySQL read/write, a Redis operation, a network request, a file read, or so on. IO multiplexing allows programs to perform IO operations such as MySQL reads and writes, Redis operations, network requests, file reads, and so on, without blocking the entire process. The familiar Redis, Nginx, and Go all use this model.

If you are confused about IO multiplexing, we recommend that you look at other related information on the Internet. Now we only need to know that this technology is very "awesome", but as long as it involves high-performance programs or software will certainly use IO multiplexing technology. That's right, Workerman is the application of IO multiplexing technology in its own underlying architecture, standing on the shoulders of giants to create Workerman, which is the fundamental reason for the high performance of Workerman. There are also some other factors, such as resident process mode, no need to reload files and other resources into memory, global variables need to be initialized only once, etc. With these technologies, Workerman is far ahead of PHP-FPM in terms of performance, but going back to what we said at the beginning, in the performance test of a certain organization, "Workerman is way ahead of Swoole". What is the reason for this? Let me tell you!

Workerman uses IO multiplexing, doesn't Swoole know about it? Since Swoole also claims to be a high-performance asynchronous communication framework, it must also use IO multiplexing, and Swoole does not just use this technique, but it embodies it to the fullest extent throughout Swoole, and even the concurrent processes that Swoole is so proud of are based on the event loop mechanism "EventLoop". Since this technology is used, it is reasonable to assume that the performance of Swoole should not be bad! Analyzing the essential differences between the two, Workerman utilizes pcntl and posix extensions to achieve process management, which is actually based on PHP implementation. Swoole is based on C/C++ language implementation of the extension program, is in the form of extension modules in PHP, in the process management is also completely C/C++ language implementation.

Cause analysis

From the test results of an organization, Workerman is stronger than Swoole performance, I think there are several reasons. First: from the source code of Workerman and Swoole implementation architecture, Workerman's architecture is more concise code less, on the contrary, Swoole's C/C++ code is more internal processing logic is more complex, Workerman essentially utilizes PHP basic extension pcntl, posix extension, and Swoole itself is self-implemented extension module, from the actual situation often basic extension module than third-party extension module, and Swoole is more complicated than the basic extension module. Workerman essentially utilizes PHP's basic extensions pcntl and posix, while Swoole itself is a self-implemented extension module. From a practical point of view, basic extensions are often more stable and reliable than third-party extensions in terms of resource management. Secondly, in single-process mode, Swoole has no way to utilize multi-core CPU resources, so Swoole's "coprogramming" is not useful, so Swoole's performance will be slightly inferior to that of Workerman in this case. Thirdly, from the point of view of the functions provided by the two, Workerman does not have similar concurrency management, concurrent management, channel management, channel communication, inter-process communication and other underlying functions of Swoole, these redundant functions in the operation of the program there is a certain amount of system overhead, when the program's complexity is increased, it is also obvious that the impact on the performance and efficiency of the entire service.

Swoole is still an excellent extension to PHP for asynchronous communication, even though it is better than Workerman under certain conditions; Swoole offers more features, such as the ability to manually asynchronize your program with a concatenation, the ability to create a pool of database connections to increase the reuse of connection resources, and the ability to use concatenated inter-process communication to share memory resources. and so on. Simply put, each has its own advantages and disadvantages, and we should combine them with the current business scenarios to make the right choice in the actual technical options.

concluding remarks

Finally, I would like to express my personal opinion that Workerman is more suitable for PHP beginners to get in touch with the field of network communication, there are not so many concepts such as concurrent, process, event loop, asynchronous blocking and non-blocking which are difficult to understand, so it is easy to use directly and get started quickly. On the contrary, Swoole many people stop at the installation of extensions, extensions to install the deployment of the environment are half dead, not to mention the use of Swoole is more suitable for long-term programming in the Linux environment, and the operating system, network programming, network protocols have a certain basic people. Some people look down on PHP from the bottom of their hearts and think that Swoole, which is based on C/C++, is more advanced, and that they want to learn the most "awesome" one. Often, this kind of mentality of wanting to run before learning how to walk, results in the most tragic fall. Through this article to see the implementation of PHP itself based on the Workerman is not very bad, so we do what we can, the shoes fit only in their own feet to know, do not put the brand-name shoes hard on their feet, the final result is not as satisfactory as the loss.

Thanks for reading and I hope this will be enlightening.


Welcome to follow, share, like, favorite, in the watch, I'm the author of WeChat public number "code farmer forefather".