Skip to content

Instantly share code, notes, and snippets.

@rlue
Last active July 19, 2018 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlue/fdac87f654374799eac546baf296fd1d to your computer and use it in GitHub Desktop.
Save rlue/fdac87f654374799eac546baf296fd1d to your computer and use it in GitHub Desktop.
Benchmark for modifications to Associations::Assign — calculating changes BEFORE assign_attributes (worst-case scenario)
require 'benchmark'
ASSOCIATIONS = {
'2' => (1..10).to_a,
'4' => (1..10).to_a,
'6' => (1..10).to_a,
'8' => (1..10).to_a,
'10' => (1..10).to_a,
'12' => (1..10).to_a,
'14' => (1..10).to_a,
'16' => (1..10).to_a,
'18' => (1..10).to_a,
'20' => (1..10).to_a
}.freeze
ORIG_ATTRS = {
'1' => (3..8).to_a,
'2' => (3..8).to_a,
'3' => (3..8).to_a,
'4' => (3..8).to_a,
'5' => (3..8).to_a,
'6' => (3..8).to_a,
'7' => (3..8).to_a,
'8' => (3..8).to_a,
'9' => (3..8).to_a,
'10' => (3..8).to_a,
'11' => (3..8).to_a,
'12' => (3..8).to_a,
'13' => (3..8).to_a,
'14' => (3..8).to_a,
'15' => (3..8).to_a,
'16' => (3..8).to_a,
'17' => (3..8).to_a,
'18' => (3..8).to_a,
'19' => (3..8).to_a,
'20' => (3..8).to_a
}.freeze
def changes
unfiltered_changes.reject(&method(:no_diff?))
end
def unfiltered_changes
attrs = ASSOCIATIONS.keys
before = attrs.map { |key| ORIG_ATTRS[key] }
after = ASSOCIATIONS.values
attrs.zip(before.zip(after)).to_h
end
def no_diff?(_, values)
values.map!(&:sort) if values.all? { |val| val.respond_to?(:sort) }
# values.map!(&:presence) # [nil, []] -> [nil, nil]
values.uniq.one?
end
Benchmark.bm(15) do |x|
x.report('1000 records:') { 1000.times { changes } }
x.report('10000 records:') { 10_000.times { changes } }
x.report('100000 records:') { 100_000.times { changes } }
end
__END__
Here are the results on my machine:
user system total real
1000 records: 0.103869 0.000481 0.104350 ( 0.104405)
10000 records: 0.986262 0.000637 0.986899 ( 0.987065)
100000 records: 6.851424 0.000023 6.851447 ( 6.851865)
In the context of a several-minute LDAP sync, I think this is a small performance hit for a worst-case scenario.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment