1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
require "./lib/contracts"
require "benchmark"
require "rubygems"
require "method_profiler"
require "ruby-prof"
require "open-uri"
include Contracts
def download url
open("http://www.#{url}/").read
end
Contract String => String
def contracts_download url
open("http://www.#{url}").read
end
@urls = %w{google.com bing.com}
def benchmark
Benchmark.bm 30 do |x|
x.report "testing download" do
100.times do |_|
download(@urls.sample)
end
end
x.report "testing contracts download" do
100.times do |_|
contracts_download(@urls.sample)
end
end
end
end
def profile
profilers = []
profilers << MethodProfiler.observe(Contract)
profilers << MethodProfiler.observe(Object)
profilers << MethodProfiler.observe(Contracts::MethodDecorators)
profilers << MethodProfiler.observe(Contracts::Decorator)
profilers << MethodProfiler.observe(Contracts::Support)
profilers << MethodProfiler.observe(UnboundMethod)
10.times do |_|
contracts_download(@urls.sample)
end
profilers.each { |p| puts p.report }
end
def ruby_prof
RubyProf.start
10.times do |_|
contracts_download(@urls.sample)
end
result = RubyProf.stop
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)
end
benchmark
profile
ruby_prof if ENV["FULL_BENCH"] # takes some time
|