Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 60 lines (52 sloc) 1.497 kb
aa775a3 @myronmarston Added a benchmark to test the performance characterstics of fakeweb v…
authored
1 require 'rubygems'
2 require 'benchmark'
3 require 'net/http'
4
5 def http_request
6 res = Net::HTTP.get_response(URI.parse('http://example.com'))
7 raise "Body should be 'Hello'" unless res.body == 'Hello'
8 end
9
10 def fakeweb
11 FakeWeb.register_uri(:get, 'http://example.com', :body => 'Hello')
12 yield
13 ensure
14 FakeWeb.clean_registry
15 end
16
17 def webmock
18 WebMock.stub_request(:get, 'http://example.com').to_return(:body => 'Hello')
19 yield
20 ensure
e7a4c0d @myronmarston WebMock.reset_webmock is deprecated in favor of WebMock.reset!
authored
21 WebMock.reset!
aa775a3 @myronmarston Added a benchmark to test the performance characterstics of fakeweb v…
authored
22 end
23
24 def perform_benchmark(name)
25 puts "\n\nBenchmarking #{name}:"
26 Benchmark.benchmark do |b|
27 %w(webmock fakeweb).each do |type|
28 b.report(type) do
29 # this is a bit convoluted, but we want to ensure that each benchmark runs without the other library loaded,
30 # so we fork off a sub-process before requiring the libraries.
31 Process.fork do
32 require type
33 yield type
34 end
35 Process.wait
36 end
37 end
38 end
39 end
40
41 n = 5000
42 perform_benchmark("Single setup/teardown") do |type|
43 send(type) { n.times { http_request } }
44 end
45
46 perform_benchmark("Setup/teardown for each http request") do |type|
47 n.times { send(type) { http_request } }
48 end
49
50 # Output on my machine:
51 #
52 # Benchmarking Single setup/teardown:
356eca4 @myronmarston Update benchmarks.
authored
53 # webmock 0.000000 0.000000 6.950000 ( 6.981525)
54 # fakeweb 0.000000 0.010000 1.750000 ( 1.740679)
d34b6f7 @myronmarston Add quality spec that enforces whitespace policy.
authored
55 #
56 #
aa775a3 @myronmarston Added a benchmark to test the performance characterstics of fakeweb v…
authored
57 # Benchmarking Setup/teardown for each http request:
356eca4 @myronmarston Update benchmarks.
authored
58 # webmock 0.000000 0.000000 7.970000 ( 7.981383)
59 # fakeweb 0.000000 0.000000 2.210000 ( 2.203478)
Something went wrong with that request. Please try again.