Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHPでWebサーバーを作って高速化に挑戦する!/php-web-server
Search
Ryo Tomidokoro
April 11, 2022
Technology
1
880
PHPでWebサーバーを作って高速化に挑戦する!/php-web-server
PHPerKaigi 2022 Day2 の LTです。
Ryo Tomidokoro
April 11, 2022
Tweet
Share
More Decks by Ryo Tomidokoro
See All by Ryo Tomidokoro
フロントエンドがTypeScriptなら、バックエンドはPHPでもいいじゃない/php-is-not-bad
hanhan1978
8
12k
どうすると生き残れないのか/how-not-to-survive
hanhan1978
17
13k
100分で本番デプロイ!Laravelで作るWebアプリケーション作成/100min_web_app_cicd
hanhan1978
1
160
PHPerのための計算量入門/Complexity101 for PHPer
hanhan1978
8
2.7k
集中して作業する技術/how_to_work_deeply
hanhan1978
62
48k
PHPでデータベースを作ってみた/create-data-with-php
hanhan1978
11
10k
ADRを一年運用してみた/adr_after_a_year
hanhan1978
8
4k
B+木入門:PHPで理解する データベースインデックスの仕組み/b-plus-tree-101
hanhan1978
5
5.2k
ADRを一年運用してみた/our_story_about_adr
hanhan1978
5
2.3k
Other Decks in Technology
See All in Technology
從開發到架構設計的可觀測性實踐
philipz
0
200
会社紹介資料 / Sansan Company Profile
sansan33
PRO
6
370k
ai bot got sick (abc 2025s version)
kojira
0
150
FASTと向き合うことで見えた、大規模アジャイルの難しさと楽しさ
wooootack
0
430
Bill One 開発エンジニア 紹介資料
sansan33
PRO
4
12k
MCPを利用して自然言語で3Dプリントしてみよう!
hamadakoji
0
1.3k
バクラクのモノレポにおける AI Coding のための環境整備と {Roo,Claude} Code活用事例 / AI Coding in Bakuraku's Monorepo: Environment Setup & Case Studies with {Roo, Claude} Code
upamune
8
4.4k
Flutterアプリを⾃然⾔語で操作する
yukisakai1225
0
210
Roo CodeとClaude Code比較してみた
pharma_x_tech
1
190
Tensix Core アーキテクチャ解説
tenstorrent_japan
0
270
型システムを知りたい人のための型検査器作成入門
mame
13
3.2k
AIエージェント実践集中コース LT
okaru
1
200
Featured
See All Featured
Adopting Sorbet at Scale
ufuk
77
9.4k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.3k
How GitHub (no longer) Works
holman
314
140k
GitHub's CSS Performance
jonrohan
1031
460k
Optimising Largest Contentful Paint
csswizardry
37
3.3k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Testing 201, or: Great Expectations
jmmastey
42
7.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
47
2.8k
Practical Orchestrator
shlominoach
188
11k
Transcript
@hanhan1978 PHPでWebサーバーを作って高速化に挑 戦する! PHPerKaigi 2022 LT
@hanhan1978 • 富所 亮 • 所属 株式会社カオナビ💎 Expert • 職業
Webアプリケーションエンジニア • ブログ https://blog.hanhans.net • Yokohama North AM https://anchor.fm/yokohama-north-am 2
本日のテーマ 3
PHP 8.1 でウェブサーバーを作って 世界一を目指す! 4
ルール説明 5
各言語、実装、ミドルウェアのウェブサーバーは / へのアクセスに対して {status : “OK”} というレスポンスを返す 6
各言語、実装、ミドルウェアのウェブサーバーは / へのアクセスに対して {status : “OK”} というレスポンスを返す 7
ベンチマーカーには wrk を使う。 ベンチマークコマンドは下記 wrk -t3 -c100 -d30s –latency http://127.0.0.1:8080/
8
9 実測
まずは対戦相手から 10
11 Go
Nginx 12 docker run -v default.conf:/etc/nginx/conf.d/default.conf -p 8080:80 --rm nginx
Node.js 13
14
top 1 で簡易に負荷状況を見る 15
top 1 で簡易に負荷状況を見る 16 特定のCPUコアに負荷が偏る
top 1 で簡易に負荷状況を見る 17
top 1 で簡易に負荷状況を見る 18 満遍なく負荷をかけられている こちらが理想的な形
19 PHP実装
Webサーバーとは? ようするに HTTP の仕様にそった 文字列を Socket 通信で返却すればOK 20
まずは、シングルプロセス 21
まずは、シングルプロセス 22 実に単純明快
23 結果
無念の敗退 24
次は、IO多重化 25
次は、IO多重化 26 複数コネクションを同時に扱 えるようになった
27 結果
むしろ遅くなる 28
シンプルすぎる返却値なので socket_select 分遅くなったと推測 29
よし!ノンブロックだ! 30
よし!ノンブロックだ! 31 時代はノンブロッキング
32 結果
ブロッキングとほぼ変わらない 33
この辺で、PHPのコア側にブロックしてい る箇所があるという想像が働く 34
実際の負荷状況 35
実際の負荷状況 36 全てのコアが遊びまくっている
ノンブロッキングの場合は無限ループのコードにな るので、若干1コアの負荷が上がるが、ほぼ負荷を かけられない 37
諦めずにpreforkしてみる 38
諦めずにpreforkしてみる 39 ノンブロッキングサーバーを 複数プロセスで利用
40 結果
ちょっと改善したが... 41
そろそろ勝てる気がしなくなってきたので 手当たりしだいに試した 42
手当たり次第の結果 43 実装 Req/Seq Amp 11933 RoadRunner 19618 Revolt 20297
mod-php 30389
なんとか Node.js をかわした 44
しかし、それでいいのか? 45
mod-php での実装 46
mod-php での実装 47 これでいいのか?
助けて Open Swoole ….. 48
Open Swoole での実装 49
50 結果
勝てば官軍 51
52 優勝だ!
ちょっとだけ、まじめなラップアップ 53
Open Swoole は、PHPコアのブロッキングする関 数を独自実装で置き換えている。 結局のところ、ボトルネックを自前で差し替えるよう な豪腕を発揮しない限りPHP単体では勝てないの かもしれない。 54
この件については、まじめな追加調査をします! 影PHP勉強会でお会いしましょう! 55