From aa26c845d7759f8640df2126a46f260f79fde9e0 Mon Sep 17 00:00:00 2001 From: dblock Date: Wed, 16 Sep 2020 10:16:42 -0400 Subject: [PATCH] Benchmark retaining setup objects when remounting, fixed in #2102. --- Gemfile | 1 + benchmark/remounting.rb | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 benchmark/remounting.rb diff --git a/Gemfile b/Gemfile index 48a26eb179..7e70dbcfee 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,7 @@ end group :development do gem 'appraisal' gem 'benchmark-ips' + gem 'benchmark-memory' gem 'guard' gem 'guard-rspec' gem 'guard-rubocop' diff --git a/benchmark/remounting.rb b/benchmark/remounting.rb new file mode 100644 index 0000000000..a585c1d2e8 --- /dev/null +++ b/benchmark/remounting.rb @@ -0,0 +1,47 @@ +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) +require 'grape' +require 'benchmark/memory' + +class VotingApi < Grape::API + logger Logger.new(STDOUT) + + helpers do + def logger + VotingApi.logger + end + end + + namespace 'votes' do + get do + logger + end + end +end + +class PostApi < Grape::API + mount VotingApi +end + +class CommentAPI < Grape::API + mount VotingApi +end + +env = Rack::MockRequest.env_for('/votes', method: 'GET') + +Benchmark.memory do |api| + calls = 1000 + + api.report('using Array') do + VotingApi.instance_variable_set(:@setup, []) + calls.times { PostApi.call(env) } + puts " setup size: #{VotingApi.instance_variable_get(:@setup).size}" + end + + api.report('using Set') do + VotingApi.instance_variable_set(:@setup, Set.new) + calls.times { PostApi.call(env) } + puts " setup size: #{VotingApi.instance_variable_get(:@setup).size}" + end + + api.compare! +end