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
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
460
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
510
ジョジョの世界をXRで再現する
mofurune
0
180
ActionController meets Protocol Buffers
sorah
0
670
自サークルのIdPからログアウトできるようになるまで
taiseiue
1
140
phperだってパンチカードでコーディングしたい
o0h
PRO
0
380
Simple Storage Service(S3) is not simple
iwatsukayura
0
310
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
300
우리의 Nitro Modules는 당신의 Turbo Modules보다 아름답다
l2hyunwoo
0
110
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
190
🇪🇸 React Alicante 2026 (Workshop)
ythecombinator
PRO
0
100
Optunaを作ったときにやりたかったこと、その後 (2026/10/16, Optuna Meetup #3)
iwiwi
2
1.2k
Featured
See All Featured
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
530
Chasing Engaging Ingredients in Design
codingconduct
0
370
Navigating Team Friction
lara
192
16k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
Large-scale JavaScript Application Architecture
addyosmani
516
110k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
930
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Building Applications with DynamoDB
mza
96
7.2k
Docker and Python
trallard
47
4.2k
Odyssey Design
rkendrick25
PRO
2
880
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.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]