Skip to content

Instantly share code, notes, and snippets.

@deepak
Created August 30, 2010 08:37
Show Gist options
  • Save deepak/557179 to your computer and use it in GitHub Desktop.
Save deepak/557179 to your computer and use it in GitHub Desktop.
To benchmark ActiveRecord connection-pool size
class TestController < ApplicationController
# To benchmark ActiveRecord connection-pool size
# connection pool 5
def ping
t1 = Time.now
1.upto(10) { Product.find(Integer(rand*1000)) }
time_taken = (Time.now - t1)
render :text => "time taken: #{time_taken.inspect}"
end
def ping_multi
t1 = Time.now
foo = []
1.upto(10) do
foo << Thread.new { Product.find(Integer(rand*1000)) }
end
foo.each {|x| x.join }
time_taken = (Time.now - t1)
render :text => "time taken: #{time_taken.inspect}"
end
end
__END__
== Product model
#
# Table name: products
#
# id :integer(4) not null, primary key
# name :string(255)
# created_at :datetime
# updated_at :datetime
#
class Product < ActiveRecord::Base
end
== connection pool 5
> ab -c 2 -n 20 "http://127.0.0.1:3000/test/ping"
Server Software:
Server Hostname: 127.0.0.1
Server Port: 3000
Document Path: /test/ping
Document Length: 20 bytes
Concurrency Level: 2
Time taken for tests: 0.129 seconds
Complete requests: 20
Failed requests: 1
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Write errors: 0
Total transferred: 5259 bytes
HTML transferred: 399 bytes
Requests per second: 155.40 [#/sec] (mean)
Time per request: 12.870 [ms] (mean)
Time per request: 6.435 [ms] (mean, across all concurrent requests)
Transfer rate: 39.91 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 6 13 4.9 11 28
Waiting: 6 13 4.9 11 28
Total: 6 13 4.9 11 28
Percentage of the requests served within a certain time (ms)
50% 11
66% 12
75% 13
80% 18
90% 20
95% 28
98% 28
99% 28
100% 28 (longest request)
> ab -c 2 -n 20 "http://127.0.0.1:3000/test/ping_multi"
Server Software:
Server Hostname: 127.0.0.1
Server Port: 3000
Document Path: /test/ping_multi
Document Length: 12609 bytes
Concurrency Level: 2
Time taken for tests: 136.274 seconds
Complete requests: 20
Failed requests: 0
Write errors: 0
Non-2xx responses: 20
Total transferred: 255820 bytes
HTML transferred: 252180 bytes
Requests per second: 0.15 [#/sec] (mean)
Time per request: 13627.373 [ms] (mean)
Time per request: 6813.686 [ms] (mean, across all concurrent requests)
Transfer rate: 1.83 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 5068 13375 4677.6 10153 20165
Waiting: 5068 13375 4677.6 10153 20165
Total: 5068 13375 4677.6 10153 20165
Percentage of the requests served within a certain time (ms)
50% 10153
66% 15162
75% 20112
80% 20116
90% 20165
95% 20165
98% 20165
99% 20165
100% 20165 (longest request)
# there were a lot of ActiveRecord timeouts like:
Processing TestController#ping_multi (for 127.0.0.1 at 2010-08-30 18:40:30) [GET]
ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5 seconds. The max pool size is currently 5; consider increasing it.):
app/controllers/test_controller.rb:17:in `join'
app/controllers/test_controller.rb:17:in `ping_multi'
app/controllers/test_controller.rb:17:in `each'
app/controllers/test_controller.rb:17:in `ping_multi'
Rendering rescues/layout (internal_server_error)
== connection pool 10
ab -c 2 -n 20 "http://127.0.0.1:3000/test/ping"
Server Software:
Server Hostname: 127.0.0.1
Server Port: 3000
Document Path: /test/ping
Document Length: 20 bytes
Concurrency Level: 2
Time taken for tests: 0.127 seconds
Complete requests: 20
Failed requests: 2
(Connect: 0, Receive: 0, Length: 2, Exceptions: 0)
Write errors: 0
Total transferred: 5257 bytes
HTML transferred: 397 bytes
Requests per second: 157.07 [#/sec] (mean)
Time per request: 12.733 [ms] (mean)
Time per request: 6.367 [ms] (mean, across all concurrent requests)
Transfer rate: 40.32 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 5 13 3.9 11 22
Waiting: 5 13 3.9 11 22
Total: 5 13 3.9 11 22
Percentage of the requests served within a certain time (ms)
50% 11
66% 12
75% 13
80% 18
90% 19
95% 22
98% 22
99% 22
100% 22 (longest request)
> ab -c 2 -n 20 "http://127.0.0.1:3000/test/ping_multi"
Server Software:
Server Hostname: 127.0.0.1
Server Port: 3000
Document Path: /test/ping_multi
Document Length: 20 bytes
Concurrency Level: 2
Time taken for tests: 105.513 seconds
Complete requests: 20
Failed requests: 6
(Connect: 0, Receive: 0, Length: 6, Exceptions: 0)
Write errors: 0
Non-2xx responses: 2
Total transferred: 30371 bytes
HTML transferred: 25578 bytes
Requests per second: 0.19 [#/sec] (mean)
Time per request: 10551.327 [ms] (mean)
Time per request: 5275.663 [ms] (mean, across all concurrent requests)
Transfer rate: 0.28 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 2
Processing: 5041 10301 1975.2 10037 15081
Waiting: 5039 10300 1975.5 10037 15081
Total: 5042 10301 1975.1 10037 15081
Percentage of the requests served within a certain time (ms)
50% 10037
66% 10040
75% 10085
80% 10112
90% 15047
95% 15081
98% 15081
99% 15081
100% 15081 (longest request)
== connection pool 20
> ab -c 2 -n 20 "http://127.0.0.1:3000/test/ping"
Server Software:
Server Hostname: 127.0.0.1
Server Port: 3000
Document Path: /test/ping
Document Length: 20 bytes
Concurrency Level: 2
Time taken for tests: 0.119 seconds
Complete requests: 20
Failed requests: 3
(Connect: 0, Receive: 0, Length: 3, Exceptions: 0)
Write errors: 0
Total transferred: 5257 bytes
HTML transferred: 397 bytes
Requests per second: 167.45 [#/sec] (mean)
Time per request: 11.944 [ms] (mean)
Time per request: 5.972 [ms] (mean, across all concurrent requests)
Transfer rate: 42.98 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 6 12 4.3 11 27
Waiting: 6 12 4.3 11 27
Total: 6 12 4.3 11 27
Percentage of the requests served within a certain time (ms)
50% 11
66% 11
75% 11
80% 11
90% 20
95% 27
98% 27
99% 27
100% 27 (longest request)
> ab -c 2 -n 20 "http://127.0.0.1:3000/test/ping_multi"
Server Software:
Server Hostname: 127.0.0.1
Server Port: 3000
Document Path: /test/ping_multi
Document Length: 20 bytes
Concurrency Level: 2
Time taken for tests: 50.377 seconds
Complete requests: 20
Failed requests: 1
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Write errors: 0
Total transferred: 5298 bytes
HTML transferred: 399 bytes
Requests per second: 0.40 [#/sec] (mean)
Time per request: 5037.722 [ms] (mean)
Time per request: 2518.861 [ms] (mean, across all concurrent requests)
Transfer rate: 0.10 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 54 4787 1114.3 5030 5121
Waiting: 54 4787 1114.3 5030 5121
Total: 54 4787 1114.3 5030 5121
Percentage of the requests served within a certain time (ms)
50% 5030
66% 5032
75% 5033
80% 5034
90% 5074
95% 5121
98% 5121
99% 5121
100% 5121 (longest request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment