如何度量Ruby语句,而不是函数,持续时间
有人告诉我,我的软件在生产中运行得太慢了。为了识别速度慢的代码行,我将插入如下所示的如何度量Ruby语句,而不是函数,持续时间,ruby,benchmarking,instrumentation,Ruby,Benchmarking,Instrumentation,有人告诉我,我的软件在生产中运行得太慢了。为了识别速度慢的代码行,我将插入如下所示的put语句,日志将指示输出的时间戳,通过查看日志的时间间隔,我可以看到哪些语句占用的时间最多 有没有更好的办法?我发现了一些在“功能”级别进行度量的宝石,但我的目标是“语句级别”度量 def process_api_carts(shop) puts "B7BEC BCAA here2.1" pager = Pager.new(shop.shopify_client, '/admin/checkouts.j
put
语句,日志将指示输出的时间戳,通过查看日志的时间间隔,我可以看到哪些语句占用的时间最多
有没有更好的办法?我发现了一些在“功能”级别进行度量的宝石,但我的目标是“语句级别”度量
def process_api_carts(shop)
puts "B7BEC BCAA here2.1"
pager = Pager.new(shop.shopify_client, '/admin/checkouts.json', 'checkouts') do |request|
request.params['updated_at_min'] = 1.day.ago.to_s(:abandoned_cart_timestamp)
end
puts "B7BEC BCAA here2.2"
carts = pager.to_a
puts "B7BEC BCAA here2.3"
carts.sort_by! do |cart|
Time.zone.parse(cart['created_at'])
end
puts "B7BEC BCAA here2.4"
carts.reverse!
puts "B7BEC BCAA here2.5"
# Some carts don't have a customer record attached. Don't know why
carts.reject! do |cart|
cart['customer'].nil?
end
puts "B7BEC BCAA here2.6"
carts.uniq! do |cart|
cart['customer']['id']
end
puts "B7BEC BCAA here2.7"
carts.each do |checkout|
begin
puts "B7BEC BCAA here2.8"
abandoned_cart = shop.abandoned_carts.create(
body: checkout.to_json,
shopify_id: checkout['id']
)
puts "B7BEC BCAA here2.9"
shop.events.create(
topic: Events::CartAbandoned::TOPIC,
body: {
type: 'api',
abandoned_cart_id: abandoned_cart.id
}.to_json
)
puts "B7BEC BCAA here2.10"
end
end
end
你的工作流程似乎有点混乱。为什么不拒绝,选择uniq,然后按相反顺序排序。来自Ruby的标准库。这是最好的方法,因为您可以轻松禁用它,而无需删除所有的
放置;e、 图中显示的代码片段并没有本质上的慢,尽管我不明白为什么你要费心倒车。你也在分割那些同样容易在线完成的工作(拒绝)。不太可能是这段代码的速度慢了,而是在以后进行任何API调用的过程中。您可能也想看看这段代码。一些好的技巧,尤其是StackProf可能适合您的场景:您可以将其缩小到carts.select{c}c['customer']}.uniq{cart}cart['customer']['id']}.sort{a,b | Time.zone.parse(b['created|u at'])Time.zone.parse(a['created|u at'])。每个