需要在页面上输出每次require的调用时间。于是修改了 rails中的一个 方法,方法是module的一部分 。但是 如何remixins,却是大问题了。
或者另外一个方法直接修改 mixins以后的代码。 但是嵌套了多个module之后, 不清楚怎么去修改这个方法了。
以下是代码
[code]
module ActionController #:nodoc:
module Benchmarking #:nodoc:
module ClassMethods
def perform_action_with_benchmark
unless logger
perform_action_without_benchmark
else
runtime = [Benchmark::measure{ perform_action_without_benchmark }.real, 0.0001].max
@runtime_require = runtime #这句是添加的代码
log_message = "Completed inaa #{sprintf("%.5f", runtime)} (#{(1 / runtime).floor} reqs/sec)"
log_message << rendering_runtime(runtime) if defined?(@rendering_runtime)
log_message << active_record_runtime(runtime) if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
log_message << " | #{headers["Status"]}"
log_message << " [#{complete_request_uri rescue "unknown"}]"
logger.info(log_message)
end
end
end
end
end[/code]
或者另外一个方法直接修改 mixins以后的代码。 但是嵌套了多个module之后, 不清楚怎么去修改这个方法了。
以下是代码
[code]
module ActionController #:nodoc:
module Benchmarking #:nodoc:
module ClassMethods
def perform_action_with_benchmark
unless logger
perform_action_without_benchmark
else
runtime = [Benchmark::measure{ perform_action_without_benchmark }.real, 0.0001].max
@runtime_require = runtime #这句是添加的代码
log_message = "Completed inaa #{sprintf("%.5f", runtime)} (#{(1 / runtime).floor} reqs/sec)"
log_message << rendering_runtime(runtime) if defined?(@rendering_runtime)
log_message << active_record_runtime(runtime) if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
log_message << " | #{headers["Status"]}"
log_message << " [#{complete_request_uri rescue "unknown"}]"
logger.info(log_message)
end
end
end
end
end[/code]
本文介绍了一种在Rails应用中修改Benchmarking模块的方法,以便记录每次require调用的时间。通过覆盖perform_action_with_benchmark方法并添加日志记录,可以实现对性能的更详细监控。
1719

被折叠的 条评论
为什么被折叠?



