discourse/script/measure.rb

Failed to ignore revisions in .git-blame-ignore-revs.

172 lines
3.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2015-03-04 22:01:55 +08:00
# using this script to try figure out why Ruby 2 is slower than 1.9
2013-09-17 19:34:38 +08:00
require 'flamegraph'
Flamegraph.generate('test.html', fidelity: 2) do
require File.expand_path("../../config/environment", __FILE__)
end
exit
2013-08-22 07:01:30 +08:00
require 'memory_profiler'
result = MemoryProfiler.report do
2013-09-17 08:23:44 +08:00
require File.expand_path("../../config/environment", __FILE__)
end
result.pretty_print
exit
2013-08-22 07:01:30 +08:00
2013-09-17 08:23:44 +08:00
require 'benchmark'
2013-08-22 07:01:30 +08:00
def profile_allocations(name)
GC.disable
initial_size = ObjectSpace.count_objects
yield
changes = ObjectSpace.count_objects
changes.each do |k, _|
2013-08-22 07:01:30 +08:00
changes[k] -= initial_size[k]
end
puts "#{name} changes"
2017-07-28 09:20:09 +08:00
changes.sort { |a, b| b[1] <=> a[1] }.each do |a, b|
2013-08-22 07:01:30 +08:00
next if b <= 0
# 1 extra hash for tracking
2017-07-28 09:20:09 +08:00
puts "#{a} #{a == :T_HASH ? b - 1 : b}"
2013-08-22 07:01:30 +08:00
end
GC.enable
end
def profile(name, &block)
puts "Profiling all object allocation for #{name}"
GC.start
GC.disable
items = []
objs = []
ObjectSpace.trace_object_allocations do
block.call
ObjectSpace.each_object do |o|
objs << o
end
objs.each do |o|
g = ObjectSpace.allocation_generation(o)
if g
l = ObjectSpace.allocation_sourceline(o)
f = ObjectSpace.allocation_sourcefile(o)
c = ObjectSpace.allocation_class_path(o)
m = ObjectSpace.allocation_method_id(o)
items << "Allocated #{c} in #{m} #{f}:#{l}"
end
end
end
2017-07-28 09:20:09 +08:00
items.group_by { |x| x }.sort { |a, b| b[1].length <=> a[1].length }.each do |row, group|
2013-08-22 07:01:30 +08:00
puts "#{row} x #{group.length}"
end
GC.enable
profile_allocations(name, &block)
end
2013-08-29 13:27:33 +08:00
def stuff
u = User.first
r = TopicQuery.new(u, {}).list_latest
r.topics.to_a
end
stuff
profile_allocations "stuff" do
stuff
end
# Benchmark.bmbm do |x|
2017-07-28 09:20:09 +08:00
#
2013-08-29 13:27:33 +08:00
# x.report("find") do
# 100.times{stuff}
# end
2017-07-28 09:20:09 +08:00
#
2013-08-29 13:27:33 +08:00
# end
2017-07-28 09:20:09 +08:00
#
2013-08-29 13:27:33 +08:00
# x.report("grab 10 users id") do
# 100.times{User.limit(10).select(:id).to_a}
# end
2017-07-28 09:20:09 +08:00
#
2013-08-29 13:27:33 +08:00
# x.report("grab 10 users") do
# 100.times{User.limit(10).to_a}
# end
2017-07-28 09:20:09 +08:00
#
2013-08-29 13:27:33 +08:00
# profile("topic query") do
# r = TopicQuery.new(u, {}).list_latest
# r.topics.to_a
# end
2017-07-28 09:20:09 +08:00
#
2013-08-22 07:01:30 +08:00
# RubyProf.start
2017-07-28 09:20:09 +08:00
#
2013-08-29 13:27:33 +08:00
# r = TopicQuery.new(u, {}).list_latest
# r.topics.to_a
2017-07-28 09:20:09 +08:00
#
2013-08-22 07:01:30 +08:00
# result = RubyProf.stop
2013-08-29 13:27:33 +08:00
# printer = RubyProf::GraphPrinter.new(result)
# # printer = RubyProf::FlatPrinter.new(result)
2013-08-22 07:01:30 +08:00
# printer.print(STDOUT, :min_percent => 2)
2017-07-28 09:20:09 +08:00
#
2013-08-22 07:01:30 +08:00
# exit
2017-07-28 09:20:09 +08:00
#
2013-08-22 07:01:30 +08:00
# # User.limit(10).to_a
# User.limit(10).select(:created_at).to_a
2017-07-28 09:20:09 +08:00
#
2013-08-22 07:01:30 +08:00
# profile("limit 10") do
# User.limit(10).select(:created_at).to_a
# end
2017-07-28 09:20:09 +08:00
#
2013-08-22 07:01:30 +08:00
# exit
# User.limit(10).to_a
# exit
#
2013-08-29 13:27:33 +08:00
# User.select('id, 2 bob').first
# Benchmark.bmbm do |x|
2017-07-28 09:20:09 +08:00
#
2013-08-29 13:27:33 +08:00
# x.report("find") do
# 100.times{User.find(1)}
# end
2017-07-28 09:20:09 +08:00
#
2013-08-29 13:27:33 +08:00
# x.report("grab 10 users created_at") do
# 100.times{User.limit(10).select(:created_at).to_a}
# end
2017-07-28 09:20:09 +08:00
#
2013-08-29 13:27:33 +08:00
# x.report("grab 10 users id") do
# 100.times{User.limit(10).select(:id).to_a}
# end
2017-07-28 09:20:09 +08:00
#
2013-08-29 13:27:33 +08:00
# x.report("grab 10 users") do
# 100.times{User.limit(10).to_a}
# end
2017-07-28 09:20:09 +08:00
#
#
2013-08-29 13:27:33 +08:00
# x.report("pg direct grab 10 users") do
# 100.times do
# r = ActiveRecord::Base.connection.raw_connection.async_exec("select * from users limit 10")
# r.fields.each_with_index do |f,i|
# r.ftype(i)
# end
# r.each_row do |x|
# x
# end
# end
# end
2017-07-28 09:20:09 +08:00
#
2013-08-29 13:27:33 +08:00
# end
2017-07-28 09:20:09 +08:00
#
2013-08-22 07:01:30 +08:00
# profile("find") do
# User.find(1)
# end
# puts
# profile("where") do
# User.where(id: 1).first
# end