Skip to content

Commit 2cbe2c0

Browse files
authored
Merge pull request #1 from dblock/leak-2102
Benchmark retaining setup objects when remounting, fixed in ruby-grape#2102.
2 parents 609eb31 + aa26c84 commit 2cbe2c0

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ end
1717
group :development do
1818
gem 'appraisal'
1919
gem 'benchmark-ips'
20+
gem 'benchmark-memory'
2021
gem 'guard'
2122
gem 'guard-rspec'
2223
gem 'guard-rubocop'

benchmark/remounting.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2+
require 'grape'
3+
require 'benchmark/memory'
4+
5+
class VotingApi < Grape::API
6+
logger Logger.new(STDOUT)
7+
8+
helpers do
9+
def logger
10+
VotingApi.logger
11+
end
12+
end
13+
14+
namespace 'votes' do
15+
get do
16+
logger
17+
end
18+
end
19+
end
20+
21+
class PostApi < Grape::API
22+
mount VotingApi
23+
end
24+
25+
class CommentAPI < Grape::API
26+
mount VotingApi
27+
end
28+
29+
env = Rack::MockRequest.env_for('/votes', method: 'GET')
30+
31+
Benchmark.memory do |api|
32+
calls = 1000
33+
34+
api.report('using Array') do
35+
VotingApi.instance_variable_set(:@setup, [])
36+
calls.times { PostApi.call(env) }
37+
puts " setup size: #{VotingApi.instance_variable_get(:@setup).size}"
38+
end
39+
40+
api.report('using Set') do
41+
VotingApi.instance_variable_set(:@setup, Set.new)
42+
calls.times { PostApi.call(env) }
43+
puts " setup size: #{VotingApi.instance_variable_get(:@setup).size}"
44+
end
45+
46+
api.compare!
47+
end

0 commit comments

Comments
 (0)