Problem with multithreading on Ruby On Rail

Hi,

first of all, sorry for my english.

to start, here my config :
Server linux : ubuntu 8.4
Rails : 2.3.4
Ruby : 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux]

I wanted to test the charge capacity of my server, and I saw a problem
with the multithrearding on Ror.

Here the config file on the server
config/environments/development.rb
Code :

config.threadsafe!

Here the ruby file on the server
app/controllers/hello_controller.rb
class HelloController < ApplicationController

    def word
            $startTime = Time.now
            for valeur in 1..10000000
            end
            $stopTime = (Time.now - $startTime)
            render :text => "Hello Word!!!!, id: "+params["id"]+",

temps: "+$stopTime.to_s
end
end

And the script of “loading test” :
helloWord.sh
#!/bin/bash
count=0
while [ $count -lt $1 ]
do
php helloWord.php $count &
count=expr $count + 1
done

And the client script
$url = “http://”.URL_AMV.“/hello/word?id=”.$id;
$timeStart = microtime(TRUE);
echo “end of “.$id.” to “.$timeStart.”\n”;
$retour = file($url);
$timeStop = microtime(TRUE);
echo “end of “.$id.” to “.$timeStop.”\n”;
echo “Total for “.$id.” => “.$totalTime.”\n”;
echo $url.“\n”;
print_r($retour);

The result is :
./helloWord.sh 5

start of 0 to 1285767141.33

start of 1 to 1285767141.332

start of 2 to 1285767141.37

start of 3 to 1285767141.3795

start of 4 to 1285767141.406
end of 0 to 1285767142.2877

Total for 0 => 0.958
http://87.98.167.204:4445/hello/word?id=0
Array
(
[0] => Hello Word!!!, id: 0, temps: 0.760337
)
end of 1 to 1285767143.1712

Total for 1 => 1.839
http://87.98.167.204:4445/hello/word?id=1
Array
(
[0] => Hello Word!!!, id: 1, temps: 0.760326
)
end of 2 to 1285767143.9855

Total for 2 => 2.616
http://87.98.167.204:4445/hello/word?id=2
Array
(
[0] => Hello Word!!!, id: 2, temps: 0.760426
)
end of 3 to 1285767144.8673

Total for 3 => 3.488
http://87.98.167.204:4445/hello/word?id=3
Array
(
[0] => Hello Word!!!, id: 3, temps: 0.760231
)
end of 4 to 1285767145.6874

Total for 4 => 4.281
http://87.98.167.204:4445/hello/word?id=4
Array
(
[0] => Hello Word!!!, id: 4, temps: 0.764974
)

and the log is :
Processing HelloController#word (for 80.13.147.95 at 2010-09-29
15:31:53) [GET]
Parameters: {“id”=>“0”}
Completed in 763ms (View: 0, DB: 1) | 200 OK [http://87.98.167.204/
hello/word?id=0]
SQL (0.2ms) SET client_min_messages TO ‘panic’
SQL (0.1ms) SET client_min_messages TO ‘notice’

Processing HelloController#word (for 80.13.147.95 at 2010-09-29
15:31:54) [GET]
Parameters: {“id”=>“1”}
Completed in 763ms (View: 0, DB: 1) | 200 OK [http://87.98.167.204/
hello/word?id=1]
SQL (0.2ms) SET client_min_messages TO ‘panic’
SQL (0.1ms) SET client_min_messages TO ‘notice’

Processing HelloController#word (for 80.13.147.95 at 2010-09-29
15:31:55) [GET]
Parameters: {“id”=>“2”}
Completed in 763ms (View: 0, DB: 1) | 200 OK [http://87.98.167.204/
hello/word?id=2]
SQL (0.2ms) SET client_min_messages TO ‘panic’
SQL (0.1ms) SET client_min_messages TO ‘notice’

Processing HelloController#word (for 80.13.147.95 at 2010-09-29
15:31:56) [GET]
Parameters: {“id”=>“3”}
Completed in 763ms (View: 0, DB: 1) | 200 OK [http://87.98.167.204/
hello/word?id=3]
SQL (0.2ms) SET client_min_messages TO ‘panic’
SQL (0.1ms) SET client_min_messages TO ‘notice’

Processing HelloController#word (for 80.13.147.95 at 2010-09-29
15:31:57) [GET]
Parameters: {“id”=>“4”}
Completed in 767ms (View: 0, DB: 1) | 200 OK [http://87.98.167.204/
hello/word?id=4]

what are we seeing?

  • 5 process run together
  • Ruby take the same time for each process (760 ms)
  • BUT : RoR wait the end of each process before answer to client.

finally :
I think, there is a probleme somewhere. but WHERE?? could you help me
please?

regards,
Yann (geek)

On Sep 30, 9:06 am, Yann G. [email protected] wrote:

what are we seeing?

  • 5 process run together
  • Ruby take the same time for each process (760 ms)
  • BUT : RoR wait the end of each process before answer to client.

finally :
I think, there is a probleme somewhere. but WHERE?? could you help me
please?

Rails runs single-threadedly by default. config.threadsafe! (in one of
your environment configuration file) will enable more than one request
to be processed at a time although limitations of MRI’s threadeding
mean you might not see the difference you expect (and i think earlier
versions of mongrel only even dispatch one request to rails at a time,
because rails used to be non threadsafe)

Fred

Frederick C. wrote:

On Sep 30, 9:06�am, Yann G. [email protected] wrote:

what are we seeing?

  • 5 process run together
  • Ruby take the same time for each process (760 ms)
  • BUT : RoR wait the end of each process before answer to client.

finally :
I think, there is a probleme somewhere. but WHERE?? could you help me
please?

Rails runs single-threadedly by default. config.threadsafe! (in one of
your environment configuration file) will enable more than one request
to be processed at a time although limitations of MRI’s threadeding
mean you might not see the difference you expect (and i think earlier
versions of mongrel only even dispatch one request to rails at a time,
because rails used to be non threadsafe)

Fred

thank you.

I comment this line :
config.cache_classes = false

In the config, And I see it work better.
But, could you answer to this question :

When only one process run, the client wait 0.907 sec.
When 2 process run together, each client wait 1.705 sec.
When 5 process run together, each client wait 4.073 sec.

Whereas ruby take 0.760 sec each time, for all process in all
configuration.

Why?

Frederick C. wrote:

On Sep 30, 9:44�am, Yann G. [email protected] wrote:

I comment this line :
config.cache_classes = false

In the config, And I see it work better.

You definitely want that - class reloading is non threadsafe.

But, could you answer to this question :

When only one process run, the client wait �0.907 sec.
When 2 process run together, each client wait 1.705 sec.
When 5 process run together, each client wait 4.073 sec.

I checked mongrel’s source code - it’s still got that mutex inside the
rails dispatcher ( a relic really from when that was still needed) so
it will never dispatch more than one request to rails at a time.

Fred

I was told, start server with this commande :
server/script : is BAD!!

to perform my serveur, its better to use : unicorn/passenger/thin or
mongrel.

I let’s go to try this…

What to you think about it?

On Sep 30, 9:44 am, Yann G. [email protected] wrote:

I comment this line :
config.cache_classes = false

In the config, And I see it work better.

You definitely want that - class reloading is non threadsafe.

But, could you answer to this question :

When only one process run, the client wait 0.907 sec.
When 2 process run together, each client wait 1.705 sec.
When 5 process run together, each client wait 4.073 sec.

I checked mongrel’s source code - it’s still got that mutex inside the
rails dispatcher ( a relic really from when that was still needed) so
it will never dispatch more than one request to rails at a time.

Fred