Last active
May 26, 2017 14:26
-
-
Save jjaffeux/bdf3454eb2f6b21fc98592c4aa9813b7 to your computer and use it in GitHub Desktop.
benchmark image optim solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'base64' | |
require 'benchmark' | |
require 'fileutils' | |
sample_base64_images = [ | |
"iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAMAAABiM0N1AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAJcEhZcwAACxMAAAsTAQCanBgAAAGkUExURUdwTLlsCrl1FA0JAtKFDRALAgsIAlQ2CLV1EwkHAc6FCyAVBQYFAbZ4ELd1Dq93ESUWA0onBGE8C69uDKNSBygUArJzD8eCDsd/DMyCColKC3E4BXdNC7ZvC4dODIVQC3lDCykOAv/UPF8QA/3OL/qpDSoTBP/RN/zIKf/WRf/ZO/7ZUf/faPqxEf7bXv70x/vAJv////agDPSXC/GABRcDAPKIB/mzGv/icv/tqv/rofq5IPvDH//qmfKQCfq6F//jfv6/MiMJAP/ws/WlEvzUMvisGP+XI//xuv7zwP+OJPp9BP7HNv7ljf6gC/+gKORvBW8QBP+HI7ofH/+zLv7ohP/kWVg8Fz4mFM8uLPvMUfjNOv/gSP7tkeM3N/T3+m1PIf+pLPradjIOBOfs8fuNBKqfnYRoLcnW4eebE7tXD0wNAe28Mdnf5d2JEpx1H7CHJrK1u4gqEOutJ/7sPdWrK72hSe8+PsGhJPh5BD4HAIpwRstqEE87NOnLUmdUTXhsaY5+dX9ZHb3I0//205waF+LJPPlCQ5yNhKWUJ9a+cv1ERljKUBcAAAAidFJOUwDxHkDuLGkNORvRllJuhlG115W0+tKhpZO46vJ00c633fik2umpAAAHuElEQVRYw+3Y+Vva6BYH8CoIAu61Vbvd+1BkCcsggqCIGmRTo7iwCagsMghFFhFccO+obeefvue8iQtKrfa3O898qQEh+XjOmzch6atX/+YflubW9o7+vgGxWDzQ19/R2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wTIJgp8RMRjbfgCESDb4PuIsPE/+7wrn4+vZWkEgomXCKgU7XZlWe4Yz7IhL1ELAjkb5U4gEiHwFpmLO6pKkQcRT5FxOzJBWLlDOkQ2E7LcjmzPEAT1RtVfhEEhVE3IuHePfJ9GSJbViFJ0lCH2Lq8zZTMwdm1qiNx4vH281TyHgPlq9lUWCNeta5tVY4L59qX56Lr3p680X6me3rvuY7ucHoLxr/HpwfZ+eOP37+/Ly/61Y3weTh0dPjvG637/+PhjZN2KSsv/XuggB0EYiMKw4gaaoHFBOYC7Jq4epkDn/ueSNrQvNk3QNPFbCC74MzMIYDy3xM7izB7ymjtLt2dBuyJyJhinuFaAqOFUXaEDEmPSUNNeNYJEcahboUOjidbVRCNI9bzTtWuzDs3pYz2k9xkEdT6VNUh4JGeHjd3/4ENT7vRIuJvYwcr2o4Unov5gIO4meuuEnB652cFIChnf0YAPAdqXkFHFEHIi2gKwIuGhRZD7LjRbeMMGnp1/DRFDQUWIq1WHwrH/Hqpfrf7YbyCwkqXT+4iYAAAAAElFTkSuQmCC" | |
] | |
images = sample_base64_images.map { |image| Base64.decode64(image) } | |
def write_image(path, image) | |
FileUtils.mkdir_p(File.expand_path("..", path)) | |
open(path, "wb") { |file| file << image } | |
end | |
def get_dir_filessize(dir) | |
Dir | |
.glob(File.join(dir, '**', '*.png')) | |
.map { |file| File.size(file) } | |
.inject(:+) | |
.to_f | |
end | |
tests = [ | |
{ | |
name: "pngout -s0", | |
path: "benchoptimg/pngout_s0", | |
code: lambda { |path| | |
images.each.with_index { |image, i| | |
image_path = "#{path}/#{i}.png" | |
write_image(image_path, image) | |
`pngout #{image_path} -s0` | |
} | |
} | |
}, | |
{ | |
name: "zopflipng 20iter", | |
path: "benchoptimg/zopflipng_20iter", | |
code: lambda { |path| | |
images.each.with_index { |image, i| | |
image_path = "#{path}/#{i}.png" | |
write_image(image_path, image) | |
`zopflipng --iterations=20 --splitting=2 --filters=p --lossy_8bit --lossy_transparent -y #{image_path} #{image_path}` | |
} | |
} | |
}, | |
{ | |
name: "zopflipng max", | |
path: "benchoptimg/zopflipng_max", | |
code: lambda { |path| | |
images.each.with_index { |image, i| | |
image_path = "#{path}/#{i}.png" | |
write_image(image_path, image) | |
`zopflipng --iterations=500 --splitting=3 --filters=01234mepb --lossy_8bit --lossy_transparent -y #{image_path} #{image_path}` | |
} | |
} | |
} | |
] | |
FileUtils.rm_rf("benchoptimg") | |
puts "-- Time benchmark" | |
Benchmark.bm do |x| | |
x.report("raw") { | |
FileUtils.mkdir_p("benchoptimg/raw") | |
images.each.with_index { |image, i| write_image("benchoptimg/raw/#{i}.png", image) } | |
} | |
tests.each do |test| | |
x.report(test[:name]) { test[:code].call(test[:path]) } | |
end | |
end | |
puts "\r\n-- Size benchmark" | |
no_compression_size = get_dir_filessize("benchoptimg/raw") | |
test_results = tests.map { |test| | |
size = get_dir_filessize(test[:path]) | |
difference = (no_compression_size - size) / no_compression_size * 100.0 | |
sign = size < no_compression_size ? "-" : "+" | |
"#{test[:name]}: #{size} bytes (#{sign}#{difference.abs} %)" | |
} | |
puts <<RESULTS | |
Raw: #{no_compression_size} bytes | |
#{test_results.join("\r\n")} | |
RESULTS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage
ruby test.rb