module TestProf
Ruby applications tests profiling tools.
Contains tools to analyze factories usage, integrate with Ruby profilers, profile your examples using ActiveSupport notifications (if any) and statically analyze your code with custom RuboCop
cops.
Example usage:
require 'test_prof' # Activate a tool by providing environment variable, e.g. TEST_RUBY_PROF=1 rspec ... # or manually in your code TestProf::RubyProf.run
See other modules for more examples.
This is shamelessly borrowed from RuboCop
RSpec github.com/rubocop-hq/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
Constants
- VERSION
Public Class Methods
Run block only if provided env var is present and equal to the provided value (if any). Contains workaround for applications using Spring.
# File lib/test_prof.rb, line 74 def activate(env_var, val = nil) if spring? notify_spring_detected ::Spring.after_fork do activate!(env_var, val) do notify_spring_activate env_var yield end end else activate!(env_var, val) { yield } end end
Return a path to store artifact
# File lib/test_prof.rb, line 94 def artifact_path(filename) create_artifact_dir with_timestamps( ::File.join( config.output_dir, with_report_suffix( filename ) ) ) end
Return absolute path to asset
# File lib/test_prof.rb, line 89 def asset_path(filename) ::File.expand_path(filename, ::File.join(::File.dirname(__FILE__), "..", "assets")) end
# File lib/test_prof.rb, line 31 def config @config ||= Configuration.new end
# File lib/test_prof.rb, line 35 def configure yield config end
# File lib/test_prof.rb, line 107 def create_artifact_dir FileUtils.mkdir_p(config.output_dir)[0] end
Returns true if we're inside Minitest
# File lib/test_prof.rb, line 45 def minitest? defined?(Minitest) end
Returns the current process time
# File lib/test_prof.rb, line 57 def now Process.clock_gettime(Process::CLOCK_MONOTONIC) end
Require gem and shows a custom message if it fails to load
# File lib/test_prof.rb, line 63 def require(gem_name, msg = nil) Kernel.require gem_name block_given? ? yield : true rescue LoadError log(:error, msg) if msg false end
Returns true if we're inside RSpec
# File lib/test_prof.rb, line 40 def rspec? defined?(RSpec::Core) end
Returns true if Spring is used and not disabled
# File lib/test_prof.rb, line 50 def spring? # See https://github.com/rails/spring/blob/577cf01f232bb6dbd0ade7df2df2ac209697e741/lib/spring/binstub.rb disabled = ENV["DISABLE_SPRING"] defined?(::Spring::Application) && (disabled.nil? || disabled.empty? || disabled == "0") end
Private Class Methods
# File lib/test_prof.rb, line 113 def activate!(env_var, val) yield if ENV[env_var] && (val.nil? || val === ENV[env_var]) end
# File lib/test_prof.rb, line 135 def notify_spring_activate(env_var) log :info, "Activating #{env_var} with `Spring.after_fork`" end
# File lib/test_prof.rb, line 129 def notify_spring_detected return if instance_variable_defined?(:@spring_notified) log :info, "Spring detected" @spring_notified = true end
# File lib/test_prof.rb, line 123 def with_report_suffix(path) return path if config.report_suffix.nil? "#{path.sub(/\.\w+$/, "")}-#{config.report_suffix}#{::File.extname(path)}" end
# File lib/test_prof.rb, line 117 def with_timestamps(path) return path unless config.timestamps? timestamps = "-#{now.to_i}" "#{path.sub(/\.\w+$/, "")}#{timestamps}#{::File.extname(path)}" end