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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
130
プロダクト開発から業務改善コンサルまで。事業全体へ「染み出す」ことで広がるエンジニアの可能性
ham0215
0
240
AI時代に「チーム開発」を見直す ~個人アサインへのシフトと、AI駆動開発の実践例~
ham0215
0
71
機能開発を止めないために!運用と開発のバランスを可視化するために使っている指標をご紹介
ham0215
0
68
未来のAI駆動開発をイメージしながらAI開発基盤を整備する
ham0215
1
220
AIと過ごす1日〜全業務フローにAIを組み込む実践ガイド〜
ham0215
0
170
生成AIによる生産性向上〜テック企業やファインディの活用事例〜
ham0215
1
160
生成AI導入の効果を最大化する データ活用戦略
ham0215
0
680
データ駆動経営の道しるべ:プロダクト開発指標の戦略的活用法
ham0215
2
620
Other Decks in Programming
See All in Programming
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
200
気持ちぃ〜角丸 Squircle
ryu1013
0
310
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
290
残高管理から台帳サービスへの進化
artoy
1
200
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
220
Workers Cache を知る
syumai
0
360
phperだってパンチカードでコーディングしたい
o0h
PRO
0
330
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
2
880
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.2k
ActionController meets Protocol Buffers
sorah
0
520
AI時代のコードレビューは人に向けるな、仕組みに向けろ
texmeijin
5
3.7k
The Rails Doctrine Decade
koic
2
590
Featured
See All Featured
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
4
4.4k
Everyday Curiosity
cassininazir
0
350
Designing Experiences People Love
moore
143
24k
Code Reviewing Like a Champion
maltzj
529
40k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
New Earth Scene 8
popppiees
4
2.7k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
260
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
280
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
730
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
530
Marketing to machines
jonoalderson
1
5.9k
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
まとめ テストコードはパフォーマンスを気にせず実装されていることが多く、ちょっと 改善するだけで効果絶大です!!(と個人的には思っています) 運用改善の時間が取れる場合はぜひ見直してみてはいかがでしょうか!?