From 2d0ceffb0977c6e0ac49c989de66788b1ae6edf0 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 2 Mar 2018 19:17:39 -0800 Subject: [PATCH] Updated benchmark scripts [skip ci] --- Rakefile | 2 +- benchmark/Gemfile | 2 +- benchmark/benchmark.rb | 98 -------------------------------------------------------------------------------------------------- benchmark/index.rb | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ benchmark/search.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+), 100 deletions(-) delete mode 100644 benchmark/benchmark.rb create mode 100644 benchmark/index.rb create mode 100644 benchmark/search.rb diff --git a/Rakefile b/Rakefile index 8c215e6..487be13 100644 --- a/Rakefile +++ b/Rakefile @@ -16,5 +16,5 @@ Rake::TestTask.new do |t| end task :benchmark do - require_relative "benchmark/benchmark" + require_relative "benchmark/index" end diff --git a/benchmark/Gemfile b/benchmark/Gemfile index ef1f7bd..6404855 100644 --- a/benchmark/Gemfile +++ b/benchmark/Gemfile @@ -3,7 +3,7 @@ source "https://rubygems.org" # Specify your gem's dependencies in searchkick.gemspec gemspec path: "../" -# gem "sqlite3" +gem "sqlite3" gem "pg" gem "activerecord", "~> 5.2.0.rc1" gem "activerecord-import" diff --git a/benchmark/benchmark.rb b/benchmark/benchmark.rb deleted file mode 100644 index 7adf9d9..0000000 --- a/benchmark/benchmark.rb +++ /dev/null @@ -1,98 +0,0 @@ -require "bundler/setup" -Bundler.require(:default) -require "active_record" -require "benchmark" -require "active_support/notifications" - -ActiveSupport::Notifications.subscribe "request.searchkick" do |*args| - event = ActiveSupport::Notifications::Event.new(*args) - p event.duration.round -end - -ActiveJob::Base.queue_adapter = :sidekiq - -Searchkick.redis = Redis.new - -ActiveRecord::Base.default_timezone = :utc -ActiveRecord::Base.time_zone_aware_attributes = true -# ActiveRecord::Base.establish_connection adapter: "sqlite3", database: "/tmp/searchkick" -ActiveRecord::Base.establish_connection "postgresql://localhost/searchkick_demo_development" -# ActiveRecord::Base.logger = Logger.new(STDOUT) - -ActiveJob::Base.logger = nil - -class Product < ActiveRecord::Base - searchkick batch_size: 1000 - - def search_data - { - name: name, - color: color, - store_id: store_id - } - end -end - -# total_docs = 100000 - -# ActiveRecord::Migration.create_table :products, force: :cascade do |t| -# t.string :name -# t.string :color -# t.integer :store_id -# end - -# Product.import ["name", "color", "store_id"], total_docs.times.map { |i| ["Product #{i}", ["red", "blue"].sample, rand(10)] } - -# puts "Imported" - -result = nil -report = nil -stats = nil - -Product.searchkick_index.delete rescue nil - -GC.start -GC.disable -start_mem = GetProcessMem.new.mb - -time = - Benchmark.realtime do - # result = RubyProf.profile do - # report = MemoryProfiler.report do - # stats = AllocationStats.trace do - reindex = Product.reindex #(async: true) - # p reindex - # end - - # 60.times do |i| - # if reindex.is_a?(Hash) - # docs = Searchkick::Index.new(reindex[:index_name]).total_docs - # else - # docs = Product.searchkick_index.total_docs - # end - # puts "#{i}: #{docs}" - # if docs == total_docs - # break - # end - # p Searchkick.reindex_status(reindex[:index_name]) if reindex.is_a?(Hash) - # sleep(1) - # # Product.searchkick_index.refresh - # end - end - -puts -puts "Memory: #{(GetProcessMem.new.mb - start_mem).round(1)}mb" -puts "Time: #{time.round(1)}s" - -if result - printer = RubyProf::GraphPrinter.new(result) - printer.print(STDOUT, min_percent: 5) -end - -if report - puts report.pretty_print -end - -if stats - puts result.allocations(alias_paths: true).group_by(:sourcefile, :class).to_text -end diff --git a/benchmark/index.rb b/benchmark/index.rb new file mode 100644 index 0000000..7adf9d9 --- /dev/null +++ b/benchmark/index.rb @@ -0,0 +1,98 @@ +require "bundler/setup" +Bundler.require(:default) +require "active_record" +require "benchmark" +require "active_support/notifications" + +ActiveSupport::Notifications.subscribe "request.searchkick" do |*args| + event = ActiveSupport::Notifications::Event.new(*args) + p event.duration.round +end + +ActiveJob::Base.queue_adapter = :sidekiq + +Searchkick.redis = Redis.new + +ActiveRecord::Base.default_timezone = :utc +ActiveRecord::Base.time_zone_aware_attributes = true +# ActiveRecord::Base.establish_connection adapter: "sqlite3", database: "/tmp/searchkick" +ActiveRecord::Base.establish_connection "postgresql://localhost/searchkick_demo_development" +# ActiveRecord::Base.logger = Logger.new(STDOUT) + +ActiveJob::Base.logger = nil + +class Product < ActiveRecord::Base + searchkick batch_size: 1000 + + def search_data + { + name: name, + color: color, + store_id: store_id + } + end +end + +# total_docs = 100000 + +# ActiveRecord::Migration.create_table :products, force: :cascade do |t| +# t.string :name +# t.string :color +# t.integer :store_id +# end + +# Product.import ["name", "color", "store_id"], total_docs.times.map { |i| ["Product #{i}", ["red", "blue"].sample, rand(10)] } + +# puts "Imported" + +result = nil +report = nil +stats = nil + +Product.searchkick_index.delete rescue nil + +GC.start +GC.disable +start_mem = GetProcessMem.new.mb + +time = + Benchmark.realtime do + # result = RubyProf.profile do + # report = MemoryProfiler.report do + # stats = AllocationStats.trace do + reindex = Product.reindex #(async: true) + # p reindex + # end + + # 60.times do |i| + # if reindex.is_a?(Hash) + # docs = Searchkick::Index.new(reindex[:index_name]).total_docs + # else + # docs = Product.searchkick_index.total_docs + # end + # puts "#{i}: #{docs}" + # if docs == total_docs + # break + # end + # p Searchkick.reindex_status(reindex[:index_name]) if reindex.is_a?(Hash) + # sleep(1) + # # Product.searchkick_index.refresh + # end + end + +puts +puts "Memory: #{(GetProcessMem.new.mb - start_mem).round(1)}mb" +puts "Time: #{time.round(1)}s" + +if result + printer = RubyProf::GraphPrinter.new(result) + printer.print(STDOUT, min_percent: 5) +end + +if report + puts report.pretty_print +end + +if stats + puts result.allocations(alias_paths: true).group_by(:sourcefile, :class).to_text +end diff --git a/benchmark/search.rb b/benchmark/search.rb new file mode 100644 index 0000000..5f31048 --- /dev/null +++ b/benchmark/search.rb @@ -0,0 +1,42 @@ +require "bundler/setup" +Bundler.require(:default) +require "active_record" +require "benchmark" + +ActiveRecord::Base.default_timezone = :utc +ActiveRecord::Base.time_zone_aware_attributes = true +ActiveRecord::Base.establish_connection adapter: "sqlite3", database: "/tmp/searchkick" + +class Product < ActiveRecord::Base + searchkick batch_size: 1000 + + def search_data + { + name: name, + color: color, + store_id: store_id + } + end +end + +# total_docs = 1000000 + +# ActiveRecord::Migration.create_table :products, force: :cascade do |t| +# t.string :name +# t.string :color +# t.integer :store_id +# end + +# Product.import ["name", "color", "store_id"], total_docs.times.map { |i| ["Product #{i}", ["red", "blue"].sample, rand(10)] } + +# puts "Imported" + +# Product.reindex + +time = + Benchmark.realtime do + Product.search("product", fields: [:name], where: {color: "red", store_id: 5}, load: false) + end + +puts +puts "Time: #{time.round(1)}s" -- libgit2 0.21.0