ReactPHP: practical intro

30
SymfonyCafe Kyiv 2015 ReactPHP: Practical Intro Dennis Povshedny, freelance

Transcript of ReactPHP: practical intro

SymfonyCafe Kyiv 2015

ReactPHP: Practical Intro

Dennis Povshedny, freelance

SymfonyCafe Kyiv 2015

Agenda

- Intro about myself and the task- ReactPHP- Using of ReactPHP- Questions / Demo

2

SymfonyCafe Kyiv 2015

About myself

Main tools: C++, PHP, LinuxPreferred area: troubleshooting, optimizations,proof-of-concept, tech leadingOpensource: Drupal

3

SymfonyCafe Kyiv 2015

Task: check of sitemap.xml

- Rapid check of large sitemap.xml- Check both local and remote sitemap- learn something new

- candidates: PHP, C++, Bash, ReactPHP, Go, Rust, ...

4

SymfonyCafe Kyiv 2015

- Reactor pattern- Event Driven- Non-blocking I/O- Pure PHP, small

5

What is ReactPHP?

SymfonyCafe Kyiv 2015

Projects based on ReactPHP

- Redis Client/Server- Asterisk Management- Munin- Async SQL client- … several more

6

SymfonyCafe Kyiv 20157

Reactor

SymfonyCafe Kyiv 2015

Core sample: HTTP server

8

SymfonyCafe Kyiv 2015

Layers

- Event loop as a backbone- Low-level components: DNS resolver, timers/ticks, sockets, network client/server)- Middle level: HTTP Server/Clients- High: Promise, Stream

9

SymfonyCafe Kyiv 2015

Layers in practice

10

$loop = React\EventLoop\Factory::create();$socket = new React\Socket\Server($loop);$http = new React\Http\Server($socket);

Higher

SymfonyCafe Kyiv 2015

Timers, including periodic

11

SymfonyCafe Kyiv 2015

Event Loop Engines

12

if (function_exists('event_base_new')) { return new LibEventLoop(); } else if (class_exists('libev\EventLoop')) { return new LibEvLoop; } else if (class_exists('EventBase')) { return new ExtEventLoop; }

Installing additional libraries is optional

SymfonyCafe Kyiv 2015

More: Async I/O

System: LibEIO packageBinding: eio PECLGitHub: react/filesystem

13

SymfonyCafe Kyiv 2015

More: Promises

Proxy objects to store values for yet unknown results

Also known as Deferred, Yield and Futures in other languages

14

SymfonyCafe Kyiv 2015

- https://github.com/reactphp/react/wiki/Users :about 40 projects;- Can be evaluated as alternative when you need to create multithreaded app- Community- Unit tests

15

Project state

SymfonyCafe Kyiv 2015

Disadvantages

- version is 0.4.x- complicated to debug an application

16

SymfonyCafe Kyiv 2015

Part II - using of ReactPHP

17

SymfonyCafe Kyiv 2015

The Octopus project

An app which uses two featuresof the ReactPHP:- Async Http Requests;- Timers.

Purpose: education + simple tool needed to check sitemap validity

18

SymfonyCafe Kyiv 2015

Parallel execution of GET requests

19

❖ ~70 ms per page vs ~300 ms for sequential check;❖ low memory and CPU usage

SymfonyCafe Kyiv 2015

...can’t say the same for server :)

20

SymfonyCafe Kyiv 201521

Extract from settings file

SymfonyCafe Kyiv 2015

Application main file

22

SymfonyCafe Kyiv 2015

- a few full days of development, initially with idea of rapid check of sitemap.xml before Google;

- about 12k of PHP code;- 1 accepted PR to ReactPHP;- works faster than script with sequential checking (6x);- application core was developed relatively easy;

Octopus facts and numbers

23

SymfonyCafe Kyiv 2015

Handlers: named function

Regular named function can be used as well

24

SymfonyCafe Kyiv 2015

OpenSource, Experimental

- code is more Proof-of-concept stype;- published on GitHub, welcome to fork and experiment

25

SymfonyCafe Kyiv 2015

Possible evolution & new tools

- Octopus: parse for links, check them as well- Other project: compare/warmup DNS servers

26

SymfonyCafe Kyiv 2015

Sum it up

Job is done.Used two components, there is many more:- Streams;- Child processes;- Futures (Promises)

27

SymfonyCafe Kyiv 2015

LinksSee those...http://reactphp.orghttp://blog.wyrihaximus.net/2015/01/reactphp-introductionhttps://github.com/dpovshed/octopushttp://marcjschmidt.de/blog/2014/02/08/php-high-performance.html

...and also thosehttps://github.com/reactphp/react/wiki/FAQhttps://philsturgeon.uk/blog/2013/11/benchmarking-codswallop-nodejs-v-php/https://github.com/reactphp/react/wiki/Event-Loop-Implementationshttps://github.com/php-pm/php-pm

28

SymfonyCafe Kyiv 2015

Questions / Demo

29

SymfonyCafe Kyiv 2015

Thank you!

Dennis Povshednyhttp://dennisp.tel

https://ua.linkedin.com/in/1dennis1Email: [email protected]

30