spec/benchmarks.rb in rack-reducer-0.1.2 vs spec/benchmarks.rb in rack-reducer-1.0.0

- old
+ new

@@ -1,56 +1,41 @@ -require 'spec_helper' -require_relative 'fixtures' +require_relative 'spec_helper' require 'sinatra/base' require 'json' require 'benchmark/ips' -class App < Sinatra::Base - get '/conditionals' do - @artists = DB[:artists] - if (genre = params[:genre]) - @artists = @artists.grep(:genre, "%#{genre}%", case_insensitive: true) - end - if (name = params[:name]) - @artists = @artists.grep(:name, "%#{name}%", case_insensitive: true) - end - - @artists.to_json +Conditionals = lambda do |params = {}| + @artists = DB[:artists] + if (genre = params[:genre]) + @artists = @artists.grep(:genre, "%#{genre}%", case_insensitive: true) end + if (name = params[:name]) + @artists = @artists.grep(:name, "%#{name}%", case_insensitive: true) + end - get '/reduction' do - @artists = Rack::Reducer.call(params, dataset: DB[:artists], filters: [ - ->(genre:) { grep(:genre, "%#{genre}%", case_insensitive: true) }, - ->(name:) { grep(:name, "%#{name}%", case_insensitive: true) }, - ]) + @artists.to_json +end - @artists.to_json - end +Reduction = lambda do |params = {}| + @artists = Rack::Reducer.call(params, dataset: DB[:artists], filters: [ + ->(genre:) { grep(:genre, "%#{genre}%", case_insensitive: true) }, + ->(name:) { grep(:name, "%#{name}%", case_insensitive: true) }, + ]) + + @artists.to_json end -describe 'Performance' do - let(:app) { App } +Benchmark.ips(3) do |bm| + bm.report('conditionals, empty params') { Conditionals.call } - it 'compares favorably to spaghetti code when params are empty' do - Benchmark.ips(3) do |bm| - bm.report('conditionals, empty params') do - get '/conditionals' - end - bm.report('reduction, empty params') do - get '/reduction' - end - bm.compare! - end + bm.report('reduction, empty params') { Reduction.call } + + bm.report('conditionals, full params') do + Conditionals.call({ name: 'blake', genre: 'electric' }) end - it 'compares favorably to spaghetti code when params are full' do - Benchmark.ips(3) do |bm| - bm.report('conditionals, full params') do - get '/conditionals?name=blake&genre=electronic' - end - bm.report('reduction, full params') do - get '/reduction?name=blake&genre=electronic' - end - bm.compare! - end + bm.report('reduction, full params') do + Reduction.call({ name: 'blake', genre: 'electric' }) end + + bm.compare! end