Research and Practice of Swoole Asynchronous Multithreading Design Method
Research and Practice of Swoole Asynchronous Multithreading Design Method
Zhengming Chen*
School of Information Science and Engineering
Shaoguan University
Shaoguan, China
e-mail: [email protected]
2164
Authorized licensed use limited to: Inst. Federal de Educ. Ciência e Tecn. do RS. Downloaded on April 08,2024 at 22:07:57 UTC from IEEE Xplore. Restrictions apply.
IO. If you use the native function of PHP to read the disk or to implement asynchronous programs, so PHP code does not
network, there will be a waiting time. For example, the need to add any additional keywords, the underlying can
slowest disk reading may take more than 10ms. In most automatically process the process, asynchronous.
current scenarios , the Intranet is roughly 1ms~10ms and the
Extranet 100ms is possible. But the PHP code cannot do B. Swoole Application Example
anything in this event, it can only wait for the data to return, 1) Create a new TCP/UDP server
and the resource utilization is low. In practical applications , Create a new TCP/UDP server in Swoole. Use
processes are often opened to solve concurrency , However , swoole_server("127.0.0.1",9501) to create a new server
it may cause process competition for system resources . A object that listens on local port 9501. After the new object is
large number of work processes wastes a lot of CPU completed, you need to set the object parameters, including
resources , and too many resources is wasted in the process the number of worker processes started by the server, the
switching . number of task processes opened, and set related callbacks
Swoole's asynchronous multi-threading design approach such as 'connect', 'receive', 'close', 'task', 'finish', etc. function.
can solve the above problem : When the program reads the At least the 'task' and 'finish' callback functions must be set
network or disk, Swoole will join the event listener , no before using 'task', where 'connect' is the listener connection
longer to wait anymore , the subsequent code can do other entry event and 'receive' is the listen data receive event, and
things . When the network or disk read completes returning so on. After the setting is completed, the server object can be
data , execute the code that requires this data down. used for task delivery. The specific execution logic of the
Because the PHP language does not support task is defined in the 'task' callback function. For example, in
multithreading , Swoole can use multi-process mode , there the process of sending information by the client and
is process memory isolation from multi-process mode , and performing the task delivery after receiving the information
shared memory can be used for communication between by the server, the client first connects with the server, and
threads [21]. after successful, sends data by using 'send', triggering the
Therefore, Swoole can be used as an asynchronous non- server's 'receive' callback function. In the 'receive' call task
blocking network communication server. Like the Figure 1 delivery processing specific logic, such as: send mail,
shows that ln the initialization phase, Swoole will first create processing data of excle table, etc. may have high
the main process master, load the relevant configuration, and concurrency function.
register all necessary callback functions to the main server. There are four parameters in the 'task' callback definition,
Then start the onStartx callback function in the main process which are $serv, $task_id, $from_id, $data. When calling
and trigger the onWorkerStart callback [22]. 'task', you only need to fill in the fourth parameter, $data, and
the rest of the system will be filled automatically. Processing
the function logic in the 'task' callback needs to return the
result of the task execution, and returning a non-null value in
the 'task' callback indicates that the posted task has
completed.
There are three parameters in the definition of 'finish'
callback, which are $serv, $task_id, $data. This callback is
automatically called at the end of each task. $data is the
value of renturn in the task, and the rest of the parameters are
automatically populated. Remember, after the setup is
complete, you must use the server object to open the service
at the end, such as $serv->start();
2) Create a new TCP client
Create a new TCP client in Swoole. Use
swoole_client(TCP, ASYNC) to create a new client object
and call connect('127.0.0.1', 9503, 1) to complete the
connection with the server. This is established as an
Figure 1. Swoole running flowchart asynchronous client, which is non-blocking. Asynchronous
clients need to set the callback function. There are 4 event
Asynchronous non-blocking in Swoole is single-process
callbacks that must be set to onConnect, onError, onReceive,
single-threaded. All socket operations are a callback function
onClose. Triggered when the client connection is successful,
and return directly, so the process will continue to execute
the connection fails, the data is received, and the connection
until epoll_wait. Then, the event polling is started. When a
is closed. On the server side, you can use 'sendto' to send
network event arrives, a corresponding socket can read the
information to the client. The client can use the client object
data and execute the corresponding callback function. A new
to call recv() to receive and output the page.
socket can be registered in the callback function to poll the
3) Server in Swoole
epoll event.
Now the latest version of the Swoole extension supports You can also create a web server, a WebSocket server in
coroutines like Go, which can use asynchronous design code Swoole. The Http server only needs to pay attention to the
request response, so only need to listen to an onRequest
2165
Authorized licensed use limited to: Inst. Federal de Educ. Ciência e Tecn. do RS. Downloaded on April 08,2024 at 22:07:57 UTC from IEEE Xplore. Restrictions apply.
event. This event is fired whenever a new Http request B. Implement Asynchronous SMS Sending
comes in. The WebSocket server is a long connection server The background processing short message sending
built on top of the Http server. The client first sends a Http logic adopts the asynchronous task delivery , which is
request to handshake with the server. After the handshake is
reflected on the fact that the user can immediately return the
successful, the onOpen event will be triggered, indicating
sending success or the countdown after clicking and
that the connection is ready. The onOpen function can get
the $request object, which contains information about the sending , without waiting for the third party platform to send
Http handshake, such as GET parameters, cookies, and Http the logic execution completion , returning the success flags /
header information. flagged to execute the other code .
The specific steps are as follows: after judging and
C. Summary obtaining the mobile phone number transmitted by the front-
Swoole provides asynchronous task processing, mostly end JS, generating a 4-digit verification code random number,
for handling high concurrency or time-consuming tasks, If assembling the json data format to the server object in the
you need to perform more time-consuming operations in the $_POST for asynchronous task delivery, and corresponding
Server program, such as group sending information, directly mobile phone number and verification code Store with redis
executing the logic code will block the current process, for subsequent verification. After the task delivery is
causing the server to respond slowly, but Swoole can post an completed, whether or not it is blocked, the transmission
asynchronous task to the TaskWorker process pool without success flag is immediately returned, and the tasks do not
affecting the processing speed of the current request. interfere with each other. Within the range that the server can
bear, multiple users can simultaneously send information and
IV. IMPLEMENTATION OF SERVER AND TEST OF SWOOLE immediately receive the transmission success flag.
Up to now, there are already mature programs in the The task processing logic called when sending SMS is
development of online chat rooms using PHP and Swoole already defined when the server is newly created. Since there
[23]. This chat room case has a single function and is are usually multiple functions in the system that need to use
designed to verify its excellent high concurrency the task, the encapsulated processing logic is called in the
performance by using asynchronous multithreading in callback, and is called using the method name and data.
Swoole. The main modules are: set up a local server, realize Therefore, when sending a short message, you only need to
SMS verification registration and login, push live events to pass the sendSms method name and data data to perform
each user in real time, and multi-user online live chat. asynchronous task delivery.
A. Build a Local Server C. Realize Live Events and Chat room Scenes
Before setting up the server, you need to install Swoole The built server has opened port 8811, which is used to
under Linux source. Before that, you need to install PHP. instantly push live events to each user. First create a client in
After the PHP installation is successful, you can install the JS, write the client connection address, such as
source for Swoole and configure the rest of the extensions ws://127.0.0.1:8811 and ws://127.0.0.1:8812, and then
such as redis. During installation, the remaining extensions instantiate the address. You also need to set the client's
can be added during configuration according to the order of 'onopen', 'onmessage', 'onclose', 'onerror' and other callback
configuration, compilation, and installation. After the events. Each time the user accesses the page, each
installation is complete, add extensions to the PHP corresponding port on the server side has a unique identifier
configuration file to use. of $fd. When the user accesses, the $fd accessed by the
Use swoole_websocket_server("0.0.0.0",8811) to open different port is stored in the redis by the identifier for
port 8881 and listen to all IPs, Calling to listen("0.0.0.0", pushing information. After the JS code of the connection
8812) with a server object can listen to another port at the server is introduced to the front-end page , a long connection
same time. Open two ports for asynchronous logic that is automatically established with the server when the user
implements different functions on the same page. Also need accesses the page. When pushing the live event information
to set the relevant parameters of the server, here set the through the front-end page, the server takes out all the $fd
number of starting work processes of 4, open the accessed through the corresponding port, pushes the
asynchronous task and set the number of task processes of 4 information through the server object, sending the assembled
and open the static directory support, 'enable_static_handler ' information to the user page of the corresponding port, and
=> true , specify the accessible static through 'document_root can dynamically add the page label by using JS. It is
' table of Contents. You also need to set the callback reflected in the fact that when the server pushes information,
functions 'open', 'message', 'workerstart', 'request', 'task', the user interface dynamically adds the assembled
'finish', 'close'. In the server page, the server object is stored information.
in $_POST, so that the task can be posted on other pages. Multi-user online lives chat uses port 8812, which also
After the relevant settings are completed, a simple local establishes client connection in JS. Since the live event and
server is set up, and the relevant directory can be accessed the online chat is on the same page, after the page is
through the IP+ port number. introduced, the client automatically establishes two long
connections with the server each time the user accesses. The
2166
Authorized licensed use limited to: Inst. Federal de Educ. Ciência e Tecn. do RS. Downloaded on April 08,2024 at 22:07:57 UTC from IEEE Xplore. Restrictions apply.
processing logic here is similar to another client code, except response time are 4645.218 ms. In a test with a total of 2000
that the message is assembled and added for a specific concurrent users entering the login page, there is sometimes
scenario. The server side uniformly adopts 'push' for message an average processing time of 47.426 ms and an average
push, and the $fd identifier is handed over to different clients response time of 47425.604 ms, which may be accompanied
for processing. The $fd recorded by different ports can be by a few failed requests.
stored in redis using a specific flag, or the 'connection' The second test are to simulate a page with a total of
iterator can be used to traverse the $fd of a specific port 1000 concurrent amounts of 100 and a total of 2000
access, call port[1]->connections with the server object, and concurrent 1000s to access the successfully registered page,
traverse through 8812 through foreach. The port records $fd. that is, online at the same time. Table 3 shows the results of
the second test:
D. Test the Concurrency of the Swoole Server
The test here is the ability of the server built with swoole TABLE III. ONLINE PAGE CONCURRENT ANALYSIS
to bear the concurrency. The number of worker processes Average
started by the swoole-http-server server is 4, and the number Number of Concurrent
processing Average response time
virtual users number
of task processes opened is 4. The Table 1 illustrates the test time
environment. The Server Bandwidth is 1 Mbs, the CPU is 1 1000 100 81.426 ms 8142.621 ms
2000 1000 83.746 ms 83746.291 ms
core, and the RAM is 2G. The Client RAM is 2G, the Net-
work card is 10M/100M.
The test here is a successful login, ie online page. Here
TABLE I. TEST ENVIRONMENT each page needs to establish two long connections to the
server for two different types of information push. Therefore,
Machine category Machine configuration
the system processing and response are more delayed than
the login page. When a user with a total of 1000 concurrent
100 accesses the online page, the average processing time of
Operating system:Ubuntu 16.04 64-bit;
the system and the average response time of the user are
Server (one) CPU:1 core;
several times that of the login page. When a user with a total
RAM:2 GB;Bandwidth:1 Mbps of 1000 concurrent 100 accesses the online page, the average
Operating system:Windows 10 processing time of the system and the average response time
Client (one) Professional version 64-bit of the user is several times that of the login page, which is
RAM:4.00 GB;Network card 10 M/100 M approximately 81.426 ms and 8142.621 ms, but under the
test of a total amount of 2000 concurrent 1000, the data
Based on the above environment, a simple stress test was fluctuates, sometimes with an average processing time of 90
performed on the server built by Swoole. Mainly to verify ms or even 100 ms, with an average response time of 90000
the feasibility of PHP+Swoole to build a high concurrent ms or even 100000 ms. When the concurrency is too large,
server, and initially test the concurrent performance to the processing time and response time will be unstable,
facilitate the verification of concurrent performance in the which is mainly reflected on the user requests delay, and
future. Test one, a user who simulates a total of 1000 there will be a few failure requests.
concurrent amounts 100 and a total amount of 2000 Test three, a user simulating a total of 1000 concurrent
concurrent 1000s respectively enters the login page. The test amounts 100 and a total amount of 2000 concurrent 1000s
results are shown in Table 2: simultaneously transmits information. The back-end message
push function is tested here. The original logic needs to
TABLE II. ORDINARY PAGE CONCURRENCY ANALYSIS obtain the post value passed by the front-end, that is, the user
name and content, and reassemble the information push in a
Average
Number of Concurrent
processing Average response time certain format, but here, for the convenience of testing, the
virtual users number user name and content are randomly generated, that is, direct
time
1000 100 27.083 ms 2708.260 ms access to the back-end logical processing page can push
messages to existing clients, and mainly test the
2000 1000 27.629 ms 27629.209 ms asynchronous push performance of the system.
That is, when the total amount of 1000 concurrent
quantity 100 and the total amount of 2000 concurrent
The login page is tested here, and the results indicates quantity 1000 are simultaneously pushed to the opened client,
that within the range that the server can afford, as the number the results of this test are shown in Table 4.
of users and the amount of concurrency increaseed, the
average processing time of the system fluctuates little, but TABLE IV. PUSH LOGIC CONCURRENT ANALYSIS
the average response time of users increased significantly.
Average
During the test, due to fluctuations in the client's network Number of Concurrent
processing Average response time
card traffic and limited bandwidth on the server side, virtual users number
time
occasionally a long delay occurred. In a test with a total of 1000 100 11.271 ms 1127.147 ms
1000 concurrent users entering the login page, the average 2000 1000 11.308 ms 11308.217 ms
processing time is sometimes 46.452 ms and the average
2167
Authorized licensed use limited to: Inst. Federal de Educ. Ciência e Tecn. do RS. Downloaded on April 08,2024 at 22:07:57 UTC from IEEE Xplore. Restrictions apply.
The simulation here is that multiple users send (PDP), 2015 23rd Euromicro International Conference on. IEEE,
information online at the same time, in which asynchronous 2015: 155-162.
task task delivery is used for each client push information. [4] Ke Y, Wang Y, Li Y. A High Performance Web Server Based on
Asynchronous and Zero-copy[J]. Applied Mechanics & Materials,
When a user with a total of 1000 concurrent amounts of 100 2014.
simultaneously sends a message, the system average [5] El-Samad W M A, El-Aziz M M A. Evaluation of Multithreading in
processing time and the average user response time Multicore Processors[J]. Journal of Emerging Trends in Engineering
sometimes fluctuates. Under the premise that the task and Applied Sciences (JETEAS), 2017, 8(2): 58-63.
delivery capacity does not exceed the processing capability, [6] Lorenzon A F, Cera M C, Beck A C S. Performance and energy
that is, the number of operations is less than the onTask evaluation of different multi-threading interfaces in embedded and
processing speed, the number of clients has little effect on general purpose systems[J]. Journal of Signal Processing Systems,
2015, 80(3): 295-307.
the efficiency of message push, that is, the delay effect of
opening a client and opening ten clients to message push Not [7] Shepilov Y, Pavlova D, Kazanskaia D. Multithreading MAS Platform
for Real-Time Scheduling[J]. International Journal of Software
big. Innovation (IJSI), 2016, 4(1): 48-60.
The above simple chat room case based on Swoole is [8] Li T, Ren Y, Yu D, et al. Resources-conscious asynchronous high-
configured under the Linux server platform. After the AB speed data transfer in multicore systems: Design, optimizations, and
stress test, the logically simple login page can support a total evaluation[C]//2015 IEEE International Parallel and Distributed
of 2000, and the number of concurrent users is 1000. In Processing Symposium (IPDPS). IEEE, 2015: 1097-1106.
contrast, under the same total amount and concurrency, the [9] Yeung M M, Chen Y K. Methods for reducing energy consumption of
user's online page needs to establish two long connections buffered applications using simultaneous multi-threading processor:
U.S. Patent 9,323,571[P]. 2016-4-26.
with the server. The system processes the request several
[10] Frazier G R, Indukuru V R. Initiating assist thread upon asynchronous
times as long as the login page, and the delay sometimes event for processing simultaneously with controlling thread and
fluctuates. For asynchronous message push, in the simulation updating its running status in status register: U.S. Patent 9,152,426[P].
total 1000 and the concurrent number 100 test, the average 2015-10-6.
time for the server to process the backend message push [11] Halstead R J, Absalyamov I, Najjar W A, et al. FPGA-based
(where the push logic is simplified) is approximately 11.271 Multithreading for In-Memory Hash Joins[C]//CIDR. 2015.
ms, and the average user response time is approximately [12] Vaidyanathan K, Kalamkar D D, Pamnany K, et al. Improving
1127.147 ms. concurrency and asynchrony in multithreaded MPI applications using
software offloading[C]//Proceedings of the International Conference
V. IN CONCLUSION for High Performance Computing, Networking, Storage and Analysis.
ACM, 2015: 30.
This paper mainly studies the implementation principle [13] Si M, Pena A J, Hammond J, et al. Casper: An asynchronous progress
of Swoole asynchronous multi-threading, and designs and model for MPI RMA on many-core architectures[C]//Parallel and
implements a real-time chat room based on asynchronous Distributed Processing Symposium (IPDPS), 2015 IEEE International.
high concurrent Swoole to verify its effect. The results show IEEE, 2015: 665-676.
that PHP + Swoole can replace Web services with complex [14] Chaimov N, Ibrahim K Z, Williams S, et al. Exploiting
communication concurrency on high performance computing
programming languages such as C++ and Java, providing systems[C]//Proceedings of the Sixth International Workshop on
high-concurrency asynchronous design to improve Programming Models and Applications for Multicores and
development efficiency. This means that PHP is no longer Manycores. ACM, 2015: 132-143.
limited to the development of background functions, can [15] Lee W J, Shin Y, Hwang S J, et al. Reorder buffer: an energy-
achieve network communication functions, so PHP can be efficient multithreading architecture for hardware MIMD ray
based on Swoole to achieve functions that PHP could not traversal[C]//Proceedings of the 7th Conference on High-Performance
Graphics. ACM, 2015: 21-32.
achieve in the past. It can be said that Swoole redefines PHP.
[16] Alur R. Principles of cyber-physical systems[M]. MIT Press, 2015.
Based on the operating principle and test data of Swoole in
[17] Warrier S. Asynchronous connection handling in a multi-threaded
this paper, more scenarios will be used in future experiments server: U.S. Patent Application 15/460,852[P]. 2018-9-20.
to evaluate the performance of Swoole.
[18] Alim A, Clegg R G. Flick: Developing and running application-
specific network services[J]. 2016.
ACKNOWLEDGMENT
[19] Gupta M, Lowell D, Kalamatianos J, et al. Compiler techniques to
This research was supported by the program of National reduce the synchronization overhead of gpu redundant
innovation and entrepreneurship training of undergraduate multithreading[C]//Design Automation Conference (DAC), 2017 54th
student of Shaoguan University(Grant No.201810576012). ACM/EDAC/IEEE. IEEE, 2017: 1-6.
[20] Chen K H, Chen J J, Kriebel F, et al. Task mapping for redundant
REFERENCES multithreading in multi-cores with reliability and performance
heterogeneity[J]. IEEE Transactions on Computers, 2016, 65(11):
[1] ParsonsD.Multithreading[M]//Foundational Java. Springer, London, 3441-3455.
2012: 357-386.
[21] Kai Zhang. Implementation and Discussion of TCP/IP Network
[2] Ge Y, Shi W, Zhang Q, et al. System and Method for an Communication Protocol[J]. Network and information, 2008,
Asynchronous Processor with Multiple Threading: U.S. Patent 22(9):24-25.
Application 14/476,535[P]. 2015-3-12.
[22] Fuxin Liu. Research and Implementation of Universal Processing
[3] Denis A. pioman: a pthread-based Multithreaded Communication Platform for Internet of Things Data Based on Cloud Computing[D].
Engine[C]//Parallel, Distributed and Network-Based Processing Huazhong Normal University, 2015.
2168
Authorized licensed use limited to: Inst. Federal de Educ. Ciência e Tecn. do RS. Downloaded on April 08,2024 at 22:07:57 UTC from IEEE Xplore. Restrictions apply.
[23] Xiaochen Yu. Design and Implementation of Network Chat System [25] Yuan Li. Stress load testing for web applications[J]. Science and
Based on Swoole[D]. Beijing University of Posts and Technology Information (Science Teaching and Research). 2008, 21:
Telecommunications, 2017. 057
[24] Kewen Li, Bo Su. Research on Rapid Stress Test Based on Web
Application[J]. Microcomputer application, 2007.28(9):991-994
2169
Authorized licensed use limited to: Inst. Federal de Educ. Ciência e Tecn. do RS. Downloaded on April 08,2024 at 22:07:57 UTC from IEEE Xplore. Restrictions apply.