aboutsummaryrefslogtreecommitdiffstats
path: root/toys/brhute-rb/brhute/brhute.rb
blob: 26021d3cbe62988bb8b8178d150343e4783da2e6 (plain) (blame)
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
require 'eventmachine'
require 'em-http'
#require 'fiber'

# Ressources - emhttp:
# https://github.com/igrigorik/em-http-request/blob/master/examples/multi.rb
# https://github.com/igrigorik/em-http-request/wiki/Keep-Alive-and-HTTP-Pipelining
# Ressources - fiber
# https://github.com/igrigorik/em-http-request/blob/master/examples/fibered-http.rb
# http://www.igvita.com/2009/05/13/fibers-cooperative-scheduling-in-ruby/

class Brhute_proc
    def initialize(urls, reqs_per_connection, verbose)
        attr_accessor :nb_connections
        @urls, @reqs_per_connection = urls, reqs_per_connection
        @nb_connections = 3
        @run()

    def run()
        EventMachine.run do
        #  Fiber.new{
        #    puts "Setting up HTTP request #1"
        #    data = async_fetch('http://www.google.com/')
        #    puts "Fetched page #1: #{data.response_header.status}"
        #    puts "Setting up HTTP request #2"
        #    data = async_fetch('http://www.google.com/toto')
        #    puts "Fetched page #2: #{data.response_header.status}"
        #    puts "Setting up HTTP request #3"
        #    data = async_fetch('http://www.google.com/tata')
        #    puts "Fetched page #3: #{data.response_header.status}"

        multi = EventMachine::MultiRequest.new

        reqs.each_with_index do |url, idx|
            http = EventMachine::HttpRequest.new(url, :connect_timeout => 1)
            req = http.get
            multi.add idx, req
        end

        multi.callback do
            p multi.responses[:callback].size
            p multi.responses[:errback].size

            EventMachine.stop
        end
        #  }.resume
        end
end

class Brhute
    def initialize(urls, nb_connections=3, req_per_connection=10, verbose=false)
        Brhute_proc(urls, nb_connections, req_per_connection, verbose)