Skip to content

Commit

Permalink
Merge pull request #13 from pocke/Add_benchmark_script
Browse files Browse the repository at this point in the history
Add benchmark script
  • Loading branch information
pocke committed Mar 16, 2024
2 parents 4345755 + 886b82c commit 830d020
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ gem "rubocop", "~> 1.21", require: false
gem 'steep', require: false

gem 'sqlite3'

gem 'benchmark-ips'
25 changes: 25 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,28 @@ task :steep do
end

task default: %i[rubocop steep spec]

namespace :benchmark do
task :to_sql do
puts "# Benchmarking #to_sql mehtod"
puts
puts "## Wihtout AR::Originator"
sh 'ruby', 'benchmark/to_sql.rb'

puts
puts "## With AR::Originator"
sh 'ruby', '-r', 'activerecord/originator', 'benchmark/to_sql.rb'
end

task :select_query do
puts "# Benchmarking SELECT Query"
puts
puts "## Wihtout AR::Originator"
sh 'ruby', 'benchmark/select_query.rb'

puts
puts "## With AR::Originator"
sh 'ruby', '-r', 'activerecord/originator', 'benchmark/select_query.rb'
end
end
task benchmark: %i[benchmark:to_sql benchmark:select_query]
2 changes: 1 addition & 1 deletion activerecord-originator.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject do |f|
(File.expand_path(f) == __FILE__) ||
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile benchmark/])
end
end
spec.bindir = "exe"
Expand Down
26 changes: 26 additions & 0 deletions benchmark/select_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'active_record'
require 'benchmark/ips'

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(File::NULL)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title, null: false
t.string :state, null: false
t.integer :comments_count, default: 0
t.integer :likes_count, default: 0
end
end

class Post < ActiveRecord::Base
end

100.times do |i|
Post.create!(title: "title#{i}", state: i.odd? ? 'draft' : 'published', comments_count: i, likes_count: i)
end

Benchmark.ips do |x|
x.report("without where") { Post.all.to_sql }
x.report('with single condition') { Post.where(title: 'title1').to_sql }
x.report('with multiple condition') { Post.where(state: 'draft', comments_count: ..42, likes_count: ..43).to_sql }
end
22 changes: 22 additions & 0 deletions benchmark/to_sql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'active_record'
require 'benchmark/ips'

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(File::NULL)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title, null: false
t.string :state, null: false
t.integer :comments_count, default: 0
t.integer :likes_count, default: 0
end
end

class Post < ActiveRecord::Base
end

Benchmark.ips do |x|
x.report("without where") { Post.all.to_sql }
x.report('with single condition') { Post.where(title: 'hello').to_sql }
x.report('with multiple condition') { Post.where(title: 'hello', state: 'draft', comments_count: 42, likes_count: 43).to_sql }
end

0 comments on commit 830d020

Please sign in to comment.