Skip to content

Commit

Permalink
Demonstrate the memory leak in ruby-grape#2102 with remounting.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Sep 17, 2020
1 parent d40d697 commit 80e40cc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ end
group :development do
gem 'appraisal'
gem 'benchmark-ips'
gem 'benchmark-memory'
gem 'guard'
gem 'guard-rspec'
gem 'guard-rubocop'
Expand Down
47 changes: 47 additions & 0 deletions benchmark/remounting.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 80e40cc

Please sign in to comment.