Add byte counter

This commit is contained in:
Florian Weingarten 2015-06-07 16:47:26 +00:00
parent 1e9163fdf4
commit 7bc9e1aae6
4 changed files with 29 additions and 8 deletions

View File

@ -22,6 +22,11 @@ module Jekyll
end
end
def increment_bytes(filename, bytes)
@stats[filename][:bytes] ||= 0
@stats[filename][:bytes] += bytes
end
def increment_time(filename, time)
@stats[filename][:time] ||= 0.0
@stats[filename][:time] += time

View File

@ -16,18 +16,28 @@ module Jekyll
def render(*args)
measure_time do
@template.render(*args)
measure_bytes do
@template.render(*args)
end
end
end
def render!(*args)
measure_time do
@template.render!(*args)
measure_bytes do
@template.render!(*args)
end
end
end
private
def measure_bytes
str = yield
ensure
@renderer.increment_bytes(@filename, str.bytesize)
end
def measure_time
before = Time.now
yield

View File

@ -57,11 +57,11 @@ module Jekyll
end
def table_widths(data)
widths = [ 0, 0, 0 ]
widths = []
data.each do |row|
row.each_with_index do |cell, index|
widths[index] = [ cell.length, widths[index] ].max
widths[index] = [ cell.length, widths[index] ].compact.max
end
end
@ -72,17 +72,23 @@ module Jekyll
sorted = @stats.sort_by{ |filename, file_stats| -file_stats[:time] }
sorted = sorted.slice(0, n)
table = [[ 'Filename', 'Count', 'Total time' ]]
table = [[ 'Filename', 'Count', 'Bytes', 'Time' ]]
sorted.each do |filename, file_stats|
row = []
row << filename
row << file_stats[:count].to_s
row << format_bytes(file_stats[:bytes])
row << "%.3f" % file_stats[:time]
table << row
end
table
end
def format_bytes(bytes)
bytes /= 1024.0
"%.2fK" % bytes
end
end
end

View File

@ -13,9 +13,9 @@ class TestLiquidRenderer < JekyllUnitTest
output = @renderer.stats_table
expected = [
/^Filename\s+|\s+Count\s+|\s+Total time$/,
/^-+\++-+\++-+$/,
/^_posts\/2010-01-09-date-override\.markdown\s+|\s+\d+\s+|\s+\d+\.\d{3}$/,
/^Filename\s+|\s+Count\s+|\s+Bytes\s+|\s+Time$/,
/^-+\++-+\++-+\++-+$/,
/^_posts\/2010-01-09-date-override\.markdown\s+|\s+\d+\s+|\s+\d+\.\d{2}K\s+|\s+\d+\.\d{3}$/,
]
expected.each do |regexp|