Created
August 23, 2017 20:44
-
-
Save mattnwa/22d73c455b99c564bcdec63f87447f75 to your computer and use it in GitHub Desktop.
Benchmarking upsert gem vs. find_or_create + update rails
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 'benchmark/ips' | |
Benchmark.ips do |x| | |
# the warmup phase (default 2) and calculation phase (default 5) | |
x.config(:time => 5, :warmup => 2) | |
Times = 500 | |
times = Times | |
def upsert(id) | |
Score.upsert({scoreable_id: 7864, scoreable_type: 'FacebookPage'}, raw_value: 1234) | |
end | |
def find_or_create(id) | |
Score.find_or_create_by(scoreable_id: 7864, scoreable_type: 'FacebookPage').update(raw_value: 1234) | |
end | |
def iterate | |
i = 0 | |
while i < Times | |
yield | |
i += 1 | |
end | |
end | |
x.report("upsert") do |times| | |
iterate { upsert(7864) } | |
end | |
x.report("find_or_create_by_update") do |times| | |
iterate { find_or_create(7864) } | |
end | |
# Compare the iterations per second of the various reports! | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`0.101 (± 0.0%) i/s - 1.000 in 9.914837s
Comparison:
upsert: 0.2 i/s
find_or_create_by_update: 0.1 i/s - 1.74x slower
=> #<Benchmark::IPS::Report:0x007fce48a720b0
@DaTa=nil,
@entries=
[#<Benchmark::IPS::Report::Entry:0x007fce4fb288b8
@Iterations=1,
@Label="upsert",
@measurement_cycle=1,
@Microseconds=5685253.442993164,
@show_total_time=true,
@stats=#<Benchmark::IPS::Stats::SD:0x007fce4fb289a8 @error=0, @mean=0.17589365364748302>>,
#<Benchmark::IPS::Report::Entry:0x007fce54a89ee8
@Iterations=1,
@Label="find_or_create_by_update",
@measurement_cycle=1,
@Microseconds=9914837.382995605,
@show_total_time=true,
@stats=#<Benchmark::IPS::Stats::SD:0x007fce54a8a000 @error=0, @mean=0.10085894113755665>>]>`