Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional ruby-profiling with --profile-ruby #4034

Merged
merged 4 commits into from
Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ group(:maintenance) do
end

group(:development, :test) do
# for profiling
gem "ruby-prof"

gem "simplecov"
gem 'rack', "~> 1.5.1"
Expand Down
6 changes: 6 additions & 0 deletions lib/chef/application/apply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ class Chef::Application::Apply < Chef::Application
:description => 'Enable whyrun mode',
:boolean => true

option :profile_ruby,
:long => "--[no-]profile-ruby",
:description => "Output ruby execution profile graph",
:boolean => true,
:default => false

option :color,
:long => '--[no-]color',
:boolean => true,
Expand Down
8 changes: 7 additions & 1 deletion lib/chef/application/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Author:: AJ Christensen (<aj@opscode.com)
# Author:: Christopher Brown (<cb@opscode.com>)
# Author:: Mark Mzyk (mmzyk@opscode.com)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -55,6 +55,12 @@ class Chef::Application::Client < Chef::Application
:boolean => true,
:default => false

option :profile_ruby,
:long => "--[no-]profile-ruby",
:description => "Output ruby execution profile graph",
:boolean => true,
:default => false

option :color,
:long => '--[no-]color',
:boolean => true,
Expand Down
8 changes: 7 additions & 1 deletion lib/chef/application/solo.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# Author:: AJ Christensen (<aj@opscode.com>)
# Author:: Mark Mzyk (mmzyk@opscode.com)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -52,6 +52,12 @@ class Chef::Application::Solo < Chef::Application
:boolean => true,
:default => false

option :profile_ruby,
:long => "--[no-]profile-ruby",
:description => "Output ruby execution profile graph",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to copy the scary here, too?

:boolean => true,
:default => false

option :color,
:long => '--[no-]color',
:boolean => true,
Expand Down
32 changes: 32 additions & 0 deletions lib/chef/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
require 'chef/mixin/deprecation'
require 'ohai'
require 'rbconfig'
begin
require 'ruby-prof'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not do this in start_profiling or profiling_prereqs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 of one, half dozen of the other? I certainly can...

rescue LoadError
# ruby-prof is optional.
end

class Chef
# == Chef::Client
Expand Down Expand Up @@ -232,6 +237,8 @@ def initialize(json_attribs=nil, args={})
# @return Always returns true.
#
def run
start_profiling

run_error = nil

runlock = RunLock.new(Chef::Config.lockfile)
Expand Down Expand Up @@ -284,6 +291,9 @@ def run
run_completed_successfully
events.run_completed(node)

# keep this inside the main loop to get exception backtraces
end_profiling

# rebooting has to be the last thing we do, no exceptions.
Chef::Platform::Rebooter.reboot_if_needed!(node)
rescue Exception => run_error
Expand Down Expand Up @@ -891,6 +901,28 @@ def run_failed_notifications
attr_reader :override_runlist
attr_reader :specific_recipes

def profiling_prereqs!
if !defined? RubyProf
raise "You must have the ruby-prof gem installed in order to use --profile-ruby"
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a truly trivial nitpick, the begin is redundant here no?

end

def start_profiling
return unless Chef::Config[:profile_ruby]
profiling_prereqs!
RubyProf.start
end

def end_profiling
return unless Chef::Config[:profile_ruby]
profiling_prereqs!
path = Chef::FileCache.create_cache_path("graph_profile.out", false)
File.open(path, "w+") do |file|
RubyProf::GraphPrinter.new(RubyProf.stop).print(file, {})
end
Chef::Log.warn("Ruby execution profile dumped to #{path}")
end

def empty_directory?(path)
!File.exists?(path) || (Dir.entries(path).size <= 2)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/integration/client/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,23 @@ class ::Blah
result.error!
end

it "should complete with success when using --profile-ruby and output a profile file" do
file 'config/client.rb', <<EOM
local_mode true
cookbook_path "#{path_to('cookbooks')}"
EOM
result = shell_out!("#{chef_client} -c \"#{path_to('config/client.rb')}\" -o 'x::default' -z --profile-ruby", :cwd => chef_dir)
expect(File.exist?(path_to("config/local-mode-cache/cache/graph_profile.out"))).to be true
end

it "doesn't produce a profile when --profile-ruby is not present" do
file 'config/client.rb', <<EOM
local_mode true
cookbook_path "#{path_to('cookbooks')}"
EOM
result = shell_out!("#{chef_client} -c \"#{path_to('config/client.rb')}\" -o 'x::default' -z", :cwd => chef_dir)
expect(File.exist?(path_to("config/local-mode-cache/cache/graph_profile.out"))).to be false
end
end

when_the_repository "has a cookbook that generates deprecation warnings" do
Expand Down