Skip to content

Commit 335869e

Browse files
committed
Add FragmentCaching benchmark
1 parent 02ad8c2 commit 335869e

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

test/benchmark/bm_caching.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def get_caching(on_off = 'on'.freeze)
3131
get("/caching/#{on_off}")
3232
end
3333

34+
def get_fragment_caching(on_off = 'on'.freeze)
35+
get("/fragment_caching/#{on_off}")
36+
end
37+
3438
def get_non_caching(on_off = 'on'.freeze)
3539
get("/non_caching/#{on_off}")
3640
end
@@ -101,8 +105,10 @@ def assert_equal(expected, actual, message)
101105
time = 10
102106
{
103107
'caching on: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'on'] },
108+
'caching on: fragment caching serializers: gc off' => { disable_gc: true, send: [:get_fragment_caching, 'on'] },
104109
'caching on: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'on'] },
105110
'caching off: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'off'] },
111+
'caching off: fragment caching serializers: gc off' => { disable_gc: true, send: [:get_fragment_caching, 'off'] },
106112
'caching off: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'off'] }
107113
}.each do |label, options|
108114
assertion.clear

test/benchmark/controllers.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class PostController < ActionController::Base
22
POST =
33
begin
4+
updated_at = Time.current
45
if ENV['BENCH_STRESS']
56
comments = (0..50).map do |i|
6-
Comment.new(id: i, body: 'ZOMG A COMMENT')
7+
Comment.new(id: i, body: 'ZOMG A COMMENT', updated_at: updated_at + i)
78
end
89
else
9-
comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT')]
10+
comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT', updated_at: updated_at)]
1011
end
1112
author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura')
1213
Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
@@ -17,6 +18,11 @@ def render_with_caching_serializer
1718
render json: POST, serializer: CachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
1819
end
1920

21+
def render_with_fragment_caching_serializer
22+
toggle_cache_status
23+
render json: POST, serializer: FragmentCachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
24+
end
25+
2026
def render_with_non_caching_serializer
2127
toggle_cache_status
2228
render json: POST, adapter: :json, meta: { caching: perform_caching }
@@ -73,5 +79,6 @@ def toggle_cache_status
7379
get '/status(/:on)' => 'post#render_cache_status'
7480
get '/clear' => 'post#clear'
7581
get '/caching(/:on)' => 'post#render_with_caching_serializer'
82+
get '/fragment_caching(/:on)' => 'post#render_with_fragment_caching_serializer'
7683
get '/non_caching(/:on)' => 'post#render_with_non_caching_serializer'
7784
end

test/benchmark/fixtures.rb

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class BlogSerializer < ActiveModel::Serializer
1313
Rails.configuration.serializers << BlogSerializer
1414

1515
class CommentSerializer < ActiveModel::Serializer
16-
attributes :id, :body
16+
attributes :id, :body, :updated_at
1717

1818
belongs_to :post
1919
belongs_to :author
@@ -43,7 +43,7 @@ def blog
4343
Rails.configuration.serializers << PostSerializer
4444

4545
class CachingAuthorSerializer < AuthorSerializer
46-
cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
46+
cache key: 'writer', skip_digest: true
4747
end
4848
Rails.configuration.serializers << CachingAuthorSerializer
4949

@@ -58,9 +58,9 @@ class CachingPostSerializer < ActiveModel::Serializer
5858

5959
attributes :id, :title, :body
6060

61-
has_many :comments, serializer: CommentSerializer
6261
belongs_to :blog, serializer: BlogSerializer
63-
belongs_to :author, serializer: AuthorSerializer
62+
belongs_to :author, serializer: CachingAuthorSerializer
63+
has_many :comments, serializer: CachingCommentSerializer
6464

6565
link(:post_authors) { 'https://example.com/post_authors' }
6666

@@ -77,6 +77,41 @@ def blog
7777
end
7878
Rails.configuration.serializers << CachingPostSerializer
7979

80+
class FragmentCachingAuthorSerializer < AuthorSerializer
81+
cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
82+
end
83+
Rails.configuration.serializers << FragmentCachingAuthorSerializer
84+
85+
class FragmentCachingCommentSerializer < CommentSerializer
86+
cache expires_in: 1.day, except: [:updated_at], skip_digest: true
87+
end
88+
Rails.configuration.serializers << CachingCommentSerializer
89+
90+
# see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
91+
class FragmentCachingPostSerializer < ActiveModel::Serializer
92+
cache key: 'post', expires_in: 0.1, skip_digest: true
93+
94+
attributes :id, :title, :body
95+
96+
belongs_to :blog, serializer: BlogSerializer
97+
belongs_to :author, serializer: FragmentCachingAuthorSerializer
98+
has_many :comments, serializer: FragmentCachingCommentSerializer
99+
100+
link(:post_authors) { 'https://example.com/post_authors' }
101+
102+
meta do
103+
{
104+
rating: 5,
105+
favorite_count: 10
106+
}
107+
end
108+
109+
def blog
110+
Blog.new(id: 999, name: 'Custom blog')
111+
end
112+
end
113+
Rails.configuration.serializers << FragmentCachingPostSerializer
114+
80115
if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
81116
require 'active_record'
82117

@@ -167,7 +202,7 @@ def read_attribute_for_serialization(key)
167202
end
168203

169204
class Comment < BenchmarkModel
170-
attr_accessor :id, :body
205+
attr_accessor :id, :body, :updated_at
171206
end
172207

173208
class Author < BenchmarkModel

0 commit comments

Comments
 (0)