~ubuntu-branches/ubuntu/utopic/ruby-excon/utopic

« back to all changes in this revision

Viewing changes to benchmarks/excon.rb

  • Committer: Package Import Robot
  • Author(s): Laurent Bigonville
  • Date: 2012-03-07 16:36:21 UTC
  • Revision ID: package-import@ubuntu.com-20120307163621-bztj2b8860dxpzs7
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'rubygems' if RUBY_VERSION < '1.9'
 
2
require 'bundler'
 
3
 
 
4
Bundler.require(:default)
 
5
Bundler.require(:benchmark)
 
6
 
 
7
require 'sinatra/base'
 
8
 
 
9
require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'excon')
 
10
 
 
11
module Excon
 
12
  class Server < Sinatra::Base
 
13
 
 
14
    def self.run
 
15
      Rack::Handler::WEBrick.run(
 
16
        Excon::Server.new,
 
17
        :Port => 9292,
 
18
        :AccessLog => [],
 
19
        :Logger => WEBrick::Log.new(nil, WEBrick::Log::ERROR)
 
20
      )
 
21
    end
 
22
 
 
23
    get '/data/:amount' do |amount|
 
24
      'x' * amount.to_i
 
25
    end
 
26
 
 
27
  end
 
28
end
 
29
 
 
30
def with_server(&block)
 
31
  pid = Process.fork do
 
32
    Excon::Server.run
 
33
  end
 
34
  loop do
 
35
    sleep(1)
 
36
    begin
 
37
      Excon.get('http://localhost:9292/api/foo')
 
38
      break
 
39
    rescue
 
40
    end
 
41
  end
 
42
  yield
 
43
ensure
 
44
  Process.kill(9, pid)
 
45
end
 
46
 
 
47
require 'tach'
 
48
 
 
49
size = 10_000
 
50
path = '/data/' << size.to_s
 
51
url = 'http://localhost:9292' << path
 
52
 
 
53
times = 1_000
 
54
 
 
55
with_server do
 
56
 
 
57
  Tach.meter(times) do
 
58
 
 
59
    tach('Excon') do
 
60
      Excon.get(url).body
 
61
    end
 
62
 
 
63
    excon = Excon.new(url)
 
64
    tach('Excon (persistent)') do
 
65
      excon.request(:method => 'get').body
 
66
    end
 
67
 
 
68
  end
 
69
end