Skip to content

Commit

Permalink
Add --profile flag to compile command
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jul 10, 2016
1 parent 0660600 commit 437bc51
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/nanoc/cli/commands/compile.rb
Expand Up @@ -14,6 +14,7 @@
SKIP - The item was deemed not outdated and was therefore not recompiled
EOS
flag nil, :profile, 'profile compilation' if Nanoc::Feature.enabled?('PROFILER')

module Nanoc::CLI::Commands
class Compile < ::Nanoc::CLI::CommandRunner
Expand Down Expand Up @@ -354,6 +355,29 @@ def log(level, action, path, duration)
end
end

# Records a profile using StackProf
class StackProfProfiler < Listener
PROFILE_FILE = 'tmp/stackprof_profile'.freeze

# @see Listener#enable_for?
def self.enable_for?(command_runner)
command_runner.options.fetch(:profile, false)
end

# @see Listener#start
def start
require 'stackprof'
StackProf.start(mode: :cpu)
end

# @see Listener#stop
def stop
# FIXME: crashes when StackProf is not loaded
StackProf.stop
StackProf.results(PROFILE_FILE)
end
end

attr_accessor :listener_classes

def initialize(options, arguments, command)
Expand Down Expand Up @@ -392,6 +416,7 @@ def default_listener_classes
Nanoc::CLI::Commands::Compile::TimingRecorder,
Nanoc::CLI::Commands::Compile::GCController,
Nanoc::CLI::Commands::Compile::FileActionPrinter,
Nanoc::CLI::Commands::Compile::StackProfProfiler,
]
end

Expand Down

0 comments on commit 437bc51

Please sign in to comment.