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
RSpecの実行時間を1/5にした話
Search
ham
November 13, 2019
Programming
1.3k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
RSpecの実行時間を1/5にした話
ham
November 13, 2019
More Decks by ham
See All by ham
その投資は、資本になっていますか?AI時代の開発投資を、ROIだけで判断しない「開発資本」という考え方
ham0215
0
26
プロダクト開発から業務改善コンサルまで。事業全体へ「染み出す」ことで広がるエンジニアの可能性
ham0215
0
180
AI時代に「チーム開発」を見直す ~個人アサインへのシフトと、AI駆動開発の実践例~
ham0215
0
65
機能開発を止めないために!運用と開発のバランスを可視化するために使っている指標をご紹介
ham0215
0
66
未来のAI駆動開発をイメージしながらAI開発基盤を整備する
ham0215
1
210
AIと過ごす1日〜全業務フローにAIを組み込む実践ガイド〜
ham0215
0
160
生成AIによる生産性向上〜テック企業やファインディの活用事例〜
ham0215
1
160
生成AI導入の効果を最大化する データ活用戦略
ham0215
0
660
データ駆動経営の道しるべ:プロダクト開発指標の戦略的活用法
ham0215
2
570
Other Decks in Programming
See All in Programming
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
250
AI に Inclusive UI を書かせよう — Design Rules Skill で Compose UI を作り直す
theoriatec2024
1
490
iOSDC2026登壇資料.pdf
riofujimon
0
170
AWS Step Functions 大規模並列の壁を越える / jaws-sonic-2026-niigata-step-functions
kasacchiful
PRO
1
480
モバイル交通系ICへのチャージ実例から考える、クロスプラットフォーム開発におけるiOS実機テスト設計とCI運用
yusuga
1
480
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
360
App Intentsのビルドプロセスを支える技術
kntkymt
0
380
cdk deploy JawsSonic #MARATHONしながらAWSリソースをデプロイしてみよう
akihisaikeda
2
140
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
3k
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
130
AIは賢い。でも実行環境は? CLIおじさんがAI時代に伝えたいこと ~ CLIおじさんがAI時代に伝えたいこと ~
curekoshimizu
1
240
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
420
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
670
How to train your dragon (web standard)
notwaldorf
97
6.8k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
The Invisible Side of Design
smashingmag
301
52k
Making the Leap to Tech Lead
cromwellryan
135
10k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Design in an AI World
tapps
1
320
The agentic SEO stack - context over prompts
schlessera
0
940
Transcript
RSpecの実行時間を1/5にした話 2019/11/13 Ebisu.rb #26 ham
自己紹介 name: naoto hamada twitter: @hamchance0215 biography: → → 最近はRoRのBackend
Engineer Javascriptの知識が古すぎるのでUpdate中
アプリ概要 概要 WebアプリケーションのAPI versions Ruby 2.4.1 Rails 5.2.3 Rspec-rails 3.9.0
エンドポイント数 79 ジョブ数 27
改善前 $ bundle exec rspec ……………*………………………………………… ……………*…………*……………………………… …………………………………………**…… Finished in
5 minutes 57 seconds 1366 examples, 0 failures, 5 pending
よくないところを直す
jobを同期処理にする 下記の記述が個別のテストケースごとに書いてあった。 Rails.application.config.active_job.queue_adapter = :test 個別に記載していると冗長だし漏れることもあるので、 config/environments/test.rbに移動 発生した問題 active storageにファイルを上げた時にファイルの解析が非同期で動いてお
り、それも同期処理になってしまいテストが遅くなった。 https://github.com/rails/rails/blob/master/activestorage/app/models/active_storage/blob/analyzable.rb#L37 →テストする必要ないのでmockにした。
外部接続のmockを整理 外部接続のmockがテストケースごとに書いてあった。 個別に記載していると冗長だし漏れることもあるので、 spec/spec_helper.rbに移動 config.before(:each) do allow(xxx).to receive(:yyy).and_return(true) ... end
Pending exampleを動かす(or 消す) pendingしているテストは意味がないので動くように直す。 本当に必要がないテストならば消す。 Pendingは極力使わない!!使ったらとしてもすぐ直す!!!!
database cleanerを外す
database cleanerを外す database cleanerを外した理由 • RSpecがテストごとにRollbackしてくれる • いろんなところに設定が増殖していた • ar_internal_metadataも消しちゃう
• truncateのスピードが遅い 詳細は下記にまとめています https://qiita.com/ham0215/items/7516117df87d2631e31d
profileで解析
遅いテストを探す rspecに--profileをつけると遅いテストをランキング形式で並べてくれます これで遅いテストを探しました $ bundle exec rspec --profile 10
create_list撲滅
create_list撲滅 FactoryBot.create_listは大量データを作る時に便利ですが1件ごとに insertを発行してしまいます FactoryBot.create_list(:user, 100) →100件insertが発行される users = FactoryBot.build_list(:user, 100)
User.import users →bulk insertで登録 詳細は下記にまとめています https://qiita.com/ham0215/items/dad6f5d3d63bd498d999
モデル生成のN+1対応
モデル生成のN+1対応 下記のように記述した時、review生成時にuserを毎回selectしてしまう。 let!(:user) { FactoryBot.create(:user) } # => insert user
let!(:draft_review) { FactoryBot.create(:review, :draft, user_id: user.id) } # => select user + insert review let!(:published_review) { FactoryBot.create(:review, :published, user_id: user.id) } # => select user + insert review 下記のようにモデルを直接渡せばselectされない let!(:draft_review) { FactoryBot.create(:review, :draft, user: user) } # => insert review 詳細は下記にまとめています https://qiita.com/ham0215/items/2943511a4336a77a6aed
sleepをスタブ化
sleepをスタブ化 外部接続のリトライなどで明示的にsleepしている処理があるが、テストでは sleepしなくて良い場合が多い →sleepをmockにする 発生した問題 spec/spec_helper.rbにsleepのmock定義したら、 RSpec::Mocks::OutsideOfExampleErrorが発生するようになった。 DBコネクションのsleepもmockになってしまったようだ https://github.com/rails/rails/blob/6-0-stable/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb#L339 →テストケースごとに個別にmockを定義
改善後 $ bundle exec rspec ……………………………………………………… ……………………………………………………… ………………………………………………..… Finished in
1 minute 45.64 seconds 1366 examples, 0 failures 5 minute 58 seconds ↓ 約1/5に短縮!! 1 minute 45.64 seconds
まとめ テストコードはパフォーマンスを気にせず実装されていることが多く、ちょっと 改善するだけで効果絶大です!!(と個人的には思っています) 運用改善の時間が取れる場合はぜひ見直してみてはいかがでしょうか!?