Created
February 5, 2015 15:38
-
-
Save amarshall/babd7b51087c4d7577a7 to your computer and use it in GitHub Desktop.
CanCanCan performance scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler' | |
require 'csv' | |
Bundler.setup | |
require 'cancan' | |
NUM_SUBJECTS = 1..100 | |
# returns µs | |
def time &block | |
before = Time.now.to_r | |
yield | |
difference = Time.now.to_r - before | |
(difference * 10**6).to_i | |
end | |
results = NUM_SUBJECTS.map do |num_subjects| | |
subjects = (1..num_subjects).map { Class.new } | |
rules = %i[read update manage destroy].product(subjects) | |
ability = Class.new do | |
include CanCan::Ability | |
define_method(:initialize) do | |
rules.each do |action, subject| | |
can action, subject | |
end | |
end | |
end.new | |
timings = [] | |
3.times do | |
rules.each do |action, subject| | |
timing = time { ability.can?(action, subject) } | |
timings << timing | |
end | |
end | |
timings.sort! | |
mean = timings.reduce(:+) / timings.size.to_f | |
ninty = timings[timings.size * 0.9] | |
[num_subjects, mean, ninty] | |
end | |
id = `git rev-parse head`.strip | |
CSV.open("rule-lookup-timing-#{id}.csv", 'w') do |csv| | |
csv << %w[num-subjects mean 90%] | |
results.each do |result| | |
csv << result | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler' | |
Bundler.setup | |
require 'cancan' | |
ITERATIONS = 8 | |
NUM_SUBJECTS = 100 | |
# returns nanoseconds | |
def time &block | |
before = Time.now.to_r | |
yield | |
difference = Time.now.to_r - before | |
(difference * 10**6).to_i | |
end | |
subjects = (1..NUM_SUBJECTS).map { Class.new } | |
rules = %i[read update manage destroy].product(subjects) | |
ability = Class.new do | |
include CanCan::Ability | |
define_method(:initialize) do | |
rules.each do |action, subject| | |
can action, subject | |
end | |
end | |
end.new | |
timings = [] | |
ITERATIONS.times do | |
rules.each do |action, subject| | |
timing = time { ability.can?(action, subject) } | |
timings << timing | |
end | |
end | |
id = `git rev-parse head`.strip | |
File.write("rule-lookup-timing--#{id}.csv", timings.join("\n")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment