Skip to content

Commit 17d0759

Browse files
committed
Merge pull request #1698 from bf4/fix/benchmark
Fix CachingPostSerializer defining associations twice; add FragmentCaching benchmaking
2 parents b4e2ac3 + 335869e commit 17d0759

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
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: 56 additions & 4 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

@@ -52,14 +52,66 @@ class CachingCommentSerializer < CommentSerializer
5252
end
5353
Rails.configuration.serializers << CachingCommentSerializer
5454

55-
class CachingPostSerializer < PostSerializer
55+
# see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
56+
class CachingPostSerializer < ActiveModel::Serializer
5657
cache key: 'post', expires_in: 0.1, skip_digest: true
58+
59+
attributes :id, :title, :body
60+
5761
belongs_to :blog, serializer: BlogSerializer
5862
belongs_to :author, serializer: CachingAuthorSerializer
5963
has_many :comments, serializer: CachingCommentSerializer
64+
65+
link(:post_authors) { 'https://example.com/post_authors' }
66+
67+
meta do
68+
{
69+
rating: 5,
70+
favorite_count: 10
71+
}
72+
end
73+
74+
def blog
75+
Blog.new(id: 999, name: 'Custom blog')
76+
end
6077
end
6178
Rails.configuration.serializers << CachingPostSerializer
6279

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+
63115
if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
64116
require 'active_record'
65117

@@ -150,7 +202,7 @@ def read_attribute_for_serialization(key)
150202
end
151203

152204
class Comment < BenchmarkModel
153-
attr_accessor :id, :body
205+
attr_accessor :id, :body, :updated_at
154206
end
155207

156208
class Author < BenchmarkModel

0 commit comments

Comments
 (0)