$30 off During Our Annual Pro Sale. View Details »

PHP7.1 is fast(?)

uzulla
January 01, 2017

PHP7.1 is fast(?)

2017/?/? (forgot)
at somewhere.

this is kind of (joke|comedy).

uzulla

January 01, 2017
Tweet

More Decks by uzulla

Other Decks in Programming

Transcript

  1. PHP7.1 is fast(?)

    View Slide

  2. name:
    ͍ͩ͠͡ΎΜ͍ͪ
    (Ishida)
    nick:
    uzulla
    (͏ͣΒ)
    fun:
    PHP and !

    View Slide

  3. conclusion
    7.1 is fast !
    (but any evidence here)

    View Slide

  4. need benchmark !!

    View Slide

  5. by the way...
    php can't to be standalone httpd. right?
    (php -s is for only development use)
    (btw, hhvm can be !)
    If will benchmark with apache/nginx, that is unfair(???)

    View Slide

  6. use "ReactPHP"!
    (use libevent)
    standalone httpd(and other), that written by PHP!

    View Slide

  7. Test Enviroment
    4 VPS (Vultr, $5/mo)
    4 1Core
    4 1GB mem
    4 ubuntu 16.10
    4 PHP7.1.2

    View Slide

  8. Write git clone code.
    4 httpd, written by php
    4 https://github.com/uzulla/
    ReactPHP_PostSupportSample
    4 include monolog, twig(template engine.)

    View Slide

  9. require_once 'vendor/autoload.php';
    $loader = new Twig_Loader_Filesystem(__DIR__.'/templates');
    $twig = new Twig_Environment($loader, [
    'cache' => __DIR__.'/twig_cache',
    ]);
    $access_log = new \Monolog\Logger('access');
    $access_log->pushHandler(new \Monolog\Handler\StreamHandler(__DIR__.'/access.log', \Monolog\Logger::INFO));
    // app
    $app = function (\Uzulla\React\Http\Request $req,\React\Http\Response $res) use ($twig, $access_log) {
    $path = $req->getPath();
    $method = $req->getMethod();
    $access_log->addInfo("{$method}\t{$path}");
    $params = [
    'params'=>$req->getParams(),
    'query'=>$req->getQuery()
    ];
    if($method === 'GET' && preg_match('|\A/\z|u', $path)){
    $res->writeHead(200, ['Content-Type' => 'text/html']);
    $res->end($twig->render('index.twig',$params));
    }elseif($method === 'POST' && preg_match('|\A/\z|u', $path)){
    $res->writeHead(200, ['Content-Type' => 'text/html']);
    $res->end($twig->render('index.twig',$params));
    }else{
    $res->writeHead(404, ['Content-Type' => 'text/html']);
    $res->end($twig->render('notfound.twig'));
    }
    };
    // build reactor
    $loop = React\EventLoop\Factory::create();
    $socket = new React\Socket\Server($loop);
    $http = new \Uzulla\React\Http\Server($socket);
    $http->on('request', $app);
    $socket->listen(8080);
    echo "running...\n";
    $loop->run();

    View Slide

  10. $ git clone THAT
    $ composer install
    $ php boot.php
    listen...

    View Slide

  11. ab(apache bench)
    4 ab -n 10000 -c 10 http://127.0.0.1:8080/

    View Slide

  12. Requests per second:
    3477.39 #/sec

    View Slide

  13. okay, try more concurrent connection.

    View Slide

  14. 5000 concurrent
    4 ab -n 10000 -c 5000 http://127.0.0.1:8080/

    View Slide

  15. fail
    4 socket: Too many open files (24)

    View Slide

  16. ok, ab's limits.
    try another tool.
    apt install wrk
    4 https://github.com/wg/wrk

    View Slide

  17. wrk
    4 wrk -c 5000 -d 5 http://127.0.0.1:8080/

    View Slide

  18. Requests/sec
    98.16

    View Slide

  19. tooooo slow,
    hmmm, concurrent overkill?
    try 1000 concurrent (same as ab's test).

    View Slide

  20. 4 wrk -c 1000 -d 5 http://127.0.0.1:8080/
    4 Requests/sec: 83.17

    View Slide

  21. !

    View Slide

  22. okey, wrk is dosen't work.
    try with httperf !
    4 https://github.com/httperf/httperf

    View Slide

  23. httperf
    4 httperf --port=8080 --server=127.0.0.1 --
    rate=2800 --num-conns=10000 --hog

    View Slide

  24. Request rate:
    2799.6 req/s

    View Slide

  25. seems good.
    but, is this correct?
    I need try more tools !

    View Slide

  26. siege
    4 siege -c 100 http://127.0.0.1:8080/
    4 Transaction rate: 393.35 trans/sec

    View Slide

  27. Untrustworthy...
    (Of course, these bench mark tools are more
    trustworthy than weird httpd.)

    View Slide

  28. all results (?)
    ab: 3477.3 #/sec
    wrk: 98.1 req/sec
    httperf: 2799.6 req/s
    siege: 393.3 trans/sec

    View Slide

  29. me:
    I'm screwed
    lestrrat ( a.k.a daisuke maki):
    Why don't you write a bechmarker yourself ?
    me:
    what?

    View Slide

  30. me:
    OK, I'll write tools!
    (at YESTERDAY 11:36 pm)

    View Slide

  31. I wrote "benchmark.php" !! (ta daaa!)

    View Slide

  32. $c = 10000 ;
    $target = "http://127.0.0.1:8080" ;
    $start_usec = microtime(true);
    for( $i=0; $c>$i; $i++ ){
    $res = file_get_contents($target);
    }
    $used_time = (microtime(true) - $start_usec) ;
    echo ceil( 1/( $used_time/$c ) );
    echo "req/sec";

    View Slide

  33. haha... yes, just kidding.
    let's play.

    View Slide

  34. (unreliable) result
    4 3,731 req/sec

    View Slide

  35. I can't believe this result.
    (faster than ab ;lol)
    it's not bad.
    but ...

    View Slide

  36. I thought, this is slow benchmark tool. !
    I want more faster". but how?
    I need concurrent.!
    but, this is php.
    (curl_multi ? yes...but... )
    OK! WE NEED FORK! !

    View Slide

  37. $max_worker = 10;
    $c = 10000;
    $c_ = $c/$max_worker;
    $target="http://127.0.0.1:8080";
    $start_usec = microtime(true);
    $pids=[];
    for($x=0;$max_worker>$x;$x++){
    $pid = pcntl_fork();
    if ($pid == -1) { die("fork failed");}
    else if ($pid) { $pids[]=$pid; }
    else {
    for($i=0;$c_>$i;$i++){ $res = file_get_contents($target); } exit;
    }
    }
    foreach($pids as $pid){ pcntl_waitpid($pid, $status); }
    $used_time = (microtime(true) - $start_usec) ;
    echo ceil( 1/( $used_time/$c ) ), "req/sec";

    View Slide

  38. 2881req/sec
    (no fork ver 3,731 req/sec)

    View Slide

  39. no no, more slow !!! !

    View Slide

  40. !
    need more core!!!

    View Slide

  41. create 4core 8GB mem server !

    View Slide

  42. benchmark.php
    4147req/sec
    fork_benchmark.php
    3995req/sec

    View Slide

  43. NO! why fork ver is more slow!? !

    View Slide

  44. my httpd is BAD, that is why!!! !
    I use nginx to benchmark my benchmark!! !

    View Slide

  45. result(?) with nginx
    4 benchmark.php
    4 6817req/sec
    4 fork_benchmark.php
    4 13730req/sec

    View Slide

  46. OK!
    4 fork ver faster than not fork ver!!

    View Slide

  47. ( what is ok? )

    View Slide

  48. meanwhile in apache bench...
    4 ab -c 10 -n 10000 http://nginx/
    4 Requests per second: 15273.21

    View Slide

  49. compare own tool and ab.
    4 fork_benchmark.php:
    4 13,730 req/sec
    4 ab:
    4 15,273.21 #/sec

    View Slide

  50. okey!(?)

    View Slide

  51. conclusion
    my bench (written by PHP) 0.9 times
    faster than AB !!
    ( DISCLAIMER, THIS RESULT IS REAL RESULT. BUT JUST
    PLAYING.)

    View Slide

  52. OK !!

    View Slide

  53. "what is ok?"
    (now 2:12 am)

    View Slide

  54. Other benchmarks.
    4 php7.1.2 with my_httpd + fork_bench.php(c:10)
    4 3858req/sec
    4 php5.6.30 with my_httpd + fork_bench.php(c:10)
    4 2425req/sec
    4 hhvm3.18.1 with my_httpd + fork_bench.php(c:10)
    4 2377req/sec
    4 (but, hhvm w/o libevent)

    View Slide

  55. conclusion
    PHP7.1 is fast
    (1.6x faster than 5.6 (at this test))

    View Slide

  56. (REAL) conclusion
    USE RELIABLE TOOLS
    USE NGINX
    DON'T USE PHP FOR WRITE A
    BENCHMARK TOOL

    View Slide

  57. but fun.
    PHP is waiting New Challengers .....
    FIN

    View Slide

  58. //NEXT
    use pthread;

    View Slide