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
650
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
残高管理から台帳サービスへの進化
artoy
1
210
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
300
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
3
1k
気持ちぃ〜角丸 Squircle
ryu1013
0
330
sbt 2.0: takeoff
eed3si9n
0
170
PyConTW 2026 - AI Coding 不只是工具升級, 區域醫院醫療資訊系統協作開發與資訊團隊轉型實踐
cclai999
0
140
Seven New Principles of Software Engineering
yegor256
0
210
phperだってパンチカードでコーディングしたい
o0h
PRO
0
380
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
2
890
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
180
Zero-Tap Sign-In対応、結局何をすればいいの?
mitohato14
0
300
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
120
Featured
See All Featured
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
290
My Coaching Mixtape
mlcsv
0
360
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Crafting Experiences
bethany
1
390
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
460
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.3k
Producing Creativity
orderedlist
PRO
348
41k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
Raft: Consensus for Rubyists
vanstee
142
7.8k
Why Our Code Smells
bkeepers
PRO
340
58k
Everyday Curiosity
cassininazir
0
360
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]