mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1785 from bf4/fix_benchmark_expected_cache_response
Make bencharked model names reflect relationships; fix expected response
This commit is contained in:
commit
b9e7dc5584
@ -66,25 +66,25 @@ class ApiAssertion
|
|||||||
def expected
|
def expected
|
||||||
@expected ||=
|
@expected ||=
|
||||||
{
|
{
|
||||||
'post' => {
|
'primary_resource' => {
|
||||||
'id' => 1337,
|
'id' => 1337,
|
||||||
'title' => 'New Post',
|
'title' => 'New PrimaryResource',
|
||||||
'body' => 'Body',
|
'body' => 'Body',
|
||||||
'comments' => [
|
'virtual_attribute' => {
|
||||||
{
|
'id' => 999,
|
||||||
'id' => 1,
|
'name' => 'Free-Range Virtual Attribute'
|
||||||
'body' => 'ZOMG A COMMENT'
|
|
||||||
}
|
|
||||||
],
|
|
||||||
'blog' => {
|
|
||||||
'id' => 999,
|
|
||||||
'name' => 'Custom blog'
|
|
||||||
},
|
},
|
||||||
'author' => {
|
'has_one_relationship' => {
|
||||||
'id' => 42,
|
'id' => 42,
|
||||||
'first_name' => 'Joao',
|
'first_name' => 'Joao',
|
||||||
'last_name' => 'Moura'
|
'last_name' => 'Moura'
|
||||||
}
|
},
|
||||||
|
'has_many_relationships' => [
|
||||||
|
{
|
||||||
|
'id' => 1,
|
||||||
|
'body' => 'ZOMG A HAS MANY RELATIONSHIP'
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,12 +4,23 @@ require_relative './app'
|
|||||||
time = 10
|
time = 10
|
||||||
disable_gc = true
|
disable_gc = true
|
||||||
ActiveModelSerializers.config.key_transform = :unaltered
|
ActiveModelSerializers.config.key_transform = :unaltered
|
||||||
comments = (0..50).map do |i|
|
has_many_relationships = (0..50).map do |i|
|
||||||
Comment.new(id: i, body: 'ZOMG A COMMENT')
|
HasManyRelationship.new(id: i, body: 'ZOMG A HAS MANY RELATIONSHIP')
|
||||||
end
|
end
|
||||||
author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura')
|
has_one_relationship = HasOneRelationship.new(
|
||||||
post = Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
|
id: 42,
|
||||||
serializer = PostSerializer.new(post)
|
first_name: 'Joao',
|
||||||
|
last_name: 'Moura'
|
||||||
|
)
|
||||||
|
primary_resource = PrimaryResource.new(
|
||||||
|
id: 1337,
|
||||||
|
title: 'New PrimaryResource',
|
||||||
|
virtual_attribute: nil,
|
||||||
|
body: 'Body',
|
||||||
|
has_many_relationships: has_many_relationships,
|
||||||
|
has_one_relationship: has_one_relationship
|
||||||
|
)
|
||||||
|
serializer = PrimaryResourceSerializer.new(primary_resource)
|
||||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
||||||
serialization = adapter.as_json
|
serialization = adapter.as_json
|
||||||
|
|
||||||
|
|||||||
@ -1,31 +1,30 @@
|
|||||||
class PostController < ActionController::Base
|
class PrimaryResourceController < ActionController::Base
|
||||||
POST =
|
PRIMARY_RESOURCE =
|
||||||
begin
|
begin
|
||||||
updated_at = Time.current
|
|
||||||
if ENV['BENCH_STRESS']
|
if ENV['BENCH_STRESS']
|
||||||
comments = (0..50).map do |i|
|
has_many_relationships = (0..50).map do |i|
|
||||||
Comment.new(id: i, body: 'ZOMG A COMMENT', updated_at: updated_at + i)
|
HasManyRelationship.new(id: i, body: 'ZOMG A HAS MANY RELATIONSHIP')
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT', updated_at: updated_at)]
|
has_many_relationships = [HasManyRelationship.new(id: 1, body: 'ZOMG A HAS MANY RELATIONSHIP')]
|
||||||
end
|
end
|
||||||
author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura')
|
has_one_relationship = HasOneRelationship.new(id: 42, first_name: 'Joao', last_name: 'Moura')
|
||||||
Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
|
PrimaryResource.new(id: 1337, title: 'New PrimaryResource', virtual_attribute: nil, body: 'Body', has_many_relationships: has_many_relationships, has_one_relationship: has_one_relationship)
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_with_caching_serializer
|
def render_with_caching_serializer
|
||||||
toggle_cache_status
|
toggle_cache_status
|
||||||
render json: POST, serializer: CachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
|
render json: PRIMARY_RESOURCE, serializer: CachingPrimaryResourceSerializer, adapter: :json, meta: { caching: perform_caching }
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_with_fragment_caching_serializer
|
def render_with_fragment_caching_serializer
|
||||||
toggle_cache_status
|
toggle_cache_status
|
||||||
render json: POST, serializer: FragmentCachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
|
render json: PRIMARY_RESOURCE, serializer: FragmentCachingPrimaryResourceSerializer, adapter: :json, meta: { caching: perform_caching }
|
||||||
end
|
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: PRIMARY_RESOURCE, adapter: :json, meta: { caching: perform_caching }
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_cache_status
|
def render_cache_status
|
||||||
@ -33,7 +32,7 @@ class PostController < ActionController::Base
|
|||||||
# Uncomment to debug
|
# Uncomment to debug
|
||||||
# STDERR.puts cache_store.class
|
# STDERR.puts cache_store.class
|
||||||
# STDERR.puts cache_dependencies
|
# STDERR.puts cache_dependencies
|
||||||
# ActiveSupport::Cache::Store.logger.debug [ActiveModelSerializers.config.cache_store, ActiveModelSerializers.config.perform_caching, CachingPostSerializer._cache, perform_caching, params].inspect
|
# ActiveSupport::Cache::Store.logger.debug [ActiveModelSerializers.config.cache_store, ActiveModelSerializers.config.perform_caching, CachingPrimaryResourceSerializer._cache, perform_caching, params].inspect
|
||||||
render json: { caching: perform_caching, meta: { cache_log: cache_messages, cache_status: cache_status } }.to_json
|
render json: { caching: perform_caching, meta: { cache_log: cache_messages, cache_status: cache_status } }.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -76,9 +75,9 @@ class PostController < ActionController::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
get '/status(/:on)' => 'post#render_cache_status'
|
get '/status(/:on)' => 'primary_resource#render_cache_status'
|
||||||
get '/clear' => 'post#clear'
|
get '/clear' => 'primary_resource#clear'
|
||||||
get '/caching(/:on)' => 'post#render_with_caching_serializer'
|
get '/caching(/:on)' => 'primary_resource#render_with_caching_serializer'
|
||||||
get '/fragment_caching(/:on)' => 'post#render_with_fragment_caching_serializer'
|
get '/fragment_caching(/:on)' => 'primary_resource#render_with_fragment_caching_serializer'
|
||||||
get '/non_caching(/:on)' => 'post#render_with_non_caching_serializer'
|
get '/non_caching(/:on)' => 'primary_resource#render_with_non_caching_serializer'
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,33 +1,33 @@
|
|||||||
Rails.configuration.serializers = []
|
Rails.configuration.serializers = []
|
||||||
class AuthorSerializer < ActiveModel::Serializer
|
class HasOneRelationshipSerializer < ActiveModel::Serializer
|
||||||
attributes :id, :first_name, :last_name
|
attributes :id, :first_name, :last_name
|
||||||
|
|
||||||
has_many :posts, embed: :ids
|
has_many :primary_resources, embed: :ids
|
||||||
has_one :bio
|
has_one :bio
|
||||||
end
|
end
|
||||||
Rails.configuration.serializers << AuthorSerializer
|
Rails.configuration.serializers << HasOneRelationshipSerializer
|
||||||
|
|
||||||
class BlogSerializer < ActiveModel::Serializer
|
class VirtualAttributeSerializer < ActiveModel::Serializer
|
||||||
attributes :id, :name
|
attributes :id, :name
|
||||||
end
|
end
|
||||||
Rails.configuration.serializers << BlogSerializer
|
Rails.configuration.serializers << VirtualAttributeSerializer
|
||||||
|
|
||||||
class CommentSerializer < ActiveModel::Serializer
|
class HasManyRelationshipSerializer < ActiveModel::Serializer
|
||||||
attributes :id, :body, :updated_at
|
attributes :id, :body
|
||||||
|
|
||||||
belongs_to :post
|
belongs_to :primary_resource
|
||||||
belongs_to :author
|
belongs_to :has_one_relationship
|
||||||
end
|
end
|
||||||
Rails.configuration.serializers << CommentSerializer
|
Rails.configuration.serializers << HasManyRelationshipSerializer
|
||||||
|
|
||||||
class PostSerializer < ActiveModel::Serializer
|
class PrimaryResourceSerializer < ActiveModel::Serializer
|
||||||
attributes :id, :title, :body
|
attributes :id, :title, :body
|
||||||
|
|
||||||
has_many :comments, serializer: CommentSerializer
|
has_many :has_many_relationships, serializer: HasManyRelationshipSerializer
|
||||||
belongs_to :blog, serializer: BlogSerializer
|
belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
|
||||||
belongs_to :author, serializer: AuthorSerializer
|
belongs_to :has_one_relationship, serializer: HasOneRelationshipSerializer
|
||||||
|
|
||||||
link(:post_authors) { 'https://example.com/post_authors' }
|
link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
|
||||||
|
|
||||||
meta do
|
meta do
|
||||||
{
|
{
|
||||||
@ -36,33 +36,33 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def blog
|
def virtual_attribute
|
||||||
Blog.new(id: 999, name: 'Custom blog')
|
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Rails.configuration.serializers << PostSerializer
|
Rails.configuration.serializers << PrimaryResourceSerializer
|
||||||
|
|
||||||
class CachingAuthorSerializer < AuthorSerializer
|
class CachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
|
||||||
cache key: 'writer', skip_digest: true
|
cache key: 'writer', skip_digest: true
|
||||||
end
|
end
|
||||||
Rails.configuration.serializers << CachingAuthorSerializer
|
Rails.configuration.serializers << CachingHasOneRelationshipSerializer
|
||||||
|
|
||||||
class CachingCommentSerializer < CommentSerializer
|
class CachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
|
||||||
cache expires_in: 1.day, skip_digest: true
|
cache expires_in: 1.day, skip_digest: true
|
||||||
end
|
end
|
||||||
Rails.configuration.serializers << CachingCommentSerializer
|
Rails.configuration.serializers << CachingHasManyRelationshipSerializer
|
||||||
|
|
||||||
# see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
|
# see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
|
||||||
class CachingPostSerializer < ActiveModel::Serializer
|
class CachingPrimaryResourceSerializer < ActiveModel::Serializer
|
||||||
cache key: 'post', expires_in: 0.1, skip_digest: true
|
cache key: 'primary_resource', expires_in: 0.1, skip_digest: true
|
||||||
|
|
||||||
attributes :id, :title, :body
|
attributes :id, :title, :body
|
||||||
|
|
||||||
belongs_to :blog, serializer: BlogSerializer
|
belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
|
||||||
belongs_to :author, serializer: CachingAuthorSerializer
|
belongs_to :has_one_relationship, serializer: CachingHasOneRelationshipSerializer
|
||||||
has_many :comments, serializer: CachingCommentSerializer
|
has_many :has_many_relationships, serializer: CachingHasManyRelationshipSerializer
|
||||||
|
|
||||||
link(:post_authors) { 'https://example.com/post_authors' }
|
link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
|
||||||
|
|
||||||
meta do
|
meta do
|
||||||
{
|
{
|
||||||
@ -71,33 +71,33 @@ class CachingPostSerializer < ActiveModel::Serializer
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def blog
|
def virtual_attribute
|
||||||
Blog.new(id: 999, name: 'Custom blog')
|
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Rails.configuration.serializers << CachingPostSerializer
|
Rails.configuration.serializers << CachingPrimaryResourceSerializer
|
||||||
|
|
||||||
class FragmentCachingAuthorSerializer < AuthorSerializer
|
class FragmentCachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
|
||||||
cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
|
cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
|
||||||
end
|
end
|
||||||
Rails.configuration.serializers << FragmentCachingAuthorSerializer
|
Rails.configuration.serializers << FragmentCachingHasOneRelationshipSerializer
|
||||||
|
|
||||||
class FragmentCachingCommentSerializer < CommentSerializer
|
class FragmentCachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
|
||||||
cache expires_in: 1.day, except: [:updated_at], skip_digest: true
|
cache expires_in: 1.day, except: [:body], skip_digest: true
|
||||||
end
|
end
|
||||||
Rails.configuration.serializers << CachingCommentSerializer
|
Rails.configuration.serializers << CachingHasManyRelationshipSerializer
|
||||||
|
|
||||||
# see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
|
# see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
|
||||||
class FragmentCachingPostSerializer < ActiveModel::Serializer
|
class FragmentCachingPrimaryResourceSerializer < ActiveModel::Serializer
|
||||||
cache key: 'post', expires_in: 0.1, skip_digest: true
|
cache key: 'primary_resource', expires_in: 0.1, skip_digest: true
|
||||||
|
|
||||||
attributes :id, :title, :body
|
attributes :id, :title, :body
|
||||||
|
|
||||||
belongs_to :blog, serializer: BlogSerializer
|
belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
|
||||||
belongs_to :author, serializer: FragmentCachingAuthorSerializer
|
belongs_to :has_one_relationship, serializer: FragmentCachingHasOneRelationshipSerializer
|
||||||
has_many :comments, serializer: FragmentCachingCommentSerializer
|
has_many :has_many_relationships, serializer: FragmentCachingHasManyRelationshipSerializer
|
||||||
|
|
||||||
link(:post_authors) { 'https://example.com/post_authors' }
|
link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
|
||||||
|
|
||||||
meta do
|
meta do
|
||||||
{
|
{
|
||||||
@ -106,11 +106,11 @@ class FragmentCachingPostSerializer < ActiveModel::Serializer
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def blog
|
def virtual_attribute
|
||||||
Blog.new(id: 999, name: 'Custom blog')
|
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Rails.configuration.serializers << FragmentCachingPostSerializer
|
Rails.configuration.serializers << FragmentCachingPrimaryResourceSerializer
|
||||||
|
|
||||||
if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
|
if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
|
||||||
require 'active_record'
|
require 'active_record'
|
||||||
@ -119,48 +119,48 @@ if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
|
|||||||
ActiveRecord::Schema.define do
|
ActiveRecord::Schema.define do
|
||||||
self.verbose = false
|
self.verbose = false
|
||||||
|
|
||||||
create_table :blogs, force: true do |t|
|
create_table :virtual_attributes, force: true do |t|
|
||||||
t.string :name
|
t.string :name
|
||||||
t.timestamps null: false
|
t.timestamps null: false
|
||||||
end
|
end
|
||||||
create_table :authors, force: true do |t|
|
create_table :has_one_relationships, force: true do |t|
|
||||||
t.string :first_name
|
t.string :first_name
|
||||||
t.string :last_name
|
t.string :last_name
|
||||||
t.timestamps null: false
|
t.timestamps null: false
|
||||||
end
|
end
|
||||||
create_table :posts, force: true do |t|
|
create_table :primary_resources, force: true do |t|
|
||||||
t.string :title
|
t.string :title
|
||||||
t.text :body
|
t.text :body
|
||||||
t.references :author
|
t.references :has_one_relationship
|
||||||
t.references :blog
|
t.references :virtual_attribute
|
||||||
t.timestamps null: false
|
t.timestamps null: false
|
||||||
end
|
end
|
||||||
create_table :comments, force: true do |t|
|
create_table :has_many_relationships, force: true do |t|
|
||||||
t.text :body
|
t.text :body
|
||||||
t.references :author
|
t.references :has_one_relationship
|
||||||
t.references :post
|
t.references :primary_resource
|
||||||
t.timestamps null: false
|
t.timestamps null: false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Comment < ActiveRecord::Base
|
class HasManyRelationship < ActiveRecord::Base
|
||||||
belongs_to :author
|
belongs_to :has_one_relationship
|
||||||
belongs_to :post
|
belongs_to :primary_resource
|
||||||
end
|
end
|
||||||
|
|
||||||
class Author < ActiveRecord::Base
|
class HasOneRelationship < ActiveRecord::Base
|
||||||
has_many :posts
|
has_many :primary_resources
|
||||||
has_many :comments
|
has_many :has_many_relationships
|
||||||
end
|
end
|
||||||
|
|
||||||
class Post < ActiveRecord::Base
|
class PrimaryResource < ActiveRecord::Base
|
||||||
has_many :comments
|
has_many :has_many_relationships
|
||||||
belongs_to :author
|
belongs_to :has_one_relationship
|
||||||
belongs_to :blog
|
belongs_to :virtual_attribute
|
||||||
end
|
end
|
||||||
|
|
||||||
class Blog < ActiveRecord::Base
|
class VirtualAttribute < ActiveRecord::Base
|
||||||
has_many :posts
|
has_many :primary_resources
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
# ActiveModelSerializers::Model is a convenient
|
# ActiveModelSerializers::Model is a convenient
|
||||||
@ -201,19 +201,19 @@ else
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Comment < BenchmarkModel
|
class HasManyRelationship < BenchmarkModel
|
||||||
attr_accessor :id, :body, :updated_at
|
attr_accessor :id, :body
|
||||||
end
|
end
|
||||||
|
|
||||||
class Author < BenchmarkModel
|
class HasOneRelationship < BenchmarkModel
|
||||||
attr_accessor :id, :first_name, :last_name, :posts
|
attr_accessor :id, :first_name, :last_name, :primary_resources
|
||||||
end
|
end
|
||||||
|
|
||||||
class Post < BenchmarkModel
|
class PrimaryResource < BenchmarkModel
|
||||||
attr_accessor :id, :title, :body, :comments, :blog, :author
|
attr_accessor :id, :title, :body, :has_many_relationships, :virtual_attribute, :has_one_relationship
|
||||||
end
|
end
|
||||||
|
|
||||||
class Blog < BenchmarkModel
|
class VirtualAttribute < BenchmarkModel
|
||||||
attr_accessor :id, :name
|
attr_accessor :id, :name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user