Sha256: 3c2a5c7a41af51d956681cfa933c0f2259b47a4b8a120b2dab94967a85dcfb45

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

require 'rubygems'
require 'benchmark'
require 'net/http'
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

def http_request
  res = Net::HTTP.get_response(URI.parse('http://example.com'))
  raise "Body should be 'Hello'" unless res.body == 'Hello'
end

# def fakeweb
#   FakeWeb.register_uri(:get, 'http://example.com', :body => 'Hello')
#   yield
# ensure
#   FakeWeb.clean_registry
# end

def webmock
  WebMock.stub_request(:get, %r{http://example.com}).to_return(:body => 'Hello')
  yield
ensure
  WebMock.reset_webmock
end

def perform_benchmark(name)
  puts "\n\nBenchmarking #{name}:"
  Benchmark.benchmark do |b|
    %w(webmock).each do |type|
      b.report(type) do
        # this is a bit convoluted, but we want to ensure that each benchmark runs without the other library loaded,
        # so we fork off a sub-process before requiring the libraries.
        Process.fork do
          require type
          yield type
        end
        Process.wait
      end
    end
  end
end

n = 5000 #*2*2
# uri = Addressable::URI.heuristic_parse("http://www.example.com")
perform_benchmark("Single setup/teardown") do |type|
  send(type) { n.times { http_request } }
  # (2*n).times { WebMock::Util::URI.normalize_uri("http://www.example.com") }
end

perform_benchmark("Setup/teardown for each http request") do |type|
  n.times { send(type) { http_request } }
   # WebMock::Util::URI.normalize_uri("http://www.example.com")
end

# Output on my machine:
#
# Benchmarking Single setup/teardown:
# webmock  0.000000   0.000000  18.830000 ( 19.350281)
# fakeweb  0.000000   0.000000   1.930000 (  2.049569)
# 
# 
# Benchmarking Setup/teardown for each http request:
# webmock  0.000000   0.000000  21.350000 ( 21.795557)
# fakeweb  0.000000   0.000000   2.490000 (  2.707574)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
webmock-1.2.0 spec/benchmark.rb