Merge pull request #1698 from bf4/fix/benchmark

Fix CachingPostSerializer defining associations twice; add FragmentCaching benchmaking
This commit is contained in:
Benjamin Fleischer 2016-04-21 23:44:54 -05:00
commit 17d0759c97
3 changed files with 71 additions and 6 deletions

View File

@ -31,6 +31,10 @@ class ApiAssertion
get("/caching/#{on_off}") get("/caching/#{on_off}")
end end
def get_fragment_caching(on_off = 'on'.freeze)
get("/fragment_caching/#{on_off}")
end
def get_non_caching(on_off = 'on'.freeze) def get_non_caching(on_off = 'on'.freeze)
get("/non_caching/#{on_off}") get("/non_caching/#{on_off}")
end end
@ -101,8 +105,10 @@ assertion.debug { assertion.get_status }
time = 10 time = 10
{ {
'caching on: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'on'] }, '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 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: 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'] } 'caching off: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'off'] }
}.each do |label, options| }.each do |label, options|
assertion.clear assertion.clear

View File

@ -1,12 +1,13 @@
class PostController < ActionController::Base class PostController < ActionController::Base
POST = POST =
begin begin
updated_at = Time.current
if ENV['BENCH_STRESS'] if ENV['BENCH_STRESS']
comments = (0..50).map do |i| 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 end
else else
comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT')] comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT', updated_at: updated_at)]
end end
author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura') 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) Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
@ -17,6 +18,11 @@ class PostController < ActionController::Base
render json: POST, serializer: CachingPostSerializer, adapter: :json, meta: { caching: perform_caching } render json: POST, serializer: CachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
end 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 def render_with_non_caching_serializer
toggle_cache_status toggle_cache_status
render json: POST, adapter: :json, meta: { caching: perform_caching } render json: POST, adapter: :json, meta: { caching: perform_caching }
@ -73,5 +79,6 @@ Rails.application.routes.draw do
get '/status(/:on)' => 'post#render_cache_status' get '/status(/:on)' => 'post#render_cache_status'
get '/clear' => 'post#clear' get '/clear' => 'post#clear'
get '/caching(/:on)' => 'post#render_with_caching_serializer' 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' get '/non_caching(/:on)' => 'post#render_with_non_caching_serializer'
end end

View File

@ -13,7 +13,7 @@ end
Rails.configuration.serializers << BlogSerializer Rails.configuration.serializers << BlogSerializer
class CommentSerializer < ActiveModel::Serializer class CommentSerializer < ActiveModel::Serializer
attributes :id, :body attributes :id, :body, :updated_at
belongs_to :post belongs_to :post
belongs_to :author belongs_to :author
@ -43,7 +43,7 @@ end
Rails.configuration.serializers << PostSerializer Rails.configuration.serializers << PostSerializer
class CachingAuthorSerializer < AuthorSerializer class CachingAuthorSerializer < AuthorSerializer
cache key: 'writer', only: [:first_name, :last_name], skip_digest: true cache key: 'writer', skip_digest: true
end end
Rails.configuration.serializers << CachingAuthorSerializer Rails.configuration.serializers << CachingAuthorSerializer
@ -52,14 +52,66 @@ class CachingCommentSerializer < CommentSerializer
end end
Rails.configuration.serializers << CachingCommentSerializer 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 cache key: 'post', expires_in: 0.1, skip_digest: true
attributes :id, :title, :body
belongs_to :blog, serializer: BlogSerializer belongs_to :blog, serializer: BlogSerializer
belongs_to :author, serializer: CachingAuthorSerializer belongs_to :author, serializer: CachingAuthorSerializer
has_many :comments, serializer: CachingCommentSerializer 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 end
Rails.configuration.serializers << CachingPostSerializer 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' if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
require 'active_record' require 'active_record'
@ -150,7 +202,7 @@ else
end end
class Comment < BenchmarkModel class Comment < BenchmarkModel
attr_accessor :id, :body attr_accessor :id, :body, :updated_at
end end
class Author < BenchmarkModel class Author < BenchmarkModel