Skip to content

Instantly share code, notes, and snippets.

@kwatch
Created March 2, 2011 03:01
Show Gist options
  • Save kwatch/850390 to your computer and use it in GitHub Desktop.
Save kwatch/850390 to your computer and use it in GitHub Desktop.
benchmark to measure cgi program
###
### benchmark to measure cgi program
###
require 'rubygems'
require 'benchmarker'
def cgi_file(file_name, content)
File.open(file_name, 'wb') do |f|
f.write(content)
end
File.chmod(0755, file_name)
end
cgi_file "hello1.cgi", <<'END'
#!/usr/bin/env ruby
print "Content-Type: text/html\r\n"
print "\r\n"
print "Hello"
END
cgi_file "hello2.cgi", <<'END'
#!/usr/bin/env ruby
require "cgi"
print "Content-Type: text/html\r\n"
print "\r\n"
print "Hello"
END
cgi_file "hello3.cgi", <<'END'
#!/usr/bin/env ruby
require "cgi"
cgi = CGI.new
print cgi.header
print "Hello"
END
cgi_file "hello4.cgi", <<'END'
#!/usr/bin/env ruby
require "cgi"
require "cgi/session"
cgi = CGI.new
print cgi.header
print "Hello"
END
env_cgi_content = <<'END'
#!/usr/bin/env ruby
require 'cgi'
#require 'cgi/session'
cgi = CGI.new
print cgi.header
title = "Environment Variables"
print "<html>\n"
print " <body>\n"
print " <h1>#{title}</h1>\n"
print " <table>\n"
odd = false
ENV.each do |k, v|
odd = ! odd
print " <tr class=\"#{odd ? 'odd' : 'even'}\">\n"
print " <td>#{k}</td><td>#{v}</td>\n"
print " </tr>\n"
end
print " </table>\n"
print " </body>\n"
print "</html>\n"
END
cgi_file "env1.cgi", env_cgi_content
cgi_file "env2.cgi", env_cgi_content.sub(/^#require/, 'require')
cgi_file "nil.cgi", <<'END'
#!/usr/bin/env ruby
nil
END
ENV['REQUEST_METHOD'] = 'GET'
ENV['REQUEST_URI'] = '/hello.cgi'
ENV['QUERY_STRING'] = ''
loop = 1000
#Benchmarker.new(:width=>24, :cycle=>5, :extra=>1) do |bm|
Benchmarker.new(:width=>24, :loop=>loop) do |bm|
bm.empty_task do
#loop.times { nil }
nil
end
(1..4).each do |n|
bm.task("hello#{n}.cgi") do
#loop.times { system("./hello#{n}.cgi > /dev/null") }
system("./hello#{n}.cgi > /dev/null")
end
end
(1..2).each do |n|
bm.task("env#{n}.cgi") do
#loop.times { system("./env#{n}.cgi > /dev/null") }
system("./env#{n}.cgi > /dev/null")
end
end
flag = true
#flag = false
if flag
bm.task("nil.cgi > /dev/null") do
#loop.times { system("./nil.cgi > /dev/null") }
system("./nil.cgi > /dev/null")
end
bm.task("nil.cgi") do
#loop.times { system("./nil.cgi") }
system("./nil.cgi")
end
end
end
__END__
# benchmarker.rb: release 0.0.0
# RUBY_VERSION: 1.8.7
# RUBY_PATCHLEVEL: 334
# RUBY_PLATFORM: i686-darwin10.6.0
#
# ## user sys total real
# (Empty) 0.0000 0.0000 0.0000 0.0006
# hello1.cgi 0.0600 0.4800 0.5400 11.4261
# hello2.cgi 0.0600 0.4600 0.5200 22.5066
# hello3.cgi 0.0600 0.4900 0.5500 22.6741
# hello4.cgi 0.0600 0.4400 0.5000 35.5769
# env1.cgi 0.0700 0.4700 0.5400 23.0187
# env2.cgi 0.0700 0.4500 0.5200 35.7513
# nil.cgi > /dev/null 0.0500 0.5300 0.5800 11.3228 (A)
# nil.cgi 0.0500 0.5300 0.5800 7.5846 (B)
#
# (A) 11.3228 - (B) 7.5846 = 3.7382
#
# ## real
# hello1.cgi 11.4261 - 3.7382 = 7.6879 # require nothing
# hello2.cgi 22.5066 - 3.7382 = 18.7684 # require 'cgi'
# hello3.cgi 22.6741 - 3.7382 = 18.9359 # require 'cgi'; cgi = CGI.new
# hello4.cgi 35.5769 - 3.7382 = 31.8387 # require 'cgi' & 'cgi/session'
# env1.cgi 23.0187 - 3.7382 = 19.2805 # hello3.cgi + html
# env2.cgi 35.7513 - 3.7382 = 32.0131 # hello4.cgi + html
#
#
# process invocation: 7.5846 (= (B))
# print statements: 0.1033 (= 7.6879 - 7.5846)
# require 'cgi': 11.0805 (= 18.7684 - 7.6879)
# cgi = CGI.new: 0.1675 (= 18.9359 - 18.7684)
# require 'cgi/session': 12.9028 (= 35.5769 - 22.6741)
# render html: 0.3446 (= 19.2805 - 18.9359)
#
# percentage:
# process invocation: 39.34 % (= 100 * 7.5846 / 19.2805)
# require 'cgi': 57.47 % (= 100 * 11.0805 / 19.2805)
# cgi = CGI.new: 0.87 % (= 100 * 0.1675 / 19.2805)
# print statements: 0.54 % (= 100 * 0.1033 / 19.2805)
# render html: 1.79 % (= 100 * 0.3446 / 19.2805)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment