10

ユニットテスト用のRailsプロジェクトでRSpecを使用しています。RSpecでいくつかのパフォーマンステストを設定したいのですが、「通常の」機能とフィクスチャを中断しないように設定します。

理想的には、デフォルトで実行されないように、特定の方法でパフォーマンス仕様にタグを付けることができます。次に、これらの仕様を明示的に実行するように指定すると、異なるフィクスチャのセットがロードされます(はるかに大きく、より「本番環境に似た」データセットを使用してパフォーマンステストを実行するのは理にかなっています)。

これは可能ですか?あるべきようです。

誰かがこのようなものを設定しましたか?どのようにそれについて行きましたか?

4

3 に答える 3

20

私は以下を介して私が探していたものをなんとか手に入れることができました:

# Exclude :performance tagged specs by default
config.filter_run_excluding :performance => true

# When we're running a performance test load the test fixures:
config.before(:all, :performance => true) do
  # load performance fixtures
  require 'active_record/fixtures'
  ActiveRecord::Fixtures.reset_cache
  ActiveRecord::Fixtures.create_fixtures('spec/perf_fixtures', File.basename("products.yml", '.*'))
  ActiveRecord::Fixtures.create_fixtures('spec/perf_fixtures', File.basename("ingredients.yml", '.*'))
end

# define an rspec helper for takes_less_than
require 'benchmark'
RSpec::Matchers.define :take_less_than do |n|
  chain :seconds do; end
  match do |block|
    @elapsed = Benchmark.realtime do
      block.call
    end
    @elapsed <= n
  end
end

# example of a performance test
describe Api::ProductsController, "API Products controller", :performance do
  it "should fetch all the products reasonably quickly" do
    expect do
      get :index, :format => :json
    end.to take_less_than(60).seconds
  end
end

しかし、私は、これがパフォーマンステストの最良のアイデアではないというMarnenの指摘に同意する傾向があります。

于 2012-02-13T02:56:55.257 に答える
8

RSpecでパフォーマンステストを作成するためのrspec-benchmarkRubygemを作成しました。テスト速度、リソース使用量、およびスケーラビリティーに多くの期待が寄せられています。

たとえば、コードの速度をテストするには、次のようにします。

expect { ... }.to perform_under(60).ms

または、別の実装と比較するには:

expect { ... }.to perform_faster_than { ... }.at_least(5).times

または、計算の複雑さをテストするには:

expect { ... }.to perform_logarithmic.in_range(8, 100_000)

または、割り当てられるオブジェクトの数を確認するには、次のようにします。

expect {
  _a = [Object.new]
  _b = {Object.new => 'foo'}
}.to perform_allocation({Array => 1, Object => 2}).objects
于 2019-04-21T18:53:53.810 に答える
0

パフォーマンステストを行いたい場合は、New Relicなどの本番データのスナップショットを実行してみませんか?そのために実際に異なるスペックは必要ないと思います。

于 2011-12-14T17:59:11.643 に答える