Skip to content

Instantly share code, notes, and snippets.

@pd
Created November 3, 2015 19:49
Show Gist options
  • Save pd/df5b38fed1291e82f672 to your computer and use it in GitHub Desktop.
Save pd/df5b38fed1291e82f672 to your computer and use it in GitHub Desktop.
Quick script to benchmark & profile ApiAuth request validation
# Don't use bundle. Run from your api_auth checkout.
#
# ENV:
# V = {local, gem}
# use an installed gem, or the current working dir
# M = {bench, object, cpu, wall}
# run benchmark/ips, or choose to profile object allocations or cpu cost
require 'benchmark/ips'
require 'pry'
require 'stackprof'
require 'httpi'
if ENV['V'] == 'local'
$:.unshift(File.expand_path('./lib'))
require 'api_auth'
elsif ENV['V'] == 'gem'
require 'api_auth'
else
puts "Use V=local or V=gem"
exit 1
end
puts $LOADED_FEATURES.grep(/api_auth/).first
ACCESS_ID = 'foobar'
SECRET_KEY = 'private'
# Arbitrary request taken from:
# https://github.com/mgomes/api_auth/blob/03181b36/spec/api_auth_spec.rb#L490-L497
request = HTTPI::Request.new("http://localhost/resource.xml?foo=bar&bar=foo")
request.headers.merge!(
'content-type' => 'text/plain',
'content-md5' => '1B2M2Y8AsgTpgAmY7PhCfg==',
'date' => Time.now.utc.httpdate
)
ApiAuth.sign!(request, ACCESS_ID, SECRET_KEY)
case mode = ENV.fetch('M', :object).to_sym
when :object
StackProf.run(mode: mode, out: "#{mode}-#{ENV['V']}-profile.dump") do
ApiAuth.authentic?(request, SECRET_KEY)
end
when :cpu, :wall
StackProf.run(mode: mode, out: "#{mode}-#{ENV['V']}-profile.dump") do
500.times { ApiAuth.authentic?(request, SECRET_KEY) }
end
else
Benchmark.ips do |x|
x.config(warmup: 1, time: 2)
x.report('authentic?') { ApiAuth.authentic?(request, SECRET_KEY) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment