Skip to content
Examples to show different features in Swoole 4.
Branch: master
Clone or download
Latest commit 1a3467a Mar 14, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
.gitignore
LICENSE.txt first commit Feb 14, 2019
README.md
composer.json
docker-compose.yml

README.md

Swoole by Examples

License: MIT

Examples to show different features in Swoole 4. This was created for my talk "Swoole 4: Introducing a New Coroutine Design Pattern in PHP" at SunshinePHP 2019 and ConFoo 2019. Slides can be found here.

Table of Contents:

Setup the PHP Environments

First, please run the following command to have the Docker container running in the background:

docker-compose up -d

This Docker container has PHP environments (both PHP-FPM and Swoole) built in. Now please run following command to log into the container so that we can start running some sample scripts in it:

docker exec -ti $(docker ps | grep app | awk '{print $1}') bash

There are four ports mapped in the Docker container:

Run Sample Scripts

Before running any sample scripts below, please run following command to log into the running Docker container first, if you haven't yet done that:

docker exec -ti $(docker ps | grep app | awk '{print $1}') bash

Sleep

# The script takes about 3 seconds to finish, and prints out "12".
time php php/sleep.php

# The script takes about 2 seconds to finish, and prints out "21".
time php swoole/sleep1.php

# The script takes about 2 seconds to finish, and prints out "123456".
time php swoole/sleep2.php

Coroutines in a For Loop

# The script takes about 1 second to finish, with 2,000 coroutines created.
# Without coroutine enabled, the script takes about 2,000 seconds to finish.
time php swoole/for.php
#
# Similar to the previous one but has debug message printed out during execution.
php swoole/for_debug.php

Nested Coroutines

time php swoole/nested1.php

# The script takes about 5 seconds to finish.
time php swoole/nested2.php

# The script takes about 5 seconds to finish, and prints out "127983465".
time php swoole/nested3.php

# The script takes about 5 seconds to finish, and prints out "123456789".
time php swoole/nested4.php

Channels

php swoole/channel.php

# Similar to the previous one but has debug messages printed out showing # of active coroutines.
php swoole/channel_debug.php

Defer

# The script takes about 1 second to finish, and prints out "123456".
time php swoole/defer.php

Enable Coroutines at Runtime

# The script takes about 2 seconds to finish.
# Without coroutines enabled at runtime, it takes about 3 seconds to finish.
time php swoole/enable-coroutine.php

Server Socket

Create a simple HTTP server socket with one of following two commands:

# Create a simple HTTP server socket on port 9999 with PHP.
php php/socket.php
# Create a simple HTTP server socket on port 9999 with Swoole.
php swoole/socket.php

Now use the ab command in the host to benchmark the server socket created:

ab -n 5000 -c 5 http://127.0.0.1:9999/ # Fire 5 concurrent HTTP requests.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.