1
|
|
|
# using this script to try figure out why Ruby 2 is slower than 1.9 |
2
|
|
|
require 'flamegraph' |
3
|
|
|
|
4
|
|
|
Flamegraph.generate('test.html', fidelity: 2) do |
5
|
|
|
require File.expand_path("../../config/environment", __FILE__) |
6
|
|
|
end |
7
|
|
|
exit |
8
|
|
|
|
9
|
|
|
require 'memory_profiler' |
10
|
|
|
|
11
|
|
|
result = MemoryProfiler.report do |
12
|
|
|
require File.expand_path("../../config/environment", __FILE__) |
13
|
|
|
end |
14
|
|
|
result.pretty_print |
15
|
|
|
|
16
|
|
|
exit |
17
|
|
|
|
18
|
|
|
require 'benchmark' |
19
|
|
|
|
20
|
|
|
def profile_allocations(name) |
21
|
|
|
GC.disable |
22
|
|
|
initial_size = ObjectSpace.count_objects |
23
|
|
|
yield |
24
|
|
|
changes = ObjectSpace.count_objects |
25
|
|
|
changes.each do |k, _| |
26
|
|
|
changes[k] -= initial_size[k] |
27
|
|
|
end |
28
|
|
|
puts "#{name} changes" |
29
|
|
|
changes.sort { |a, b| b[1] <=> a[1] }.each do |a, b| |
30
|
|
|
next if b <= 0 |
31
|
|
|
# 1 extra hash for tracking |
32
|
|
|
puts "#{a} #{a == :T_HASH ? b - 1 : b}" |
33
|
|
|
end |
34
|
|
|
GC.enable |
35
|
|
|
end |
36
|
|
|
|
37
|
|
|
def profile(name, &block) |
38
|
|
|
puts "Profiling all object allocation for #{name}" |
39
|
|
|
GC.start |
40
|
|
|
GC.disable |
41
|
|
|
|
42
|
|
|
items = [] |
43
|
|
|
objs = [] |
44
|
|
|
|
45
|
|
|
ObjectSpace.trace_object_allocations do |
46
|
|
|
block.call |
47
|
|
|
|
48
|
|
|
ObjectSpace.each_object do |o| |
49
|
|
|
objs << o |
50
|
|
|
end |
51
|
|
|
|
52
|
|
|
objs.each do |o| |
53
|
|
|
g = ObjectSpace.allocation_generation(o) |
54
|
|
|
if g |
55
|
|
|
l = ObjectSpace.allocation_sourceline(o) |
56
|
|
|
f = ObjectSpace.allocation_sourcefile(o) |
57
|
|
|
c = ObjectSpace.allocation_class_path(o) |
58
|
|
|
m = ObjectSpace.allocation_method_id(o) |
59
|
|
|
items << "Allocated #{c} in #{m} #{f}:#{l}" |
60
|
|
|
end |
61
|
|
|
end |
62
|
|
|
end |
63
|
|
|
|
64
|
|
|
items.group_by { |x| x }.sort { |a, b| b[1].length <=> a[1].length }.each do |row, group| |
65
|
|
|
puts "#{row} x #{group.length}" |
66
|
|
|
end |
67
|
|
|
|
68
|
|
|
GC.enable |
69
|
|
|
profile_allocations(name, &block) |
70
|
|
|
end |
71
|
|
|
|
72
|
|
|
def stuff |
73
|
|
|
u = User.first |
74
|
|
|
r = TopicQuery.new(u, {}).list_latest |
75
|
|
|
r.topics.to_a |
76
|
|
|
end |
77
|
|
|
|
78
|
|
|
stuff |
79
|
|
|
profile_allocations "stuff" do |
80
|
|
|
stuff |
81
|
|
|
end |
82
|
|
|
|
83
|
|
|
# Benchmark.bmbm do |x| |
84
|
|
|
# |
85
|
|
|
# x.report("find") do |
86
|
|
|
# 100.times{stuff} |
87
|
|
|
# end |
88
|
|
|
# |
89
|
|
|
# end |
90
|
|
|
# |
91
|
|
|
# x.report("grab 10 users id") do |
92
|
|
|
# 100.times{User.limit(10).select(:id).to_a} |
93
|
|
|
# end |
94
|
|
|
# |
95
|
|
|
# x.report("grab 10 users") do |
96
|
|
|
# 100.times{User.limit(10).to_a} |
97
|
|
|
# end |
98
|
|
|
# |
99
|
|
|
# profile("topic query") do |
100
|
|
|
# r = TopicQuery.new(u, {}).list_latest |
101
|
|
|
# r.topics.to_a |
102
|
|
|
# end |
103
|
|
|
|
104
|
|
|
# |
105
|
|
|
# RubyProf.start |
106
|
|
|
# |
107
|
|
|
# r = TopicQuery.new(u, {}).list_latest |
108
|
|
|
# r.topics.to_a |
109
|
|
|
# |
110
|
|
|
# result = RubyProf.stop |
111
|
|
|
# printer = RubyProf::GraphPrinter.new(result) |
112
|
|
|
# # printer = RubyProf::FlatPrinter.new(result) |
113
|
|
|
# printer.print(STDOUT, :min_percent => 2) |
114
|
|
|
# |
115
|
|
|
# exit |
116
|
|
|
# |
117
|
|
|
# # User.limit(10).to_a |
118
|
|
|
# User.limit(10).select(:created_at).to_a |
119
|
|
|
# |
120
|
|
|
# profile("limit 10") do |
121
|
|
|
# User.limit(10).select(:created_at).to_a |
122
|
|
|
# end |
123
|
|
|
# |
124
|
|
|
# exit |
125
|
|
|
# User.limit(10).to_a |
126
|
|
|
# exit |
127
|
|
|
# |
128
|
|
|
# User.select('id, 2 bob').first |
129
|
|
|
# Benchmark.bmbm do |x| |
130
|
|
|
# |
131
|
|
|
# x.report("find") do |
132
|
|
|
# 100.times{User.find(1)} |
133
|
|
|
# end |
134
|
|
|
# |
135
|
|
|
# x.report("grab 10 users created_at") do |
136
|
|
|
# 100.times{User.limit(10).select(:created_at).to_a} |
137
|
|
|
# end |
138
|
|
|
# |
139
|
|
|
# x.report("grab 10 users id") do |
140
|
|
|
# 100.times{User.limit(10).select(:id).to_a} |
141
|
|
|
# end |
142
|
|
|
# |
143
|
|
|
# x.report("grab 10 users") do |
144
|
|
|
# 100.times{User.limit(10).to_a} |
145
|
|
|
# end |
146
|
|
|
# |
147
|
|
|
# |
148
|
|
|
# x.report("pg direct grab 10 users") do |
149
|
|
|
# 100.times do |
150
|
|
|
# r = ActiveRecord::Base.connection.raw_connection.async_exec("select * from users limit 10") |
151
|
|
|
# r.fields.each_with_index do |f,i| |
152
|
|
|
# r.ftype(i) |
153
|
|
|
# end |
154
|
|
|
# r.each_row do |x| |
155
|
|
|
# x |
156
|
|
|
# end |
157
|
|
|
# end |
158
|
|
|
# end |
159
|
|
|
# |
160
|
|
|
# end |
161
|
|
|
# |
162
|
|
|
|
163
|
|
|
# profile("find") do |
164
|
|
|
# User.find(1) |
165
|
|
|
# end |
166
|
|
|
# puts |
167
|
|
|
# profile("where") do |
168
|
|
|
# User.where(id: 1).first |
169
|
|
|
# end |
170
|
|
|
|