spec/benchmarks.rb in rack-reducer-1.1.0 vs spec/benchmarks.rb in rack-reducer-1.1.1

- old
+ new

@@ -23,30 +23,40 @@ @artists = @artists.where(genre: genre.to_s) end if (name = params['name']) @artists = @artists.grep(:name, "%#{name}%", case_insensitive: true) end + if env['DEFAULTS'] + @artists = @artists.order(:name) + end Rack::Response.new(@artists).finish end TestReducer = Rack::Reducer.create( DB[:artists], ->(genre:) { where(genre: genre.to_s) }, + ->(name:) { grep(:name, "%#{name}%", case_insensitive: true) } +) +TestReducerWithDefaults = Rack::Reducer.create( + DB[:artists], + ->(genre:) { where(genre: genre.to_s) }, ->(name:) { grep(:name, "%#{name}%", case_insensitive: true) }, + ->(sort: 'name') { order(sort.to_sym) } ) reducer_app = lambda do |env| params = Rack::Request.new(env).params - @artists = TestReducer.apply(params) + @artists = env['DEFAULTS'] ? TestReducerWithDefaults.apply(params) : TestReducer.apply(params) Rack::Response.new(@artists).finish end Benchmark.ips do |bm| env = { 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/', - 'rack.input' => StringIO.new('') + 'rack.input' => StringIO.new(''), + 'DEFAULTS' => false } query = { 'QUERY_STRING' => 'name=blake&genre=electronic', } @@ -63,9 +73,17 @@ conditional_app.call env.dup end bm.report('Reducer (empty)') do reducer_app.call env.dup + end + + bm.report('Conditionals (defaults)') do + conditional_app.call env.merge('DEFAULTS' => true) + end + + bm.report('Reducer (defaults)') do + reducer_app.call env.merge('DEFAULTS' => true) end bm.compare! end