Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ハチャメチャに叩く

uzulla
July 13, 2018

 ハチャメチャに叩く

【非公式】PHPカンファレンス 2018 関西 前夜祭
https://dbstudychugoku.connpass.com/event/92337/

uzulla

July 13, 2018
Tweet

More Decks by uzulla

Other Decks in Programming

Transcript

  1. <?php // Ұൠతͳ௚ྻऔಘίʔυ const MAX_REQ = 10; // نఆॲཧ਺ $req_num

    = 0; while (1) { echo "."; // نఆॲཧ਺Λ௒͑ͨͷͰऴྃ if ($req_num >= MAX_REQ) { break; } // औಘ $req_num++; $_ = file_get_contents("http://example.jp"); }
  2. <?php // coroutineͰ͔͍ͯΈΔ go(function () { $now_worker_num = 0; $req_num

    = 0; while (1) { echo "."; co::sleep(0.001); // εέδϡʔϥͷͨΊʹ // نఆॲཧूΛ௒͑ͨͷͰऴྃ if ($req_num >= MAX_REQ) { break; } // ىಈதͷίϧʔνϯ͕نఆ਺ʹୡ͍ͯ͠ͳ͚Ε͹௥Ճىಈɺ // ʢنఆϫʔΧʔ਺ͳΒىಈͤͣɺϧʔϓઌ಄΁ʣ if ($now_worker_num >= MAX_WORKER_NUM) { continue; }
  3. // while (1) { // ... // go()಺ͷΫϩʔδϟ͕ίϧʔνϯͱͯ͠εέδϡʔϥʹొ࿥ go(function ()

    use (&$now_worker_num, &$req_num) { $req_num++; $now_worker_num++; $http = new \Swoole\Coroutine\Http\Client( "http://example.jp", 80); $http->get('/'); $_ = $http->body; $now_worker_num--; }); // ্ه׬ྃΛ଴ͨͣʹൈ͚Δ } });
  4. go(function () { $http = new Co\Http\Client("http://example.jp", 80); $http->get('/api.json'); $data

    = json_decode($http->body); $id = $data[0]['id']; $http->get('/api.json?id='.$id); $data = json_decode($http->body); $child_id = $data[0]['child_id']; $http->get('/api.json?child_id='.$child_id); $data = json_decode($http->body); echo $data['title']; //-> });
  5. $client = new Swoole\Http2\Client("example.jp", 80); $client->get("/", function ($o) use($client) {

    $data = json_decode($o->body); $id = $data[0]['id']; $client->get("/api.json?id=".$id, function ($o) use($client) { $data = json_decode($o->body); $child_id = $data[0]['child_id']; $client->get("/api.json?child_id=".$id, function ($o) use($client) { $data = json_decode($o->body); echo $data['title']; //---------> } } });
  6. see also • https:/ /wiki.swoole.com/wiki/page/p-coroutinehttpclient.html • https:/ /github.com/swoole/swoole-src/tree/master/examples/ coroutine •

    https:/ /wiki.swoole.com/wiki/page/752.html • https:/ /wiki.swoole.com/wiki/page/784.html • https:/ /github.com/eaglewu/swoole-ide-helper • https:/ /www.swoole.com/ • https:/ /www.swoole.co.uk/docs/modules/swoole-async-http-client