public
Description: An introduction to distributed computation with load balancers, proxies, and locality strategies.
Homepage:
Clone URL: git://github.com/nkallen/gogaruco.git
gogaruco / jb
100755 44 lines (38 sloc) 1.112 kb
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
#!/usr/bin/env ruby
 
begin
  require 'benchmark'
  require 'rubygems'
  require 'activesupport'
  require 'socket'
  require 'optparse'
  require 'ostruct'
end
 
begin
  $options = {
    :concurrency => 1,
    :count => 100,
    :host => "0.0.0.0"
  }
  OptionParser.new do |opts|
    opts.on('-n', "--number COUNT", Integer) { |count| $options[:count] = count }
    opts.on('-c', "--concurrency CONCURRENCY", Integer) { |concurrency| $options[:concurrency] = concurrency }
    opts.on('-p', "--port PORT", Integer) { |port| $options[:port] = port }
  end.parse!
end
 
count_per_worker = $options[:count] / $options[:concurrency]
 
requests = (1..10).to_a
 
benchmark = Benchmark.measure do
  threads = []
  $options[:concurrency].times do
    threads << Thread.new do
      socket = TCPSocket.new($options[:host], $options[:port])
      count_per_worker.times do
        socket.print("#{requests.rand}\n")
        socket.readline
      end
    end
  end
  threads.each(&:join)
end
 
mean = $options[:count] / benchmark.real
puts "Requests per second (throughput):\t%2f [#/sec] (mean)" % mean