Git Product home page Git Product logo

swoole-examples's Introduction

Memory scopes

See which data is available where. What code runs in which process

run memory-scopes.php
curl http://localhost:8080/
docker stop swoole

SWOOLE_PROCESS

Everything before SwooleHttpServer->start() is available in every process Everything after SwooleHttpServer->start() is available within given process

onStart is on master process, can use global onShutdown is on master process, case use global and onStart onManagerStart is on manager process, can use global onWorkerStart is worker n process, can use global onRequest is worker n process, can use global and onWorkerStart

> docker logs swoole
 1 [swoole-master   ] run
 1 [swoole-master   ] run, onStart
 6 [swoole-manager  ] run, onManagerStart
 9 [swoole-worker-0 ] run, onWorkerStart
12 [swoole-worker-3 ] run, onWorkerStart
11 [swoole-worker-2 ] run, onWorkerStart
10 [swoole-worker-1 ] run, onWorkerStart
10 [swoole-worker-1 ] run, onWorkerStart, onRequest
10 [swoole-worker-1 ] run, onWorkerStart, onRequest (REUSED)
12 [swoole-worker-3 ] run, onWorkerStart, onRequest
12 [swoole-worker-3 ] run, onWorkerStart, onRequest (REUSED)
 1 [swoole-master   ] run, onStart, onShutdown
> docker exec -it swoole ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.1  2.1 510604 44104 pts/0    Ssl+ 19:42   0:00 swoole-master
root         6  0.0  0.6 362248 12376 pts/0    S+   19:42   0:00 swoole-manager
root         9  0.0  0.8 358636 16372 pts/0    S+   19:42   0:00 swoole-worker-0
root        10  0.0  0.8 358636 16372 pts/0    S+   19:42   0:00 swoole-worker-1
root        11  0.0  0.8 358636 16372 pts/0    S+   19:42   0:00 swoole-worker-2
root        12  0.0  0.8 358636 16372 pts/0    S+   19:42   0:00 swoole-worker-3

Note that the master has an l in the STAT, which means it is multi threaded

SWOOLE_BASE

Differences;

  • There is not manager process
  • onStart scope is now also globally available
> docker logs swoole
 1 [swoole-master   ] run
 1 [swoole-master   ] run, onStart
 6 [swoole-worker-0 ] run, onStart, onWorkerStart
 7 [swoole-worker-1 ] run, onStart, onWorkerStart
 8 [swoole-worker-2 ] run, onStart, onWorkerStart
 9 [swoole-worker-3 ] run, onStart, onWorkerStart
 7 [swoole-worker-1 ] run, onStart, onWorkerStart, onRequest
 7 [swoole-worker-1 ] run, onStart, onWorkerStart, onRequest (REUSED)
 8 [swoole-worker-2 ] run, onStart, onWorkerStart, onRequest
 8 [swoole-worker-2 ] run, onStart, onWorkerStart, onRequest (REUSED)
 1 [swoole-master   ] run, onStart, onShutdown
> docker exec -it swoole ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.2  2.0 362180 41168 pts/0    Ss+  20:00   0:00 swoole-master
root         6  0.0  0.7 362792 14280 pts/0    S+   20:00   0:00 swoole-worker-0
root         7  0.0  0.7 362792 14280 pts/0    S+   20:00   0:00 swoole-worker-1
root         8  0.0  0.7 362792 14280 pts/0    S+   20:00   0:00 swoole-worker-2
root         9  0.0  0.7 362792 14280 pts/0    S+   20:00   0:00 swoole-worker-3

Note that the master is not multi threaded.

Async and Pooling

run async-pooling.php
ab -n 20 -c 4 http://localhost:8080/
docker stop swoole

Having a server with 2 workers, each having a database pool of 2
Means being able to process 4 requests concurrently!

Each database call takes exactly 1 second (sleep).

n = number of request
c = concurrent requests
s = seconds to complete

This means ab -n 20 -c 4 can complete in about 5 seconds. (20n / 4c = 5s)
This means ab -n 20 -c 10 still takes 5 seconds, database pool being the bottleneck.
Requests will take longer though, as it will accept all 10 at the same time

Without Runtime::enableCoroutine() it will take about 10 seconds, as it will block on the 1s sleep.
20 request block 20 seconds split over 2 workers = 10 seconds total time

swoole-examples's People

Contributors

joebengalen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.