# Testing Threads with connections

**URL:** <https://www.ruby-forum.com/t/testing-threads-with-connections/67151>\
**Category:** Ruby\
**Created:** [September 1, 2006, 2:15am UTC](https://www.ruby-forum.com/t/testing-threads-with-connections/67151 "2006-09-01T02:15:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![denao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8c91f0/32.png) [@denao](https://www.ruby-forum.com/u/denao)\
**Post date:** [September 1, 2006, 2:15am UTC](https://www.ruby-forum.com/t/testing-threads-with-connections/67151/1 "2006-09-01T02:15:34Z")

</div>

Hello there,

I’ve been trying to make a stress test generator in ruby for a New IO  
echo server initially.

Mainly, the idea is try to generate as many connections as possible to  
the echo server, and from time to time, send a string to the server and  
receive it back.

I don’t really have much experience in threads programming, and that’s  
the main reason to try it out.

For this script, I thought to open threads, and in each thread, do a  
loop and open connections for each thread. I tried to use 100  
threads and in each thread, open 100 connections. It runs fine, no  
errors reported to me at all, but the echo server shows only ~1200  
connections.

I’ve set up the OS settings with ulimit already (open files).

What do you guys think ? Any suggestions?

Here it goes:

– code

#!/usr/bin/ruby  
require ‘socket’

if ARGV.length \< 3  
puts "Use: $0 "  
exit 1  
else  
thrds=ARGV[0].to\_i  
conec=ARGV[1].to\_i  
tmout=ARGV[2].to\_i  
end

DEBUG=1  
teststring=“denao.”  
host=“192.168.0.100”  
port=50000  
threads=[]

for num in (1 … thrds)  
threads \<\< Thread.new(num) { |loT|  
printf “Thread (%.0f) opening %.0f connections\n”, loT.to\_s,  
conec.to\_s  
sock = []  
conec.times do |i|  
if DEBUG==1  
printf "%.0f) Opening socket %.0f… ",loT.to\_s,i  
end  
sock[i] = TCPsocket::open(host,port)  
if DEBUG==1  
print “done\n”  
end  
end

```
  while (1)
     conec.times do |j|
        if DEBUG==1
           printf "%.0f) Sending string on socket 

```

%.0f…",loT.to\_s,j  
end

```
        sock[j].send teststring, 128

        if DEBUG==1
           printf "done.\n%.0f) Waiting answer on socket 

```

%.0f…",loT.to\_s,j  
end

```
        tmp=sock[j].recv(128)

        if DEBUG==1
          if teststring == tmp
              print "matched!\n"
          else
              print "error!\n"
          end
        end

        if DEBUG==2
           printf "%.0f) Sleeping %.0fs\n",loT.to_s,tmout.to_s
        end
        sleep(tmout)
     end
  end
}

```

end

threads.each { |aThread| aThread.join }

– /code

---

<div class="post-metadata">

**Author:** ![denao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8c91f0/32.png) [@denao](https://www.ruby-forum.com/u/denao)\
**Post date:** [September 1, 2006, 11:39am UTC](https://www.ruby-forum.com/t/testing-threads-with-connections/67151/2 "2006-09-01T11:39:20Z")

</div>

On 01.09.2006 02:16, Denis Vieira wrote:

> the main reason to try it out.
> 
> For this script, I thought to open threads, and in each thread, do a  
> loop and open connections for each thread. I tried to use 100  
> threads and in each thread, open 100 connections. It runs fine, no  
> errors reported to me at all, but the echo server shows only ~1200  
> connections.
> 
> I’ve set up the OS settings with ulimit already (open files).
> 
> What do you guys think ? Any suggestions?

Insert this at the top of your script to see whether there are  
exceptions during connection creation:

Thread.abort\_on\_exception = true

Alternatively, wrap each thread’s main processing body in a begin rescue  
end to print out exceptions. (Thinking about it I’d prefer the second  
approach anyway as it makes up for more robust software.) HTH

Kind regards

```
robert
```
