2019-06-25 21:40:58 +02:00
|
|
|
require "benchmark"
|
2020-06-29 01:32:52 +02:00
|
|
|
require "option_parser"
|
2019-06-29 01:17:09 +02:00
|
|
|
require "../src/sodium"
|
2019-06-25 21:40:58 +02:00
|
|
|
require "openssl"
|
|
|
|
require "openssl/digest"
|
|
|
|
|
|
|
|
output_size = 64
|
|
|
|
sizes = [16, 64, 256, 1024, 8192, 16384]
|
2020-06-29 01:32:52 +02:00
|
|
|
|
|
|
|
optp = OptionParser.new
|
|
|
|
optp.on("--output-size", "default: 64") { |arg| output_size = arg.to_i }
|
|
|
|
optp.on("--input-sizes=ARG", "comma separated list of input sizes") { |arg| sizes = arg.split(",").map(&.to_i).to_a }
|
|
|
|
# optp.on("", "") { |arg| }
|
|
|
|
optp.parse
|
|
|
|
|
2019-06-25 21:40:58 +02:00
|
|
|
bufs = sizes.map { |size| Bytes.new size }.to_a
|
|
|
|
|
|
|
|
puts "Compare against 'openssl speed digestname'"
|
|
|
|
puts "'crystal run --release benchmarks/blake2b.cr sha1 sha256'"
|
|
|
|
|
|
|
|
Benchmark.ips(warmup: 0.5) do |bm|
|
|
|
|
sizes.each_with_index do |size, i|
|
|
|
|
bm.report "blake2b new obj per iter #{size}" do
|
2019-06-29 01:17:09 +02:00
|
|
|
d = Sodium::Digest::Blake2b.new 64
|
2019-06-25 21:40:58 +02:00
|
|
|
d.update bufs[i]
|
2020-05-19 08:13:50 +02:00
|
|
|
d.final
|
2019-06-25 21:40:58 +02:00
|
|
|
end
|
|
|
|
|
2019-06-29 01:17:09 +02:00
|
|
|
d = Sodium::Digest::Blake2b.new output_size
|
2019-06-25 21:40:58 +02:00
|
|
|
bm.report "blake2b reset per iter #{size}" do
|
|
|
|
d.reset
|
|
|
|
d.update bufs[i]
|
2020-05-19 08:13:50 +02:00
|
|
|
d.final
|
2019-06-25 21:40:58 +02:00
|
|
|
end
|
|
|
|
|
2019-06-29 01:17:09 +02:00
|
|
|
d = Sodium::Digest::Blake2b.new output_size
|
2019-06-25 21:40:58 +02:00
|
|
|
dst = Bytes.new d.digest_size
|
|
|
|
bm.report "blake2b reset reusing buffer per iter #{size}" do
|
|
|
|
d.reset
|
|
|
|
d.update bufs[i]
|
2020-05-19 08:13:50 +02:00
|
|
|
d.final dst
|
2019-06-25 21:40:58 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ARGV.each do |arg|
|
|
|
|
sizes.each_with_index do |size, i|
|
|
|
|
bm.report "#{arg} new obj per iter #{size}" do
|
|
|
|
d = OpenSSL::Digest.new arg
|
|
|
|
d.update bufs[i]
|
2020-05-19 08:13:50 +02:00
|
|
|
d.final
|
2019-06-25 21:40:58 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
d = OpenSSL::Digest.new arg
|
|
|
|
bm.report "#{arg} reset per iter #{size}" do
|
|
|
|
d.reset
|
|
|
|
d.update bufs[i]
|
2020-05-19 08:13:50 +02:00
|
|
|
d.final
|
2019-06-25 21:40:58 +02:00
|
|
|
end
|
|
|
|
|
2020-05-19 08:13:50 +02:00
|
|
|
d = OpenSSL::Digest.new arg
|
|
|
|
dst = Bytes.new d.digest_size
|
|
|
|
bm.report "#{arg} reset reusing buffer per iterr #{size}" do
|
|
|
|
d.reset
|
|
|
|
d.update bufs[i]
|
|
|
|
d.final dst
|
|
|
|
end
|
2019-06-25 21:40:58 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|