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

Merge pull request 6760
This commit is contained in:
Ashwin Maroli 2020-05-21 15:36:14 +05:30 committed by GitHub
parent 5649cac7e7
commit 589b122416
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 23 deletions

View File

@ -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:

View File

@ -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"

View File

@ -10,31 +10,11 @@ module Jekyll
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] }

58
lib/jekyll/profiler.rb Normal file
View File

@ -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

View File

@ -10,7 +10,7 @@ module Jekyll
: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.
#
@ -26,6 +26,7 @@ module Jekyll
@filter_cache = {}
@reader = Reader.new(self)
@profiler = Profiler.new(self)
@regenerator = Regenerator.new(self)
@liquid_renderer = LiquidRenderer.new(self)
@ -71,13 +72,14 @@ module Jekyll
#
# 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