Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
PHPで書いたAPIをGoに書き換えてみた 〜パフォーマンス改善の可能性を探る実験レポート〜
Search
Wakana Koizumi
April 10, 2025
Programming
640
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PHPで書いたAPIをGoに書き換えてみた 〜パフォーマンス改善の可能性を探る実験レポート〜
Wakana Koizumi
April 10, 2025
Other Decks in Programming
See All in Programming
AWSに止められる覚悟してますか?
morizo_1984
2
490
テストを司るデーモンに会いに行く 〜隔離した仮想マシンでテストを通すまで〜
h1d3mun3
1
600
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
390
Java 27新機能 / Java 27 new features
kishida
2
190
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
330
AI Agent時代のリアーキテクチャ戦略と実践
hokaccha
9
5.2k
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
230
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
140
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
270
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
330
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.7k
モバイル交通系ICへのチャージ実例から考える、クロスプラットフォーム開発におけるiOS実機テスト設計とCI運用
yusuga
1
570
Featured
See All Featured
Bash Introduction
62gerente
615
220k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
250
Paper Plane (Part 1)
katiecoart
PRO
2
11k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Typedesign – Prime Four
hannesfritz
42
3.2k
Documentation Writing (for coders)
carmenintech
77
5.6k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
31k
Evolving SEO for Evolving Search Engines
ryanjones
0
300
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
Thoughts on Productivity
jonyablonski
76
5.4k
It's Worth the Effort
3n
188
29k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
510
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]