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で書いたAPIをGoに書き換えてみた 〜パフォーマンス改善の可能性を探る実験レポート〜
Search
Wakana Koizumi
April 10, 2025
Programming
0
330
PHPで書いたAPIをGoに書き換えてみた 〜パフォーマンス改善の可能性を探る実験レポート〜
Wakana Koizumi
April 10, 2025
Tweet
Share
Other Decks in Programming
See All in Programming
Hypervel - A Coroutine Framework for Laravel Artisans
albertcht
1
120
技術同人誌をMCP Serverにしてみた
74th
1
640
Team operations that are not burdened by SRE
kazatohiei
1
310
Blazing Fast UI Development with Compose Hot Reload (droidcon New York 2025)
zsmb
1
290
High-Level Programming Languages in AI Era -Human Thought and Mind-
hayat01sh1da
PRO
0
770
なんとなくわかった気になるブロックテーマ入門/contents.nagoya 2025 6.28
chiilog
1
270
システム成長を止めない!本番無停止テーブル移行の全貌
sakawe_ee
1
200
なぜ「共通化」を考え、失敗を繰り返すのか
rinchoku
1
650
なぜ適用するか、移行して理解するClean Architecture 〜構造を超えて設計を継承する〜 / Why Apply, Migrate and Understand Clean Architecture - Inherit Design Beyond Structure
seike460
PRO
3
760
“いい感じ“な定量評価を求めて - Four Keysとアウトカムの間の探求 -
nealle
1
10k
明示と暗黙 ー PHPとGoの インターフェイスの違いを知る
shimabox
2
510
データの民主化を支える、透明性のあるデータ利活用への挑戦 2025-06-25 Database Engineering Meetup#7
y_ken
0
360
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1031
460k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Fireside Chat
paigeccino
37
3.5k
Optimising Largest Contentful Paint
csswizardry
37
3.3k
Art, The Web, and Tiny UX
lynnandtonic
299
21k
The World Runs on Bad Software
bkeepers
PRO
69
11k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
How to Ace a Technical Interview
jacobian
278
23k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.4k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Transcript
Copyright © Canly, Inc. All rights reserved. 2025.04.10 PHPで書いたAPIをGoに書き換えてみた 〜パフォーマンス改善の可能性を探る実験レポート〜
株式会社カンリー 小泉若菜
Copyright © Canly, Inc. All rights reserved. 初心者です、優しくしてください 2
Copyright © Canly, Inc. All rights reserved. 目次 3 自己紹介
課題 結論 前提共有 実験結果 P. 04 P. 06 P. 09 P. 12 P. 16 まとめ P. 29
Copyright © Canly, Inc. All rights reserved. 自己紹介 4
Copyright © Canly, Inc. All rights reserved. 自己紹介 5 茨城県水戸市
2023年1月(在籍: 2年3ヶ月) HRエンジニア部 カンリー福利厚生(フクリー) PHP, Laravel, Ruby, Ruby on Rails, React, TypeScript, Next.js, React Native お茶 なかなかいい物件が見つからない 5 小泉若菜 出身 入社年月 所属チーム スキル 趣味 悩み
Copyright © Canly, Inc. All rights reserved. 課題 6
Copyright © Canly, Inc. All rights reserved. 課題 7 店舗ピン取得のAPIのレスポンスをもっと早くしたい!
Copyright © Canly, Inc. All rights reserved. 課題 8 店舗ピン取得のAPIのレスポンスをもっと早くしたい!
• Input: ユーザーの緯度経度、ジャンル、自社割限定か などを受け取る • Output: 現在そのユーザーに表示できるクーポンを 持つ店舗のみ、返却する
Copyright © Canly, Inc. All rights reserved. 結論 9
Copyright © Canly, Inc. All rights reserved. 結論 10 PHPからGOへ
→ 約6倍 速くなった 🎉
Copyright © Canly, Inc. All rights reserved. 結論 11 GOで書いたコードを一部並列化したら
? → 直列のコードの 2倍超遅くなった 😢
Copyright © Canly, Inc. All rights reserved. 前提共有 12
Copyright © Canly, Inc. All rights reserved. 前提共有 13 •
PCのスペック ◦ MacBook Pro ◦ チップ: Apple M2 Pro ◦ メモリ: 16GB • 言語、ライブラリのバージョン ◦ PHP: 8.2.28 ◦ Laravel: 10.48.4 ◦ GO: 1.21.13 ◦ Echo: 4.11.4 • PHPとGOの比較はdockerコンテナ上で実行 実行環境
Copyright © Canly, Inc. All rights reserved. 前提共有 14 PHP
& Go • レスポンス速度 ◦ 総100回のリクエストを10リクエスト並列で実行した時の平均値 Go • メモリ使用率 ◦ 現在のメモリ使用率 ◦ メモリの割当履歴 • CPU使用率 ◦ CPUプロファイル 「パフォーマンス」の計測の指標
Copyright © Canly, Inc. All rights reserved. パフォーマンス計測 15 →
Apache Bench 使ったツール } • レスポンス速度 ◦ 総100回のリクエストを10リクエスト並列で実行した時の平均値 • メモリ使用率 ◦ 現在のメモリ使用率 ◦ メモリの割当履歴 • CPU使用率 ◦ CPUプロファイル → pprof
Copyright © Canly, Inc. All rights reserved. 実験結果 16
Copyright © Canly, Inc. All rights reserved. 結果: レスポンス速度 17
平均リクエスト処理時間 • 総100回のリクエストを10リクエスト並列で実行した時の平均値 ◦ $ ab -n 100 -c 10 http://localhost:9090/api/stores/pins • 結果: ◦ PHP: 96.757 ms ◦ GO: 16.230 ms
Copyright © Canly, Inc. All rights reserved. 結果: レスポンス速度 18
平均リクエスト処理時間 • 総100回のリクエストを10リクエスト並列で実行した時の平均値 ◦ 実行コマンド: ab -n 100 -c 10 http://localhost:9090/api/stores/pins • 結果: ◦ PHP: 96.757 ms ◦ GO: 16.230 ms ←もっと早くならないか?
Copyright © Canly, Inc. All rights reserved. 結果: メモリ使用量 19
heap & allocs分析 • heap: 現在の使用量 (メモリリーク、現在のメモリ圧迫分析) • allocs: 過去の割り当て履歴 (無駄なメモリ割り当て、頻度の高い割り当て分析) • 結果: ◦ Header.Clone → HTTPヘッダー処理によるもの。 ◦ 対象のAPIのはメモリ使用の13%程度 → APIの処理にはそこまでメモリを使用していない !
Copyright © Canly, Inc. All rights reserved. 結果: CPU使用量 20
CPU分析 • profile: seconds=10で10秒間プロファイリング • 結果: ◦ 多くは以下に使われている ▪ ネットワーク待機 ▪ syscall ▪ Echo内部 → APIの処理ではは CPUはほとんど消費していない
Copyright © Canly, Inc. All rights reserved. 21 goroutine使ってみたい ...
Copyright © Canly, Inc. All rights reserved. 結果: レスポンス速度 22
goroutineを使ってクーポンの出し分け処理を並列化 1. 変数A: ユーザーの現在地から近い順に並べられたクーポンリスト 2. 変数B: ユーザーが使えるクーポンの配列を入れる配列 3. 出し分け処理 a. クーポン1枚ごとに出し分けロジックをかける b. 通過したクーポンのみが入った配列B を作成 4. 配列Bを配列Aの順番に並び替え ←並列処理
Copyright © Canly, Inc. All rights reserved. 結果: レスポンス速度 23
平均リクエスト処理時間 • PHP: 96.757 • GO: 16.230 • GO(goroutine使用時): 30.258
Copyright © Canly, Inc. All rights reserved. 結果: レスポンス速度 24
1万件のループを並列化してみる 直列処理 並列処理(goroutine + WaitGroup + Mutex) Serial Even Count: 5000 Serial Time: 16.208µs (=0.016208ms) Parallel Even Count: 5000 Parallel Time: 20.755875ms
Copyright © Canly, Inc. All rights reserved. 結果: レスポンス速度 25
1万件のループを並列化してみる 直列処理 並列処理(goroutine + WaitGroup) Serial Even Count: 5000 Serial Time: 16.208µs(0.016208ms) Parallel Even Count: 5000 Parallel Time: 54.338958ms
Copyright © Canly, Inc. All rights reserved. 結果: レスポンス速度 26
1万件のループを並列化してみる 直列処理 並列処理(goroutineのみ) Serial Even Count: 5000 Serial Time: 16.208µs(0.016208ms) Parallel Even Count: 5000 Parallel Time: 15.900042ms
Copyright © Canly, Inc. All rights reserved. 一体いつGo routineが有効にはたらくの ..?
27
Copyright © Canly, Inc. All rights reserved. 結果: レスポンス速度 28
sleepを入れた SQLを実行する 直列処理 並列処理 Serial Even Count: 5000 Serial Time: 5.532775919(5532.775919 ms) Parallel Even Count: 5000 Parallel Time: 209.862501ms
Copyright © Canly, Inc. All rights reserved. まとめ 29
Copyright © Canly, Inc. All rights reserved. まとめ 30 •
PHPからGOへ移行することで、速度は約 6倍になった ◦ → 言語的には性能が高いことがわかった • 処理を並列化したら処理速度があがるわけではない ◦ → 並列処理の中の処理が軽い場合、 Goroutineのオーバーヘッドのほう ◦ → 適切な場面で使うことが不可欠
Copyright © Canly, Inc. All rights reserved. 31 ご清聴ありがとうございました
[email protected]