【问题标题】:How can I profile `require` statements in Ruby?如何在 Ruby 中分析 `require` 语句?
【发布时间】:2020-06-30 02:37:30
【问题描述】:

我有兴趣查看我的 Ruby 代码中的每个 require 语句需要多长时间(以秒为单位)加载。如何临时覆盖该方法并打印出一些慢速加载的日志信息?

【问题讨论】:

    标签: ruby require


    【解决方案1】:

    您可以像这样覆盖Kernel.require

    module Kernel
      # make an alias of the original require
      alias_method :original_require, :require
    
      # rewrite require
      def require name
        start = Time.now
    
        # code to time
        ret = original_require name
    
        finish = Time.now
        diff = finish - start
    
        # TODO: This should really add up all of
        # the require calls for a certain module
        if diff > 0.1
          puts "#{diff} #{name}"
        end
        ret
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 2010-12-04
      • 2018-06-24
      相关资源
      最近更新 更多