Skip to content

Fix CachingPostSerializer defining associations twice; add FragmentCaching benchmaking #1698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/benchmark/bm_caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def get_caching(on_off = 'on'.freeze)
get("/caching/#{on_off}")
end

def get_fragment_caching(on_off = 'on'.freeze)
get("/fragment_caching/#{on_off}")
end

def get_non_caching(on_off = 'on'.freeze)
get("/non_caching/#{on_off}")
end
Expand Down Expand Up @@ -101,8 +105,10 @@ def assert_equal(expected, actual, message)
time = 10
{
'caching on: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'on'] },
'caching on: fragment caching serializers: gc off' => { disable_gc: true, send: [:get_fragment_caching, 'on'] },
'caching on: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'on'] },
'caching off: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'off'] },
'caching off: fragment caching serializers: gc off' => { disable_gc: true, send: [:get_fragment_caching, 'off'] },
'caching off: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'off'] }
}.each do |label, options|
assertion.clear
Expand Down
11 changes: 9 additions & 2 deletions test/benchmark/controllers.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
class PostController < ActionController::Base
POST =
begin
updated_at = Time.current
if ENV['BENCH_STRESS']
comments = (0..50).map do |i|
Comment.new(id: i, body: 'ZOMG A COMMENT')
Comment.new(id: i, body: 'ZOMG A COMMENT', updated_at: updated_at + i)
end
else
comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT')]
comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT', updated_at: updated_at)]
end
author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura')
Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
Expand All @@ -17,6 +18,11 @@ def render_with_caching_serializer
render json: POST, serializer: CachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
end

def render_with_fragment_caching_serializer
toggle_cache_status
render json: POST, serializer: FragmentCachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
end

def render_with_non_caching_serializer
toggle_cache_status
render json: POST, adapter: :json, meta: { caching: perform_caching }
Expand Down Expand Up @@ -73,5 +79,6 @@ def toggle_cache_status
get '/status(/:on)' => 'post#render_cache_status'
get '/clear' => 'post#clear'
get '/caching(/:on)' => 'post#render_with_caching_serializer'
get '/fragment_caching(/:on)' => 'post#render_with_fragment_caching_serializer'
get '/non_caching(/:on)' => 'post#render_with_non_caching_serializer'
end
60 changes: 56 additions & 4 deletions test/benchmark/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BlogSerializer < ActiveModel::Serializer
Rails.configuration.serializers << BlogSerializer

class CommentSerializer < ActiveModel::Serializer
attributes :id, :body
attributes :id, :body, :updated_at

belongs_to :post
belongs_to :author
Expand Down Expand Up @@ -43,7 +43,7 @@ def blog
Rails.configuration.serializers << PostSerializer

class CachingAuthorSerializer < AuthorSerializer
cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
cache key: 'writer', skip_digest: true
end
Rails.configuration.serializers << CachingAuthorSerializer

Expand All @@ -52,14 +52,66 @@ class CachingCommentSerializer < CommentSerializer
end
Rails.configuration.serializers << CachingCommentSerializer

class CachingPostSerializer < PostSerializer
# see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
class CachingPostSerializer < ActiveModel::Serializer
cache key: 'post', expires_in: 0.1, skip_digest: true

attributes :id, :title, :body

belongs_to :blog, serializer: BlogSerializer
belongs_to :author, serializer: CachingAuthorSerializer
has_many :comments, serializer: CachingCommentSerializer

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

meta do
{
rating: 5,
favorite_count: 10
}
end

def blog
Blog.new(id: 999, name: 'Custom blog')
end
end
Rails.configuration.serializers << CachingPostSerializer

class FragmentCachingAuthorSerializer < AuthorSerializer
cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
end
Rails.configuration.serializers << FragmentCachingAuthorSerializer

class FragmentCachingCommentSerializer < CommentSerializer
cache expires_in: 1.day, except: [:updated_at], skip_digest: true
end
Rails.configuration.serializers << CachingCommentSerializer

# see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
class FragmentCachingPostSerializer < ActiveModel::Serializer
cache key: 'post', expires_in: 0.1, skip_digest: true

attributes :id, :title, :body

belongs_to :blog, serializer: BlogSerializer
belongs_to :author, serializer: FragmentCachingAuthorSerializer
has_many :comments, serializer: FragmentCachingCommentSerializer

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

meta do
{
rating: 5,
favorite_count: 10
}
end

def blog
Blog.new(id: 999, name: 'Custom blog')
end
end
Rails.configuration.serializers << FragmentCachingPostSerializer

if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
require 'active_record'

Expand Down Expand Up @@ -150,7 +202,7 @@ def read_attribute_for_serialization(key)
end

class Comment < BenchmarkModel
attr_accessor :id, :body
attr_accessor :id, :body, :updated_at
end

class Author < BenchmarkModel
Expand Down