Skip to content

Instantly share code, notes, and snippets.

@chulkilee
Created October 2, 2014 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chulkilee/35d414d71ea41fc3d9e3 to your computer and use it in GitHub Desktop.
Save chulkilee/35d414d71ea41fc3d9e3 to your computer and use it in GitHub Desktop.
Benchmark htauth gem to create md5 password
require 'benchmark'
require 'open3'
require 'htauth/md5'
SALT =
def run_open3
Open3.popen3('htpasswd', '-nbm', 'username', 'password') { | stdin, stdout, stderr | stdout.read }.strip.split(':')[1]
end
def run_backtick
`htpasswd -nbm username password`.strip.split(':')[1]
end
def run_hauth_dyn
HTAuth::Md5.new.encode('password')
end
$md5 = HTAuth::Md5.new
def run_hauth_reuse
$md5.encode('password')
end
n = 1000
Benchmark.bmbm do |x|
x.report('Open3') { n.times { run_open3 } }
x.report('Backtick') { n.times { run_backtick } }
x.report('HTAuth (dynamic)') { n.times { run_hauth_dyn } }
x.report('HTAuth (reuse)') { n.times { run_hauth_reuse } }
end
# ruby 2.1.3
# Mac OS X 10.9.5
# 2.7 GHz Intel Core i7 / 16 GB
Rehearsal ----------------------------------------------------
Open3 0.160000 0.510000 2.780000 ( 2.696596)
Backtick 0.080000 0.370000 2.470000 ( 2.488906)
HTAuth (dynamic) 2.200000 0.010000 2.210000 ( 2.199498)
HTAuth (reuse) 2.140000 0.000000 2.140000 ( 2.146822)
------------------------------------------- total: 9.600000sec
user system total real
Open3 0.140000 0.560000 2.730000 ( 2.661636)
Backtick 0.070000 0.390000 2.510000 ( 2.536059)
HTAuth (dynamic) 2.180000 0.030000 2.210000 ( 2.200213)
HTAuth (reuse) 2.100000 0.010000 2.110000 ( 2.108569)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment