|
require 'bundler' |
|
Bundler.require |
|
|
|
require 'cancancan' |
|
require 'allocation_tracer' |
|
require 'benchmark/ips' |
|
require 'objspace' |
|
require 'pp' |
|
|
|
def memprof(name) |
|
GC.disable |
|
before = `ps -o rss -p #{Process.pid}`.split("\n").last.to_i |
|
trace = ObjectSpace::AllocationTracer.trace do |
|
10_000.times do |
|
yield |
|
end |
|
end |
|
after = `ps -o rss -p #{Process.pid}`.split("\n").last.to_i |
|
|
|
puts "[#{name}] Mem diff: #{(after - before).to_f/1024.0} MB" |
|
#puts "[#{name}] Trace:" |
|
#puts trace |
|
#puts |
|
GC.enable |
|
GC.start |
|
end |
|
|
|
class Ability |
|
include CanCan::Ability |
|
|
|
def initialize |
|
can [:read, :write], [:foo, :bar] |
|
end |
|
end |
|
|
|
case ARGV.shift |
|
when "--ips" |
|
Benchmark.ips do |b| |
|
b.report("Array#flatten") { CanCan::Rule.new(nil, [:read, :write], [:foo, :bar], nil, nil, false) } |
|
b.report("Kernel#Array") { CanCan::Rule.new(nil, [:read, :write], [:foo, :bar], nil, nil, true) } |
|
|
|
b.compare! |
|
end |
|
when "--match" |
|
ability = Ability.new |
|
rule = ability.send(:rules).first |
|
rule.expanded_actions = ability.send(:expand_actions, rule.actions) |
|
|
|
memprof("as-is") do |
|
rule = ability.send(:rules).first |
|
rule.expanded_actions = ability.send(:expand_actions, rule.actions) |
|
rule.relevant?(:read, :bar) |
|
end |
|
else |
|
memprof("Array#flatten") { CanCan::Rule.new(nil, [:read, :write], [:foo, :bar], nil, nil, false) } |
|
memprof("Kernel#Array") { CanCan::Rule.new(nil, [:read, :write], [:foo, :bar], nil, nil, true) } |
|
end |