Simultaneously URL call, is it possible?

Hi to all!

I need to test a functionality of an application before to go in
production environment, unfortunately it isn’t a ruby application, I
thought to use a script that will call a url 5 times simultaneously or
at least systematically, let’s say every 10 seconds, is it possible with
ruby? Maybe using watir (but I don’t think it can do simultaneus call)?
Any hint?

Thanks.

Best regards.

Toki T. wrote:

I need to test a functionality of an application before to go in
production environment, unfortunately it isn’t a ruby application, I
thought to use a script that will call a url 5 times simultaneously or
at least systematically, let’s say every 10 seconds, is it possible with
ruby? Maybe using watir (but I don’t think it can do simultaneus call)?
Any hint?

Yes. You could spawn threads and call the url from those.
%w[uri net/http].each{|l|require l}
10.times do
Thread.new
{Net::HTTP.get_response(URI.parse(‘http://www.google.com/’))}
end

Watir can support multiple, concurrent calls. Create separate
Watir::IE.new
in each thread.

Bret

Bret P. wrote:

Watir can support multiple, concurrent calls. Create separate
Watir::IE.new
in each thread.

Bret

Thanks, I didn’t know that watir support concurrent calls. If I create
multiple Watir::IE.new then they will use the same IE process?

matu wrote:

Toki T. wrote:

I need to test a functionality of an application before to go in
production environment, unfortunately it isn’t a ruby application, I
thought to use a script that will call a url 5 times simultaneously or
at least systematically, let’s say every 10 seconds, is it possible with
ruby? Maybe using watir (but I don’t think it can do simultaneus call)?
Any hint?

Yes. You could spawn threads and call the url from those.
%w[uri net/http].each{|l|require l}
10.times do
Thread.new
{Net::HTTP.get_response(URI.parse(‘http://www.google.com/’))}
end

Thanks for the answer.

It’s not clear the first line of your code, could you explain it?

Thanks.

El Jueves, 24 de Julio de 2008, Toki T. escribió:

It’s not clear the first line of your code, could you explain it?

%w[uri net/http].each{|l|require l}

%w[aaa bbb ccc] is an Array:

irb> %w[aaa bbb ccc]
[“aaa”, “bbb”, “ccc”]

so it’s a “each” method of an Array so:

require “uri”
require “net/http”

Well, I think is too much freak XD

On Thu, Jul 24, 2008 at 4:04 PM, Toki T. [email protected] wrote:

Thanks, I didn’t know that watir support concurrent calls. If I create
multiple Watir::IE.new then they will use the same IE process?

Yes. But you can use Watir::IE.new_process instead and then each one
will be
in a separate process.

Bret

Iñaki Baz C. wrote:

El Jueves, 24 de Julio de 2008, Toki T. escribió:

It’s not clear the first line of your code, could you explain it?

%w[uri net/http].each{|l|require l}

%w[aaa bbb ccc] is an Array:

irb> %w[aaa bbb ccc]
[“aaa”, “bbb”, “ccc”]

so it’s a “each” method of an Array so:

require “uri”
require “net/http”

Well, I think is too much freak XD

Ok, so this:

%w[uri net/http].each{|l|require l}
10.times do
Thread.new
{Net::HTTP.get_response(URI.parse(‘http://www.google.com/’))}
end

is equal to this:

require ‘net/http’
require ‘uri’
10.times do
Thread.new
{Net::HTTP.get_response(URI.parse(‘http://www.google.com/’))}
end

Good, I’ve never used require with array.

But if I run the code above I get an error: url_call.rb:4: odd number
list for Hash

I think it’s because of the curly braces that are viewed like an hash,
right? I don’t know how Thread works in Ruby, but it seems that the
statements in the curly braces need to be on the same line as Thread.new
otherwise it give the error above…

Toki T. wrote:

require ‘net/http’
require ‘uri’
10.times do
Thread.new
{Net::HTTP.get_response(URI.parse(‘http://www.google.com/’))}
end

Yes.

Good, I’ve never used require with array.

But if I run the code above I get an error: url_call.rb:4: odd number
list for Hash

I think it’s because of the curly braces that are viewed like an hash,
right? I don’t know how Thread works in Ruby, but it seems that the
statements in the curly braces need to be on the same line as Thread.new
otherwise it give the error above…

Thread.new gets a block, so you could as well write the code above as:

require ‘net/http’
require ‘uri’
10.times do
Thread.new do
Net::HTTP.get_response(URI.parse(‘http://www.google.com/’))
end
end

maybe this is a bit more clear to you?

On Fri, Jul 25, 2008 at 1:25 PM, Toki T. [email protected] wrote:

I think this needs a call to Thread#join after all the the threads have
been
created. Otherwise the script will exit before the threads have
executed.

Bret

S2 wrote:

Toki T. wrote:

require ‘net/http’
require ‘uri’
10.times do
Thread.new
{Net::HTTP.get_response(URI.parse(‘http://www.google.com/’))}
end

Yes.

Good, I’ve never used require with array.

But if I run the code above I get an error: url_call.rb:4: odd number
list for Hash

I think it’s because of the curly braces that are viewed like an hash,
right? I don’t know how Thread works in Ruby, but it seems that the
statements in the curly braces need to be on the same line as Thread.new
otherwise it give the error above…

Thread.new gets a block, so you could as well write the code above as:

require ‘net/http’
require ‘uri’
10.times do
Thread.new do
Net::HTTP.get_response(URI.parse(‘http://www.google.com/’))
end
end

maybe this is a bit more clear to you?

Thanks, I saw that the first curly brace need to be on the same line of
Thread.new.

I’ve tested the code above, but it seems that it doesn’t work like I
thought because on the web server and application server logs I saw only
one request when I run the code (of course with the correct address),
maybe my statement isn’t clear: I need to call a URL multiple time, like
if I press F5 on the browser…I’ve found a way using Watir example, but
without it would be better so I can use the script on any platform.

Thanks.

Best regards.

Bret P. wrote:

On Fri, Jul 25, 2008 at 1:25 PM, Toki T. [email protected] wrote:

I think this needs a call to Thread#join after all the the threads have
been
created. Otherwise the script will exit before the threads have
executed.

Bret

Something like this:

require ‘net/http’
require ‘uri’

threads = []
10.times do
threads << Thread.new
{Net::HTTP.get_response(URI.parse(‘http://www.google.com/’))}
end
threads.each {|x| x.join}

But if the original script exit before the threads have executed why I
see a request and not 0 request?

On Jul 25, 2008, at 1:02 PM, Bret P. wrote:

I think this needs a call to Thread#join after all the the threads
have been
created. Otherwise the script will exit before the threads have
executed.

why make it hard?

gem install threadify

require ‘rubygems’
require ‘open-uri’
require ‘threadify’

results =
%w( http://google.com http://yahoo.com http://
foo.bar.com ).threadify do |uri|
open(uri){|fd| fd.read}
end

a @ http://codeforpeople.com/

Ara Howard wrote:

On Jul 25, 2008, at 1:02 PM, Bret P. wrote:

I think this needs a call to Thread#join after all the the threads
have been
created. Otherwise the script will exit before the threads have
executed.

why make it hard?

gem install threadify

require ‘rubygems’
require ‘open-uri’
require ‘threadify’

results =
%w( http://google.com http://yahoo.com http://
foo.bar.com ).threadify do |uri|
open(uri){|fd| fd.read}
end

a @ http://codeforpeople.com/

Thanks for the help.

It seems that I’ve some problem with the gem threadify or rubygems…I i
run the code that you’ve poste I get the error:

ruby threadify.rb
./threadify.rb:6: undefined method threadify' for #<Array:0x38ce1c0> (NoMethodError) from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require’
from threadify.rb:3
Exit code: 1

This is the require in irb:

irb(main):001:0> require ‘rubygems’
=> false
irb(main):002:0> require ‘threadify’
=> true

I’ve try to run the threadify sample
(http://www.ruby-forum.com/topic/159362) still no way…

How can I solve this problem?

Thanks.

Toki T. wrote:

I’ve tested the code above, but it seems that it doesn’t work like I
thought because on the web server and application server logs I saw only
one request when I run the code (of course with the correct address),
maybe my statement isn’t clear: I need to call a URL multiple time, like
if I press F5 on the browser…I’ve found a way using Watir example, but
without it would be better so I can use the script on any platform.

It works if you run it from irb. If you call it from a script just call
Thread.join like Bret sad, or insert a sleep(10000) at the en of the
script.

On Jul 25, 2008, at 2:19 PM, Toki T. wrote:

`require’
from threadify.rb:3

Exit code: 1

This is the require in irb:

irb(main):001:0> require ‘rubygems’
=> false
irb(main):002:0> require ‘threadify’
=> true

cfp:~ > cat a.rb
require ‘rubygems’
require ‘open-uri’
require ‘threadify’

uris = %w( http://google.com http://yahoo.com )

results =
%w( http://google.com http://yahoo.com ).threadify do |uri|
open(uri){|fd| fd.read}
end

puts results.map{|result| result[0, 42]}

cfp:~ > ruby a.rb

http://codeforpeople.com/

matu wrote:

Toki T. wrote:

I’ve tested the code above, but it seems that it doesn’t work like I
thought because on the web server and application server logs I saw only
one request when I run the code (of course with the correct address),
maybe my statement isn’t clear: I need to call a URL multiple time, like
if I press F5 on the browser…I’ve found a way using Watir example, but
without it would be better so I can use the script on any platform.

It works if you run it from irb. If you call it from a script just call
Thread.join like Bret sad, or insert a sleep(10000) at the en of the
script.

Ok, this would work well?

require ‘net/http’
require ‘uri’

threads = []
10.times do
threads << Thread.new
{Net::HTTP.get_response(URI.parse(‘http://www.google.com/’))}
end
threads.each {|x| x.join}

Or your original script with the sleep:

require ‘net/http’
require ‘uri’
10.times do
Thread.new
{Net::HTTP.get_response(URI.parse(‘http://www.google.com/’))}
end
sleep(10000)

Toki T. wrote:

Ok, this would work well?

did you try? WFM.

Ara Howard wrote:

cfp:~ > cat a.rb
require ‘rubygems’
require ‘open-uri’
require ‘threadify’

uris = %w( http://google.com http://yahoo.com )

results =
%w( http://google.com http://yahoo.com ).threadify do |uri|
open(uri){|fd| fd.read}
end

puts results.map{|result| result[0, 42]}

cfp:~ > ruby a.rb

http://codeforpeople.com/

Thnaks for the help, but I keep getting the same error, it seems like
threadify isn’t recognized or installed well…

2008/7/25 Toki T. [email protected]:

It seems that I’ve some problem with the gem threadify or rubygems…I i
run the code that you’ve poste I get the error:

ruby threadify.rb

You shouldn’t give the test script the same name as the file you want
to require. Rename “threadify.rb” to something like
“threadify-test.rb” and it should work.

Regards,
Pit

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs