gist: 10203 Download_button fork
public
Description:
Benchmarks for CachedModels
Public Clone URL: git://gist.github.com/10203.git
controllers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
class AuthorsController < ApplicationController
  # GET /authors/1
  def show
    @author = Author.find(params[:id])
  end
end
 
class CachedAuthorsController < ApplicationController
  # GET /cached_authors/1
  def show
    @cached_author = CachedAuthor.find(params[:id])
  end
end
envirnonment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MacBook 2GHz Intel Core 2 Duo / 1Gb 667 MHz DDR2 SDRAM / Mac OS X 10.5.4
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
rails 2.1.1
ruby-prof 0.6.0
memcached 1.2.6
apache-bench Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
mysql Ver 14.14 Distrib 5.1.28-rc, for apple-darwin9.4.0 (i686) using readline 5.1
mysql (gem) 2.7
Apache/2.2.8 (Unix)
passenger 2.0.3
cached_models 0.0.3 (installed as gemified plugin)
 
config/environments/benchmarking.rb
config.cache_classes = true
config.action_controller.perform_caching = true
config.cache_store = :mem_cache_store
 
 
fixtures.yml
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
# Only 1 author, with 10 posts. Each post has 10 comments and 10 tags.
 
# authors.yml
luca:
  id: 1
  first_name: Luca
  last_name: Guidi
 
# posts.yml
one:
  id: 1
  author_id: 1
  title: Lorem ipsum dolor sit amet
  text: Lorem ipsum dolor sit amet # ... 4094 chars
 
# comments.yml
one_for_one:
  post_id: 1
  author: John Doe
  email: john.doe@email.com
  text: Lorem ipsum dolor sit amet # ... 893 chars
 
# tags.yml
ruby_for_one:
  taggable_id: 1
  taggable_type: Post
  name: ruby
 
 
models.rb
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
# WITHOUT cached_models
class Author < ActiveRecord::Base
  has_many :posts
  has_many :comments, :through => :posts
  
  def full_name
    @full_name ||= [ first_name, last_name ].join(" ")
  end
end
 
class Post < ActiveRecord::Base
  belongs_to :author
  has_many :comments
  has_many :tags, :as => :taggable
end
 
class Comment < ActiveRecord::Base
  belongs_to :post
end
 
class Tag < ActiveRecord::Base
  belongs_to :taggable, :polymorphic => true
end
 
# WITH cached_models
class CachedAuthor < ActiveRecord::Base
  has_many :cached_posts, :cached => true
  has_many :cached_comments, :through => :cached_posts, :cached => true
  
  def full_name
    @full_name ||= [ first_name, last_name ].join(" ")
  end
end
 
class CachedPost < ActiveRecord::Base
  belongs_to :cached_author, :cached => true
  has_many :cached_comments, :cached => true
  has_many :cached_tags, :as => :taggable, :cached => true
end
 
class CachedComment < ActiveRecord::Base
  belongs_to :cached_post, :cached => true
end
 
class CachedTag < ActiveRecord::Base
  belongs_to :taggable, :polymorphic => true, :cached => true
end
 
 
results with apache-bench
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
$ ab -n 1000 -c 50 http://cache-bench.local/authors/1
 
Benchmarking cache-bench.local (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 1000 requests
 
 
Server Software: Apache/2.2.8
Server Hostname: cache-bench.local
Server Port: 80
 
Document Path: /authors/1
Document Length: 165716 bytes
 
Concurrency Level: 50
Time taken for tests: 109.967512 seconds
Complete requests: 1000
Failed requests: 6
   (Connect: 0, Length: 6, Exceptions: 0)
Write errors: 0
Total transferred: 165324490 bytes
HTML transferred: 164721704 bytes
Requests per second: 9.09 [#/sec] (mean)
Time per request: 5498.375 [ms] (mean)
Time per request: 109.968 [ms] (mean, across all concurrent requests)
Transfer rate: 1468.15 [Kbytes/sec] received
 
Connection Times (ms)
              min mean[+/-sd] median max
Connect: 0 0 1.0 0 19
Processing: 1 5416 2881.9 5481 18235
Waiting: 1 5411 2881.0 5475 18231
Total: 1 5417 2882.0 5481 18235
 
Percentage of the requests served within a certain time (ms)
  50% 5481
  66% 5795
  75% 5973
  80% 6081
  90% 7725
  95% 10994
  98% 15394
  99% 16008
 100% 18235 (longest request)
 
 
 
$ ab -n 1000 -c 50 http://cache-bench.local/cached_authors/1
 
Benchmarking cache-bench.local (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 1000 requests
 
 
Server Software: Apache/2.2.8
Server Hostname: cache-bench.local
Server Port: 80
 
Document Path: /cached_authors/1
Document Length: 165736 bytes
 
Concurrency Level: 50
Time taken for tests: 73.95238 seconds
Complete requests: 1000
Failed requests: 20
   (Connect: 0, Length: 20, Exceptions: 0)
Write errors: 0
Total transferred: 163018900 bytes
HTML transferred: 162421280 bytes
Requests per second: 13.68 [#/sec] (mean)
Time per request: 3654.762 [ms] (mean)
Time per request: 73.095 [ms] (mean, across all concurrent requests)
Transfer rate: 2177.95 [Kbytes/sec] received
 
Connection Times (ms)
              min mean[+/-sd] median max
Connect: 0 0 1.4 0 21
Processing: 1 3605 2971.0 2471 15037
Waiting: 0 3586 2970.8 2465 15015
Total: 1 3605 2971.4 2471 15039
 
Percentage of the requests served within a certain time (ms)
  50% 2471
  66% 4319
  75% 4989
  80% 5418
  90% 6836
  95% 11360
  98% 13164
  99% 14043
 100% 15039 (longest request)
 
 
results with browser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Look at the reqs/sec, DB elapsed time, DB queries and Cache accesses.
 
# without cache
## first request
Completed in 1.71418 (0 reqs/sec) | Rendering: 0.52964 (30%) | DB: 1.17224 (68%) | 200 OK [http://cache-bench.local/authors/1] | DB queries: 28 | cache: 0
 
## second request
Completed in 0.07203 (13 reqs/sec) | Rendering: 0.03475 (48%) | DB: 0.03642 (50%) | 200 OK [http://cache-bench.local/authors/1] | DB queries: 22 | cache: 0
 
 
 
# with cache
## first request (empty cache)
Completed in 0.38890 (2 reqs/sec) | Rendering: 0.33083 (85%) | DB: 0.04601 (11%) | 200 OK [http://cache-bench.local/cached_authors/1] | DB queries: 28 | cache: 21 (write: 21)
 
## second request (primed cache)
Completed in 0.05103 (19 reqs/sec) | Rendering: 0.04388 (85%) | DB: 0.00602 (11%) | 200 OK [http://cache-bench.local/cached_authors/1] | DB queries: 1 | cache: 21 (read: 21)
 
show.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<h1><%= @author.full_name -%></h1>
 
<h2>Posts</h2>
<% for post in @author.posts -%>
<h3><%= post.title -%></h3>
<p><%= post.text -%></p>
<h4>Tags</h4>
<p><%= post.tags.map(&:name).join(", ") %></p>
 
<h4>Comments</h4>
<div class="comments">
  <% for comment in post.comments -%>
    <h5>by <%= mail_to comment.email, comment.author, :encode => "javascript" -%></h5>
    <p><%= comment.text -%></p>
  <% end -%>
</div>
<% end -%>
 
<%= link_to 'Edit', edit_author_path(@author) %> |
<%= link_to 'Back', authors_path %>
 
 

Owner

jodosha

Revisions