public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
passenger / benchmark / overhead_of_password_checking.rb
100755 82 lines (74 sloc) 1.766 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env ruby
require 'benchmark'
require 'socket'
 
ITERATIONS = 100000
REQUEST = " " * 800
REQUEST_SIZE = REQUEST.size
RESPONSE = " " * 5 * 1024
RESPONSE_SIZE = RESPONSE.size
PASSWORD = "x" * 128
PASSWORD_SIZE = PASSWORD.size
 
def start
  benchmark_with_password_checking
  puts ""
  benchmark_without_password_checking
end
 
def benchmark_with_password_checking
  puts "Benchmarking with password checking..."
  parent, child = UNIXSocket.pair
  pid = fork do
    parent.close
    ITERATIONS.times do
      password = child.read(PASSWORD_SIZE)
      if password == PASSWORD
        child.read(REQUEST_SIZE)
        child.write(RESPONSE)
        # This flush here improves performance significantly!
        child.flush
      else
        child.close
        break
      end
    end
  end
  child.close
  
  result = Benchmark.measure do
    ITERATIONS.times do
      parent.write(PASSWORD)
      parent.write(REQUEST)
      parent.flush
      parent.read(RESPONSE_SIZE)
    end
  end
  parent.close
  Process.waitpid(pid)
  puts "User/system/real time: #{result}"
  printf "%.2f messages per second\n", ITERATIONS / result.real
end
 
def benchmark_without_password_checking
  puts "Benchmarking without password checking..."
  parent, child = UNIXSocket.pair
  pid = fork do
    parent.close
    ITERATIONS.times do
      child.read(REQUEST_SIZE)
      child.write(RESPONSE)
      # This flush here improves performance significantly!
      child.flush
    end
  end
  child.close
  
  result = Benchmark.measure do
    ITERATIONS.times do
      parent.write(REQUEST)
      # We do not call flush. For some reason it degrades performance.
      parent.read(RESPONSE_SIZE)
    end
  end
  parent.close
  Process.waitpid(pid)
  puts "Without password checking:"
  puts "User/system/real time: #{result}"
  printf "%.2f messages per second\n", ITERATIONS / result.real
end
 
start