Skip to content

Commit

Permalink
Add benchmarks, get the baseline
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Sep 14, 2017
1 parent 4a63d0e commit 9d62c71
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 0 additions & 2 deletions Gemfile
@@ -1,7 +1,5 @@
source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'coveralls', require: false

# Specify your gem's dependencies in batch-loader.gemspec
Expand Down
1 change: 1 addition & 0 deletions batch-loader.gemspec
Expand Up @@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "graphql", "~> 1.6"
spec.add_development_dependency "pry-byebug", "~> 3.4"
spec.add_development_dependency "benchmark-ips", "~> 2.7"
end
41 changes: 41 additions & 0 deletions spec/benchmarks/loading.rb
@@ -0,0 +1,41 @@
# frozen_string_literal: true

# Usage: ruby spec/benchmarks/loading.rb

require 'benchmark/ips'
require "batch_loader"

require_relative "../fixtures/models"

User.save(id: 1)

batch_loader_with_cache = BatchLoader.for(1).batch do |ids, loader|
User.where(id: ids).each { |user| loader.call(user.id, user) }
end

batch_loader_without_cache = BatchLoader.for(1).batch(cache: false) do |ids, loader|
User.where(id: ids).each { |user| loader.call(user.id, user) }
end

Benchmark.ips do |x|
x.config(time: 5, warmup: 0)
x.report("without BatchLoader") { User.where(id: [1]).first.id }
x.report("with BatchLoader with cache") { batch_loader_with_cache.id }
x.report("with BatchLoader without cache") { batch_loader_without_cache.id }
x.compare!
end

# Warming up --------------------------------------
# without BatchLoader 1.000 i/100ms
# with BatchLoader with cache 1.000 i/100ms
# with BatchLoader without cache 1.000 i/100ms

# Calculating -------------------------------------
# without BatchLoader 965.088k (±15.1%) i/s - 3.399M in 3.864552s
# with BatchLoader with cache 991.089k (± 8.0%) i/s - 3.272M
# with BatchLoader without cache 75.239k (±18.1%) i/s - 345.350k in 4.883875s

# Comparison:
# with BatchLoader with cache: 991089.2 i/s
# without BatchLoader: 965088.1 i/s - same-ish: difference falls within error
# with BatchLoader without cache: 75238.7 i/s - 13.17x slower

0 comments on commit 9d62c71

Please sign in to comment.