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

Profile various stages of a site's build process #6760

Merged
merged 12 commits into from May 21, 2020
1 change: 1 addition & 0 deletions .rubocop.yml
Expand Up @@ -139,6 +139,7 @@ Style/FormatStringToken:
Exclude:
- lib/jekyll/utils/ansi.rb
- lib/jekyll/liquid_renderer/table.rb
- lib/jekyll/profiler.rb
Style/FrozenStringLiteralComment:
EnforcedStyle: always
Style/GuardClause:
Expand Down
1 change: 1 addition & 0 deletions lib/jekyll.rb
Expand Up @@ -69,6 +69,7 @@ module Jekyll
autoload :PathManager, "jekyll/path_manager"
autoload :PluginManager, "jekyll/plugin_manager"
autoload :Publisher, "jekyll/publisher"
autoload :Profiler, "jekyll/profiler"
autoload :Reader, "jekyll/reader"
autoload :Regenerator, "jekyll/regenerator"
autoload :RelatedPosts, "jekyll/related_posts"
Expand Down
22 changes: 1 addition & 21 deletions lib/jekyll/liquid_renderer/table.rb
Expand Up @@ -10,31 +10,11 @@ def initialize(stats)
end

def to_s(num_of_rows = 50)
tabulate(data_for_table(num_of_rows))
Jekyll::Profiler.tabulate(data_for_table(num_of_rows))
end

private

def tabulate(data)
require "terminal-table"

header = data.shift
footer = data.pop
output = +"\n"

table = Terminal::Table.new do |t|
t << header
t << :separator
data.each { |row| t << row }
t << :separator
t << footer
t.style = { :alignment => :right, :border_top => false, :border_bottom => false }
t.align_column(0, :left)
end

output << table.to_s << "\n"
end

# rubocop:disable Metrics/AbcSize
def data_for_table(num_of_rows)
sorted = @stats.sort_by { |_, file_stats| -file_stats[:time] }
Expand Down
58 changes: 58 additions & 0 deletions lib/jekyll/profiler.rb
@@ -0,0 +1,58 @@
# frozen_string_literal: true

module Jekyll
class Profiler
TERMINAL_TABLE_STYLES = {
:alignment => :right,
:border_top => false,
:border_bottom => false,
}.freeze
private_constant :TERMINAL_TABLE_STYLES

def self.tabulate(table_rows)
require "terminal-table"

rows = table_rows.dup
header = rows.shift
footer = rows.pop
output = +"\n"

table = Terminal::Table.new do |t|
t << header
t << :separator
rows.each { |row| t << row }
t << :separator
t << footer
t.style = TERMINAL_TABLE_STYLES
t.align_column(0, :left)
end

output << table.to_s << "\n"
end

def initialize(site)
@site = site
end

def profile_process
profile_data = { "PHASE" => "TIME" }
total_time = 0

[:reset, :read, :generate, :render, :cleanup, :write].each do |method|
start_time = Time.now
@site.send(method)
end_time = (Time.now - start_time).round(4)
profile_data[method.to_s.upcase] = format("%.4f", end_time)
total_time += end_time
end

profile_data["TOTAL TIME"] = format("%.4f", total_time)

Jekyll.logger.info "\nBuild Process Summary:"
Jekyll.logger.info Profiler.tabulate(Array(profile_data))

Jekyll.logger.info "\nSite Render Stats:"
@site.print_stats
end
end
end
6 changes: 4 additions & 2 deletions lib/jekyll/site.rb
Expand Up @@ -10,7 +10,7 @@ class Site
:gems, :plugin_manager, :theme

attr_accessor :converters, :generators, :reader
attr_reader :regenerator, :liquid_renderer, :includes_load_paths, :filter_cache
attr_reader :regenerator, :liquid_renderer, :includes_load_paths, :filter_cache, :profiler

# Public: Initialize a new Site.
#
Expand All @@ -26,6 +26,7 @@ def initialize(config)
@filter_cache = {}

@reader = Reader.new(self)
@profiler = Profiler.new(self)
@regenerator = Regenerator.new(self)
@liquid_renderer = LiquidRenderer.new(self)

Expand Down Expand Up @@ -71,13 +72,14 @@ def config=(config)
#
# Returns nothing.
def process
return profiler.profile_process if config["profile"]

reset
read
generate
render
cleanup
write
print_stats if config["profile"]
end

def print_stats
Expand Down