benchmark.rb
2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
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 = 10000
# 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
p GetProcessMem.new.mb - start_mem
puts time.round(1)
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