Skip to content

Commit 31605d0

Browse files
committed
Make bencharked model names reflect relationships; fix expected response
1 parent d191342 commit 31605d0

File tree

4 files changed

+118
-108
lines changed

4 files changed

+118
-108
lines changed

test/benchmark/bm_caching.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,25 @@ def get(url)
6666
def expected
6767
@expected ||=
6868
{
69-
'post' => {
70-
'id' => 1337,
71-
'title' => 'New Post',
69+
'primary_resource' => {
70+
'id' => 1337,
71+
'title' => 'New PrimaryResource',
7272
'body' => 'Body',
73-
'comments' => [
74-
{
75-
'id' => 1,
76-
'body' => 'ZOMG A COMMENT'
77-
}
78-
],
79-
'blog' => {
80-
'id' => 999,
81-
'name' => 'Custom blog'
73+
'virtual_attribute' => {
74+
'id' => 999,
75+
'name' => 'Free-Range Virtual Attribute'
8276
},
83-
'author' => {
77+
'has_one_relationship' => {
8478
'id' => 42,
8579
'first_name' => 'Joao',
8680
'last_name' => 'Moura'
87-
}
81+
},
82+
'has_many_relationships' => [
83+
{
84+
'id' => 1,
85+
'body' => 'ZOMG A HAS MANY RELATIONSHIP'
86+
}
87+
]
8888
}
8989
}
9090
end

test/benchmark/bm_transform.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@
44
time = 10
55
disable_gc = true
66
ActiveModelSerializers.config.key_transform = :unaltered
7-
comments = (0..50).map do |i|
8-
Comment.new(id: i, body: 'ZOMG A COMMENT')
7+
has_many_relationships = (0..50).map do |i|
8+
HasManyRelationship.new(id: i, body: 'ZOMG A HAS MANY RELATIONSHIP')
99
end
10-
author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura')
11-
post = Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
12-
serializer = PostSerializer.new(post)
10+
has_one_relationship = HasOneRelationship.new(
11+
id: 42,
12+
first_name: 'Joao',
13+
last_name: 'Moura'
14+
)
15+
primary_resource = PrimaryResource.new(
16+
id: 1337,
17+
title: 'New PrimaryResource',
18+
virtual_attribute: nil,
19+
body: 'Body',
20+
has_many_relationships: has_many_relationships,
21+
has_one_relationship: has_one_relationship
22+
)
23+
serializer = PrimaryResourceSerializer.new(primary_resource)
1324
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
1425
serialization = adapter.as_json
1526

test/benchmark/controllers.rb

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
class PostController < ActionController::Base
2-
POST =
1+
class PrimaryResourceController < ActionController::Base
2+
PRIMARY_RESOURCE =
33
begin
4-
updated_at = Time.current
54
if ENV['BENCH_STRESS']
6-
comments = (0..50).map do |i|
7-
Comment.new(id: i, body: 'ZOMG A COMMENT', updated_at: updated_at + i)
5+
has_many_relationships = (0..50).map do |i|
6+
HasManyRelationship.new(id: i, body: 'ZOMG A HAS MANY RELATIONSHIP')
87
end
98
else
10-
comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT', updated_at: updated_at)]
9+
has_many_relationships = [HasManyRelationship.new(id: 1, body: 'ZOMG A HAS MANY RELATIONSHIP')]
1110
end
12-
author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura')
13-
Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
11+
has_one_relationship = HasOneRelationship.new(id: 42, first_name: 'Joao', last_name: 'Moura')
12+
PrimaryResource.new(id: 1337, title: 'New PrimaryResource', virtual_attribute: nil, body: 'Body', has_many_relationships: has_many_relationships, has_one_relationship: has_one_relationship)
1413
end
1514

1615
def render_with_caching_serializer
1716
toggle_cache_status
18-
render json: POST, serializer: CachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
17+
render json: PRIMARY_RESOURCE, serializer: CachingPrimaryResourceSerializer, adapter: :json, meta: { caching: perform_caching }
1918
end
2019

2120
def render_with_fragment_caching_serializer
2221
toggle_cache_status
23-
render json: POST, serializer: FragmentCachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
22+
render json: PRIMARY_RESOURCE, serializer: FragmentCachingPrimaryResourceSerializer, adapter: :json, meta: { caching: perform_caching }
2423
end
2524

2625
def render_with_non_caching_serializer
2726
toggle_cache_status
28-
render json: POST, adapter: :json, meta: { caching: perform_caching }
27+
render json: PRIMARY_RESOURCE, adapter: :json, meta: { caching: perform_caching }
2928
end
3029

3130
def render_cache_status
3231
toggle_cache_status
3332
# Uncomment to debug
3433
# STDERR.puts cache_store.class
3534
# STDERR.puts cache_dependencies
36-
# ActiveSupport::Cache::Store.logger.debug [ActiveModelSerializers.config.cache_store, ActiveModelSerializers.config.perform_caching, CachingPostSerializer._cache, perform_caching, params].inspect
35+
# ActiveSupport::Cache::Store.logger.debug [ActiveModelSerializers.config.cache_store, ActiveModelSerializers.config.perform_caching, CachingPrimaryResourceSerializer._cache, perform_caching, params].inspect
3736
render json: { caching: perform_caching, meta: { cache_log: cache_messages, cache_status: cache_status } }.to_json
3837
end
3938

@@ -76,9 +75,9 @@ def toggle_cache_status
7675
end
7776

7877
Rails.application.routes.draw do
79-
get '/status(/:on)' => 'post#render_cache_status'
80-
get '/clear' => 'post#clear'
81-
get '/caching(/:on)' => 'post#render_with_caching_serializer'
82-
get '/fragment_caching(/:on)' => 'post#render_with_fragment_caching_serializer'
83-
get '/non_caching(/:on)' => 'post#render_with_non_caching_serializer'
78+
get '/status(/:on)' => 'primary_resource#render_cache_status'
79+
get '/clear' => 'primary_resource#clear'
80+
get '/caching(/:on)' => 'primary_resource#render_with_caching_serializer'
81+
get '/fragment_caching(/:on)' => 'primary_resource#render_with_fragment_caching_serializer'
82+
get '/non_caching(/:on)' => 'primary_resource#render_with_non_caching_serializer'
8483
end

test/benchmark/fixtures.rb

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
Rails.configuration.serializers = []
2-
class AuthorSerializer < ActiveModel::Serializer
2+
class HasOneRelationshipSerializer < ActiveModel::Serializer
33
attributes :id, :first_name, :last_name
44

5-
has_many :posts, embed: :ids
5+
has_many :primary_resources, embed: :ids
66
has_one :bio
77
end
8-
Rails.configuration.serializers << AuthorSerializer
8+
Rails.configuration.serializers << HasOneRelationshipSerializer
99

10-
class BlogSerializer < ActiveModel::Serializer
10+
class VirtualAttributeSerializer < ActiveModel::Serializer
1111
attributes :id, :name
1212
end
13-
Rails.configuration.serializers << BlogSerializer
13+
Rails.configuration.serializers << VirtualAttributeSerializer
1414

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

18-
belongs_to :post
19-
belongs_to :author
18+
belongs_to :primary_resource
19+
belongs_to :has_one_relationship
2020
end
21-
Rails.configuration.serializers << CommentSerializer
21+
Rails.configuration.serializers << HasManyRelationshipSerializer
2222

23-
class PostSerializer < ActiveModel::Serializer
23+
class PrimaryResourceSerializer < ActiveModel::Serializer
2424
attributes :id, :title, :body
2525

26-
has_many :comments, serializer: CommentSerializer
27-
belongs_to :blog, serializer: BlogSerializer
28-
belongs_to :author, serializer: AuthorSerializer
26+
has_many :has_many_relationships, serializer: HasManyRelationshipSerializer
27+
belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
28+
belongs_to :has_one_relationship, serializer: HasOneRelationshipSerializer
2929

30-
link(:post_authors) { 'https://example.com/post_authors' }
30+
link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
3131

3232
meta do
3333
{
@@ -36,33 +36,33 @@ class PostSerializer < ActiveModel::Serializer
3636
}
3737
end
3838

39-
def blog
40-
Blog.new(id: 999, name: 'Custom blog')
39+
def virtual_attribute
40+
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
4141
end
4242
end
43-
Rails.configuration.serializers << PostSerializer
43+
Rails.configuration.serializers << PrimaryResourceSerializer
4444

45-
class CachingAuthorSerializer < AuthorSerializer
45+
class CachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
4646
cache key: 'writer', skip_digest: true
4747
end
48-
Rails.configuration.serializers << CachingAuthorSerializer
48+
Rails.configuration.serializers << CachingHasOneRelationshipSerializer
4949

50-
class CachingCommentSerializer < CommentSerializer
50+
class CachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
5151
cache expires_in: 1.day, skip_digest: true
5252
end
53-
Rails.configuration.serializers << CachingCommentSerializer
53+
Rails.configuration.serializers << CachingHasManyRelationshipSerializer
5454

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

5959
attributes :id, :title, :body
6060

61-
belongs_to :blog, serializer: BlogSerializer
62-
belongs_to :author, serializer: CachingAuthorSerializer
63-
has_many :comments, serializer: CachingCommentSerializer
61+
belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
62+
belongs_to :has_one_relationship, serializer: CachingHasOneRelationshipSerializer
63+
has_many :has_many_relationships, serializer: CachingHasManyRelationshipSerializer
6464

65-
link(:post_authors) { 'https://example.com/post_authors' }
65+
link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
6666

6767
meta do
6868
{
@@ -71,33 +71,33 @@ class CachingPostSerializer < ActiveModel::Serializer
7171
}
7272
end
7373

74-
def blog
75-
Blog.new(id: 999, name: 'Custom blog')
74+
def virtual_attribute
75+
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
7676
end
7777
end
78-
Rails.configuration.serializers << CachingPostSerializer
78+
Rails.configuration.serializers << CachingPrimaryResourceSerializer
7979

80-
class FragmentCachingAuthorSerializer < AuthorSerializer
80+
class FragmentCachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
8181
cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
8282
end
83-
Rails.configuration.serializers << FragmentCachingAuthorSerializer
83+
Rails.configuration.serializers << FragmentCachingHasOneRelationshipSerializer
8484

85-
class FragmentCachingCommentSerializer < CommentSerializer
86-
cache expires_in: 1.day, except: [:updated_at], skip_digest: true
85+
class FragmentCachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
86+
cache expires_in: 1.day, except: [:body], skip_digest: true
8787
end
88-
Rails.configuration.serializers << CachingCommentSerializer
88+
Rails.configuration.serializers << CachingHasManyRelationshipSerializer
8989

9090
# 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
91+
class FragmentCachingPrimaryResourceSerializer < ActiveModel::Serializer
92+
cache key: 'primary_resource', expires_in: 0.1, skip_digest: true
9393

9494
attributes :id, :title, :body
9595

96-
belongs_to :blog, serializer: BlogSerializer
97-
belongs_to :author, serializer: FragmentCachingAuthorSerializer
98-
has_many :comments, serializer: FragmentCachingCommentSerializer
96+
belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
97+
belongs_to :has_one_relationship, serializer: FragmentCachingHasOneRelationshipSerializer
98+
has_many :has_many_relationships, serializer: FragmentCachingHasManyRelationshipSerializer
9999

100-
link(:post_authors) { 'https://example.com/post_authors' }
100+
link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
101101

102102
meta do
103103
{
@@ -106,11 +106,11 @@ class FragmentCachingPostSerializer < ActiveModel::Serializer
106106
}
107107
end
108108

109-
def blog
110-
Blog.new(id: 999, name: 'Custom blog')
109+
def virtual_attribute
110+
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
111111
end
112112
end
113-
Rails.configuration.serializers << FragmentCachingPostSerializer
113+
Rails.configuration.serializers << FragmentCachingPrimaryResourceSerializer
114114

115115
if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
116116
require 'active_record'
@@ -119,48 +119,48 @@ def blog
119119
ActiveRecord::Schema.define do
120120
self.verbose = false
121121

122-
create_table :blogs, force: true do |t|
122+
create_table :virtual_attributes, force: true do |t|
123123
t.string :name
124124
t.timestamps null: false
125125
end
126-
create_table :authors, force: true do |t|
126+
create_table :has_one_relationships, force: true do |t|
127127
t.string :first_name
128128
t.string :last_name
129129
t.timestamps null: false
130130
end
131-
create_table :posts, force: true do |t|
131+
create_table :primary_resources, force: true do |t|
132132
t.string :title
133133
t.text :body
134-
t.references :author
135-
t.references :blog
134+
t.references :has_one_relationship
135+
t.references :virtual_attribute
136136
t.timestamps null: false
137137
end
138-
create_table :comments, force: true do |t|
138+
create_table :has_many_relationships, force: true do |t|
139139
t.text :body
140-
t.references :author
141-
t.references :post
140+
t.references :has_one_relationship
141+
t.references :primary_resource
142142
t.timestamps null: false
143143
end
144144
end
145145

146-
class Comment < ActiveRecord::Base
147-
belongs_to :author
148-
belongs_to :post
146+
class HasManyRelationship < ActiveRecord::Base
147+
belongs_to :has_one_relationship
148+
belongs_to :primary_resource
149149
end
150150

151-
class Author < ActiveRecord::Base
152-
has_many :posts
153-
has_many :comments
151+
class HasOneRelationship < ActiveRecord::Base
152+
has_many :primary_resources
153+
has_many :has_many_relationships
154154
end
155155

156-
class Post < ActiveRecord::Base
157-
has_many :comments
158-
belongs_to :author
159-
belongs_to :blog
156+
class PrimaryResource < ActiveRecord::Base
157+
has_many :has_many_relationships
158+
belongs_to :has_one_relationship
159+
belongs_to :virtual_attribute
160160
end
161161

162-
class Blog < ActiveRecord::Base
163-
has_many :posts
162+
class VirtualAttribute < ActiveRecord::Base
163+
has_many :primary_resources
164164
end
165165
else
166166
# ActiveModelSerializers::Model is a convenient
@@ -201,19 +201,19 @@ def read_attribute_for_serialization(key)
201201
end
202202
end
203203

204-
class Comment < BenchmarkModel
205-
attr_accessor :id, :body, :updated_at
204+
class HasManyRelationship < BenchmarkModel
205+
attr_accessor :id, :body
206206
end
207207

208-
class Author < BenchmarkModel
209-
attr_accessor :id, :first_name, :last_name, :posts
208+
class HasOneRelationship < BenchmarkModel
209+
attr_accessor :id, :first_name, :last_name, :primary_resources
210210
end
211211

212-
class Post < BenchmarkModel
213-
attr_accessor :id, :title, :body, :comments, :blog, :author
212+
class PrimaryResource < BenchmarkModel
213+
attr_accessor :id, :title, :body, :has_many_relationships, :virtual_attribute, :has_one_relationship
214214
end
215215

216-
class Blog < BenchmarkModel
216+
class VirtualAttribute < BenchmarkModel
217217
attr_accessor :id, :name
218218
end
219219
end

0 commit comments

Comments
 (0)