Skip to content

Commit a9a0e09

Browse files
committed
Update benchmarking fixtures. Add transform benchmark.
1 parent a958542 commit a9a0e09

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

test/benchmark/bm_caching.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def expected
7070
},
7171
'author' => {
7272
'id' => 42,
73-
'name' => 'Joao Moura.'
73+
'first_name' => 'Joao',
74+
'last_name' => 'Moura'
7475
}
7576
}
7677
}

test/benchmark/bm_transform.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require_relative './benchmarking_support'
2+
require_relative './app'
3+
4+
time = 10
5+
disable_gc = true
6+
ActiveModelSerializers.config.key_transform = :unaltered
7+
comments = (0..50).map do |i|
8+
Comment.new(id: i, body: 'ZOMG A COMMENT')
9+
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)
13+
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
14+
serialization = adapter.as_json
15+
16+
Benchmark.ams('unaltered', time: time, disable_gc: disable_gc) do
17+
ActiveModelSerializers::Transform.unaltered(serialization)
18+
end
19+
20+
Benchmark.ams('dash', time: time, disable_gc: disable_gc) do
21+
ActiveModelSerializers::Transform.dash(serialization)
22+
end
23+
24+
Benchmark.ams('camel', time: time, disable_gc: disable_gc) do
25+
ActiveModelSerializers::Transform.camel(serialization)
26+
end
27+
28+
Benchmark.ams('camel_lower', time: time, disable_gc: disable_gc) do
29+
ActiveModelSerializers::Transform.camel_lower(serialization)
30+
end

test/benchmark/controllers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class PostController < ActionController::Base
88
else
99
comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT')]
1010
end
11-
author = Author.new(id: 42, name: 'Joao Moura.')
11+
author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura')
1212
Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
1313
end
1414

test/benchmark/fixtures.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Rails.configuration.serializers = []
22
class AuthorSerializer < ActiveModel::Serializer
3-
attributes :id, :name
3+
attributes :id, :first_name, :last_name
44

55
has_many :posts, embed: :ids
66
has_one :bio
@@ -27,14 +27,23 @@ class PostSerializer < ActiveModel::Serializer
2727
belongs_to :blog, serializer: BlogSerializer
2828
belongs_to :author, serializer: AuthorSerializer
2929

30+
link(:post_authors) { 'https://example.com/post_authors' }
31+
32+
meta do
33+
{
34+
rating: 5,
35+
favorite_count: 10
36+
}
37+
end
38+
3039
def blog
3140
Blog.new(id: 999, name: 'Custom blog')
3241
end
3342
end
3443
Rails.configuration.serializers << PostSerializer
3544

3645
class CachingAuthorSerializer < AuthorSerializer
37-
cache key: 'writer', only: [:name], skip_digest: true
46+
cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
3847
end
3948
Rails.configuration.serializers << CachingAuthorSerializer
4049

@@ -63,7 +72,8 @@ class CachingPostSerializer < PostSerializer
6372
t.timestamps null: false
6473
end
6574
create_table :authors, force: true do |t|
66-
t.string :name
75+
t.string :first_name
76+
t.string :last_name
6777
t.timestamps null: false
6878
end
6979
create_table :posts, force: true do |t|
@@ -144,7 +154,7 @@ class Comment < BenchmarkModel
144154
end
145155

146156
class Author < BenchmarkModel
147-
attr_accessor :id, :name, :posts
157+
attr_accessor :id, :first_name, :last_name, :posts
148158
end
149159

150160
class Post < BenchmarkModel

0 commit comments

Comments
 (0)