-
Notifications
You must be signed in to change notification settings - Fork 109
--benchmark, fixes #217 #218
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
bbac895
eb6e931
f631a4c
b63e1dc
49ad630
c844f00
78e1855
ddcfe75
419eff2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'image_optim/benchmark' | ||
require 'image_optim/bin_resolver' | ||
require 'image_optim/cache' | ||
require 'image_optim/config' | ||
|
@@ -8,6 +9,7 @@ | |
require 'image_optim/image_meta' | ||
require 'image_optim/optimized_path' | ||
require 'image_optim/path' | ||
require 'image_optim/table' | ||
require 'image_optim/timer' | ||
require 'image_optim/worker' | ||
require 'in_threads' | ||
|
@@ -162,6 +164,27 @@ def optimize_image_data(original_data) | |
end | ||
end | ||
|
||
def benchmark_image(original) | ||
src = Path.convert(original) | ||
return unless (workers = workers_for_image(src)) | ||
|
||
timer = timeout && Timer.new(timeout) | ||
workers.map do |worker| | ||
start = ElapsedTime.now | ||
dst = src.temp_path | ||
begin | ||
begin | ||
worker.optimize(src, dst, timeout: timer) | ||
|
||
rescue Errors::TimeoutExceeded | ||
# nop | ||
end | ||
Benchmark.new(src, dst, ElapsedTime.now - start, worker) | ||
ensure | ||
dst.unlink | ||
end | ||
end | ||
end | ||
|
||
# Optimize multiple images | ||
# if block given yields path and result for each image and returns array of | ||
# yield results | ||
|
@@ -186,6 +209,10 @@ def optimize_images_data(datas, &block) | |
run_method_for(datas, :optimize_image_data, &block) | ||
end | ||
|
||
def benchmark_images(paths, &block) | ||
run_method_for(paths, :benchmark_image, &block) | ||
gurgeous marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
class << self | ||
# Optimization methods with default options | ||
def method_missing(method, *args, &block) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
class ImageOptim | ||
# Benchmark result for one worker+src | ||
class Benchmark | ||
gurgeous marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
attr_reader :bytes, :elapsed, :worker | ||
|
||
def initialize(src, dst, elapsed, worker) | ||
@bytes = bytes_saved(src, dst) | ||
@elapsed = elapsed | ||
@worker = worker.class.bin_sym.to_s | ||
end | ||
|
||
def bytes_saved(src, dst) | ||
src, dst = src.size, dst.size | ||
return 0 if dst == 0 # failure | ||
return 0 if dst > src # the file got bigger | ||
|
||
src - dst | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
class ImageOptim | ||
# Handy class for pretty printing a table in the terminal. This is very simple, switch to Terminal | ||
# Table, Table Tennis or similar if we need more. | ||
class Table | ||
attr_reader :rows | ||
|
||
def initialize(rows) | ||
@rows = rows | ||
end | ||
|
||
def to_s | ||
gurgeous marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
lines = [] | ||
lines << render_row(columns) | ||
lines << render_sep | ||
rows.each do |row| | ||
lines << render_row(row.values) | ||
end | ||
lines.join("\n") | ||
end | ||
|
||
protected | ||
|
||
# array of column names | ||
def columns | ||
@columns ||= rows.first.keys | ||
end | ||
|
||
# should columns be justified left or right? | ||
def justs | ||
@justs ||= columns.map do |col| | ||
rows.first[col].is_a?(Numeric) ? :rjust : :ljust | ||
end | ||
end | ||
|
||
# max width of each column | ||
def widths | ||
@widths ||= columns.map do |col| | ||
values = rows.map{ |row| fmt(row[col]) } | ||
gurgeous marked this conversation as resolved.
Show resolved
Hide resolved
|
||
([col] + values).map(&:length).max | ||
end | ||
end | ||
|
||
# render an array of row values | ||
def render_row(values) | ||
values.map.with_index do |value, ii| | ||
fmt(value).send(justs[ii], widths[ii]) | ||
gurgeous marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
end.join(' ') | ||
end | ||
|
||
# render a separator line | ||
def render_sep | ||
render_row(widths.map{ |width| '-' * width }) | ||
end | ||
|
||
# format one cell value | ||
def fmt(value) | ||
if value.is_a?(Float) | ||
format('%0.3f', value) | ||
else | ||
value.to_s | ||
end | ||
end | ||
end | ||
end |
Uh oh!
There was an error while loading. Please reload this page.