2. Proprietary and Confidential to Ruby Development Co., LTD page2
• 自己紹介
• 氏名:柴田 有一郎
• 所属:株式会社 Ruby開発
https://www.ruby-dev.jp
• 職業:Web系のプロマネ的なことをやってます
3. Proprietary and Confidential to Ruby Development Co., LTD page3
• ここから本題
• 元ネタあります。
• http://www.gladbills.com/help/development/benchmarking.md
• 2015/10/02にGitLabにマージされています
• Benchmark Specとは
= RSpec + benchmark-ips
4. Proprietary and Confidential to Ruby Development Co., LTD page4
• よくあるパフォーマンス上の問題??
• 修正でパフォーマンス上問題のある処理を入れてしまった!!
• CIでの自動化テストでは検出できない
• QAで検出できても、複数の修正が入ってると特定に時間がかか
る
5. Proprietary and Confidential to Ruby Development Co., LTD page5
• 解決するために、こんなアプローチはどう?
• CIで自動テストのように、自動ベンチマークを行う
• パフォーマンス要求を設定できる
6. Proprietary and Confidential to Ruby Development Co., LTD page6
• 具体的にどんな感じ?
• describe でbenchmark を指定
• 「benchmark」が指定されたときだけ、benchmark
spec機能を有効にします。
describe User, benchmark: true do
end
7. Proprietary and Confidential to Ruby Development Co., LTD page7
• ユーザログイン処理のbenchmark Specを例に説明
describe User, benchmark: true do
describe ‘.by_login’ do
before do
create(:user, email: ‘alice@ruby-dev.jp’)
end
ユーザを作成
例は超適当です(_ _)
8. Proprietary and Confidential to Ruby Development Co., LTD page8
• パフォーマンス要求を設定
describe User, benchmark: true do
describe ‘.by_login’ do
before do
create(:user, email: ‘alice@ruby-dev.jp’)
end
let(:iterations) { 1000 }
1秒間に1000回以上で成功
(平均して1回のログイン処理
は1ミリ秒以内)
9. Proprietary and Confidential to Ruby Development Co., LTD page9
• subjectへ登録
let(:iterations) { 1000 }
describe ‘using a Email address’ do
subject{ -> { User.by_login(‘alice@ruby-dev.jp’)} }
テスト対象として、.by_loginを
Procオブジェクトとして登録
10. Proprietary and Confidential to Ruby Development Co., LTD page10
• エクスペクテーション(expectation)は?
describe ‘using a Email address’ do
subject{ -> { User.by_login(‘alice@ruby-dev.jp’)} }
it { is_expected.to iterate_per_second(iterations) }
end
end
end
カスタムマッチャ「iterate_per_second」
を作成し、使用します。
※いきなり出てきて申し訳ないです。
以降説明あります。
11. Proprietary and Confidential to Ruby Development Co., LTD page11
• カスタムマッチャー「iterate_per_second」
• 元ネタから説明用にかなり単純化しています。
module BenchmarkMatchers
extend RSpec::Matchers::DSL
matcher :iterate_per_second do |min_iterations|
match do |block|
report = Benchmark.ips(quiet: true) do |bench|
bench.report(&block)
end
内部で、Benchmark.ipsを実施
rails_helper.rbでinclude
config.include BenchmarkMatchers,
benchmark: true
12. Proprietary and Confidential to Ruby Development Co., LTD page12
• カスタムマッチャー「iterate_per_second」
matcher :iterate_per_second do |min_iterations|
match do |block|
report = Benchmark.ips(quiet: true) do |bench|
bench.report(&block)
end
expect(report.entries[0].ips).to be >= min_iterations
end
end
要求されるイテレーション以上な
ら、テスト成功。
13. Proprietary and Confidential to Ruby Development Co., LTD page13
まとめ
今後、benchmark specを導入して、性能を
落とす処理の混入をCIで検出できるか試し
て行きたいと考えています。