alba 3.5.0 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +5 -39
- data/lib/alba/association.rb +4 -1
- data/lib/alba/conditional_attribute.rb +11 -14
- data/lib/alba/layout.rb +8 -6
- data/lib/alba/railtie.rb +2 -2
- data/lib/alba/resource.rb +59 -15
- data/lib/alba/typed_attribute.rb +2 -0
- data/lib/alba/version.rb +1 -1
- data/lib/alba.rb +51 -32
- metadata +4 -52
- data/.codeclimate.yml +0 -12
- data/.editorconfig +0 -10
- data/.github/ISSUE_TEMPLATE/bug_report.md +0 -26
- data/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- data/.github/dependabot.yml +0 -12
- data/.github/workflows/codeql-analysis.yml +0 -70
- data/.github/workflows/lint.yml +0 -17
- data/.github/workflows/main.yml +0 -41
- data/.gitignore +0 -11
- data/.rubocop.yml +0 -156
- data/.yardopts +0 -4
- data/CODE_OF_CONDUCT.md +0 -132
- data/CONTRIBUTING.md +0 -30
- data/Gemfile +0 -39
- data/HACKING.md +0 -42
- data/Rakefile +0 -17
- data/SECURITY.md +0 -12
- data/alba.gemspec +0 -33
- data/benchmark/Gemfile +0 -26
- data/benchmark/README.md +0 -137
- data/benchmark/collection.rb +0 -297
- data/benchmark/prep.rb +0 -56
- data/benchmark/single_resource.rb +0 -300
- data/bin/console +0 -15
- data/bin/setup +0 -8
- data/codecov.yml +0 -8
- data/docs/migrate_from_active_model_serializers.md +0 -359
- data/docs/migrate_from_jbuilder.md +0 -237
- data/docs/rails.md +0 -56
- data/gemfiles/without_active_support.gemfile +0 -19
- data/gemfiles/without_oj.gemfile +0 -19
- data/logo/alba-card.png +0 -0
- data/logo/alba-sign.png +0 -0
- data/logo/alba-typography.png +0 -0
data/alba.gemspec
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require_relative 'lib/alba/version'
|
2
|
-
|
3
|
-
Gem::Specification.new do |spec|
|
4
|
-
spec.name = 'alba'
|
5
|
-
spec.version = Alba::VERSION
|
6
|
-
spec.authors = ['OKURA Masafumi']
|
7
|
-
spec.email = ['masafumi.o1988@gmail.com']
|
8
|
-
|
9
|
-
spec.summary = 'Alba is the fastest JSON serializer for Ruby.'
|
10
|
-
spec.description = "Alba is the fastest JSON serializer for Ruby. It focuses on performance, flexibility and usability."
|
11
|
-
spec.homepage = 'https://github.com/okuramasafumi/alba'
|
12
|
-
spec.license = 'MIT'
|
13
|
-
spec.required_ruby_version = Gem::Requirement.new('>= 3.0.0')
|
14
|
-
|
15
|
-
spec.metadata = {
|
16
|
-
'bug_tracker_uri' => 'https://github.com/okuramasafumi/alba/issues',
|
17
|
-
'changelog_uri' => 'https://github.com/okuramasafumi/alba/blob/main/CHANGELOG.md',
|
18
|
-
'documentation_uri' => 'https://rubydoc.info/github/okuramasafumi/alba',
|
19
|
-
'source_code_uri' => 'https://github.com/okuramasafumi/alba',
|
20
|
-
'rubygems_mfa_required' => 'true'
|
21
|
-
}
|
22
|
-
|
23
|
-
# Specify which files should be added to the gem when it is released.
|
24
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
-
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
-
end
|
28
|
-
spec.bindir = 'exe'
|
29
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
-
spec.require_paths = ['lib']
|
31
|
-
|
32
|
-
spec.add_dependency "ostruct", "~> 0.6"
|
33
|
-
end
|
data/benchmark/Gemfile
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
source 'https://rubygems.org'
|
4
|
-
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
5
|
-
|
6
|
-
gem 'active_model_serializers'
|
7
|
-
gem 'activerecord', '~> 7.1'
|
8
|
-
gem 'alba', path: '../'
|
9
|
-
gem 'benchmark-ips'
|
10
|
-
gem 'benchmark-memory'
|
11
|
-
gem 'blueprinter'
|
12
|
-
gem 'csv'
|
13
|
-
gem 'fast_serializer_ruby'
|
14
|
-
gem 'jbuilder'
|
15
|
-
gem 'jserializer'
|
16
|
-
gem 'json'
|
17
|
-
gem 'multi_json'
|
18
|
-
gem 'oj'
|
19
|
-
gem 'oj_serializers'
|
20
|
-
gem 'panko_serializer'
|
21
|
-
gem 'pg'
|
22
|
-
gem 'primalize'
|
23
|
-
gem 'representable'
|
24
|
-
gem 'simple_ams'
|
25
|
-
gem 'sqlite3', '~> 1.4'
|
26
|
-
gem 'turbostreamer'
|
data/benchmark/README.md
DELETED
@@ -1,137 +0,0 @@
|
|
1
|
-
## Benchmark for json serializers
|
2
|
-
|
3
|
-
This directory contains a few different benchmark scripts.
|
4
|
-
|
5
|
-
## How to run
|
6
|
-
|
7
|
-
```
|
8
|
-
bundle install
|
9
|
-
|
10
|
-
# with `Oj.optimize_rails`
|
11
|
-
bundle exec ruby collection.rb
|
12
|
-
|
13
|
-
# without `Oj.optimize_rails`
|
14
|
-
NO_OJ_OPTIMIZE_RAILS=1 bundle exec ruby collection.rb
|
15
|
-
|
16
|
-
# with `Oj.optimize_rails` and YJIT
|
17
|
-
YJIT=1 bundle exec ruby collection.rb
|
18
|
-
|
19
|
-
# with YJIT and without `Oj.optimize_rails`
|
20
|
-
YJIT=1 NO_OJ_OPTIMIZE_RAILS=1 bundle exec ruby collection.rb
|
21
|
-
```
|
22
|
-
|
23
|
-
## Result
|
24
|
-
|
25
|
-
As a reference, here's the benchmark result run in my (@okuramasafumi) machine.
|
26
|
-
|
27
|
-
Machine spec:
|
28
|
-
|
29
|
-
|Key|Value|
|
30
|
-
|---|---|
|
31
|
-
|OS|macOS 14.7|
|
32
|
-
|CPU|Apple M1 Pro|
|
33
|
-
|RAM|16GB|
|
34
|
-
|Ruby|ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin23]|
|
35
|
-
|
36
|
-
Library versions:
|
37
|
-
|
38
|
-
|Library|Version|
|
39
|
-
|---|---|
|
40
|
-
|alba|3.2.0|
|
41
|
-
|blueprinter|1.1.0|
|
42
|
-
|fast_serializer_ruby|0.6.9|
|
43
|
-
|jserializer|0.2.1|
|
44
|
-
|oj|3.16.6|
|
45
|
-
|simple_ams|0.2.6|
|
46
|
-
|representable|3.2.0|
|
47
|
-
|turbostreamer|1.11.0|
|
48
|
-
|jbuilder|2.13.0|
|
49
|
-
|panko_serializer|0.8.2|
|
50
|
-
|active_model_serializers|0.10.14|
|
51
|
-
|
52
|
-
`benchmark-ips` with `Oj.optimize_rails`:
|
53
|
-
|
54
|
-
```
|
55
|
-
Comparison:
|
56
|
-
panko: 447.0 i/s
|
57
|
-
jserializer: 168.9 i/s - 2.65x slower
|
58
|
-
alba_inline: 149.4 i/s - 2.99x slower
|
59
|
-
alba: 146.5 i/s - 3.05x slower
|
60
|
-
turbostreamer: 138.7 i/s - 3.22x slower
|
61
|
-
rails: 105.6 i/s - 4.23x slower
|
62
|
-
fast_serializer: 97.6 i/s - 4.58x slower
|
63
|
-
blueprinter: 66.7 i/s - 6.70x slower
|
64
|
-
representable: 50.6 i/s - 8.83x slower
|
65
|
-
simple_ams: 35.5 i/s - 12.57x slower
|
66
|
-
ams: 14.8 i/s - 30.25x slower
|
67
|
-
```
|
68
|
-
|
69
|
-
`benchmark-ips` without `Oj.optimize_rails`:
|
70
|
-
|
71
|
-
```
|
72
|
-
Comparison:
|
73
|
-
panko: 457.9 i/s
|
74
|
-
jserializer: 165.9 i/s - 2.76x slower
|
75
|
-
alba: 160.1 i/s - 2.86x slower
|
76
|
-
alba_inline: 158.5 i/s - 2.89x slower
|
77
|
-
turbostreamer: 141.7 i/s - 3.23x slower
|
78
|
-
fast_serializer: 96.2 i/s - 4.76x slower
|
79
|
-
rails: 87.2 i/s - 5.25x slower
|
80
|
-
blueprinter: 67.4 i/s - 6.80x slower
|
81
|
-
representable: 43.4 i/s - 10.55x slower
|
82
|
-
simple_ams: 34.7 i/s - 13.20x slower
|
83
|
-
ams: 14.2 i/s - 32.28x slower
|
84
|
-
```
|
85
|
-
|
86
|
-
`benchmark-ips` with `Oj.optimize_rails` and YJIT:
|
87
|
-
|
88
|
-
```
|
89
|
-
Comparison:
|
90
|
-
panko: 676.6 i/s
|
91
|
-
jserializer: 285.3 i/s - 2.37x slower
|
92
|
-
turbostreamer: 264.2 i/s - 2.56x slower
|
93
|
-
alba: 258.9 i/s - 2.61x slower
|
94
|
-
fast_serializer: 179.0 i/s - 3.78x slower
|
95
|
-
rails: 150.7 i/s - 4.49x slower
|
96
|
-
alba_inline: 131.5 i/s - 5.15x slower
|
97
|
-
blueprinter: 110.0 i/s - 6.15x slower
|
98
|
-
representable: 73.5 i/s - 9.21x slower
|
99
|
-
simple_ams: 62.8 i/s - 10.77x slower
|
100
|
-
ams: 20.4 i/s - 33.10x slower
|
101
|
-
```
|
102
|
-
|
103
|
-
`benchmark-ips` with YJIT and without `Oj.optimize_rails`:
|
104
|
-
|
105
|
-
```
|
106
|
-
Comparison:
|
107
|
-
panko: 701.9 i/s
|
108
|
-
alba: 311.1 i/s - 2.26x slower
|
109
|
-
jserializer: 281.6 i/s - 2.49x slower
|
110
|
-
turbostreamer: 240.4 i/s - 2.92x slower
|
111
|
-
fast_serializer: 180.5 i/s - 3.89x slower
|
112
|
-
alba_inline: 135.6 i/s - 5.18x slower
|
113
|
-
rails: 131.4 i/s - 5.34x slower
|
114
|
-
blueprinter: 110.7 i/s - 6.34x slower
|
115
|
-
representable: 70.5 i/s - 9.96x slower
|
116
|
-
simple_ams: 57.3 i/s - 12.24x slower
|
117
|
-
ams: 20.3 i/s - 34.51x slower
|
118
|
-
```
|
119
|
-
|
120
|
-
`benchmark-memory`:
|
121
|
-
|
122
|
-
```
|
123
|
-
Comparison:
|
124
|
-
panko: 259178 allocated
|
125
|
-
turbostreamer: 817800 allocated - 3.16x more
|
126
|
-
jserializer: 826425 allocated - 3.19x more
|
127
|
-
alba: 846465 allocated - 3.27x more
|
128
|
-
alba_inline: 867361 allocated - 3.35x more
|
129
|
-
fast_serializer: 1474345 allocated - 5.69x more
|
130
|
-
rails: 2265905 allocated - 8.74x more
|
131
|
-
blueprinter: 2469905 allocated - 9.53x more
|
132
|
-
representable: 4994281 allocated - 19.27x more
|
133
|
-
ams: 5233265 allocated - 20.19x more
|
134
|
-
simple_ams: 9506817 allocated - 36.68x more
|
135
|
-
```
|
136
|
-
|
137
|
-
Conclusion: panko is extremely fast but it's a C extension gem. As pure Ruby gems, Alba, `turbostreamer` and `jserializer` are notably faster than others, but Alba is slightly slower than other two. With `Oj.optimize_rails`, `jbuilder` and Rails standard serialization are also fast.
|
data/benchmark/collection.rb
DELETED
@@ -1,297 +0,0 @@
|
|
1
|
-
# Benchmark script to run varieties of JSON serializers
|
2
|
-
# Fetch Alba from local, otherwise fetch latest from RubyGems
|
3
|
-
|
4
|
-
require_relative 'prep'
|
5
|
-
|
6
|
-
# --- Alba serializers ---
|
7
|
-
|
8
|
-
require "alba"
|
9
|
-
|
10
|
-
Alba.inflector = :active_support
|
11
|
-
|
12
|
-
class AlbaCommentResource
|
13
|
-
include ::Alba::Resource
|
14
|
-
attributes :id, :body
|
15
|
-
end
|
16
|
-
|
17
|
-
class AlbaPostResource
|
18
|
-
include ::Alba::Resource
|
19
|
-
attributes :id, :body
|
20
|
-
attribute :commenter_names do |post|
|
21
|
-
post.commenters.pluck(:name)
|
22
|
-
end
|
23
|
-
many :comments, resource: AlbaCommentResource
|
24
|
-
end
|
25
|
-
|
26
|
-
class AlbaCommentWithTransformationResource < AlbaCommentResource
|
27
|
-
transform_keys :lower_camel
|
28
|
-
end
|
29
|
-
|
30
|
-
class AlbaPostWithTransformationResource < AlbaPostResource
|
31
|
-
many :comments, resource: AlbaCommentWithTransformationResource
|
32
|
-
|
33
|
-
transform_keys :lower_camel
|
34
|
-
end
|
35
|
-
|
36
|
-
# --- ActiveModelSerializer serializers ---
|
37
|
-
|
38
|
-
require "active_model_serializers"
|
39
|
-
|
40
|
-
ActiveModelSerializers.logger = Logger.new(nil)
|
41
|
-
|
42
|
-
class AMSCommentSerializer < ActiveModel::Serializer
|
43
|
-
attributes :id, :body
|
44
|
-
end
|
45
|
-
|
46
|
-
class AMSPostSerializer < ActiveModel::Serializer
|
47
|
-
attributes :id, :body
|
48
|
-
attribute :commenter_names
|
49
|
-
has_many :comments, serializer: AMSCommentSerializer
|
50
|
-
|
51
|
-
def commenter_names
|
52
|
-
object.commenters.pluck(:name)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
# --- Blueprint serializers ---
|
57
|
-
|
58
|
-
require "blueprinter"
|
59
|
-
|
60
|
-
class CommentBlueprint < Blueprinter::Base
|
61
|
-
fields :id, :body
|
62
|
-
end
|
63
|
-
|
64
|
-
class PostBlueprint < Blueprinter::Base
|
65
|
-
fields :id, :body, :commenter_names
|
66
|
-
association :comments, blueprint: CommentBlueprint
|
67
|
-
|
68
|
-
def commenter_names
|
69
|
-
commenters.pluck(:name)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
# --- Fast Serializer Ruby
|
74
|
-
|
75
|
-
require "fast_serializer"
|
76
|
-
|
77
|
-
class FastSerializerCommentResource
|
78
|
-
include ::FastSerializer::Schema::Mixin
|
79
|
-
attributes :id, :body
|
80
|
-
end
|
81
|
-
|
82
|
-
class FastSerializerPostResource
|
83
|
-
include ::FastSerializer::Schema::Mixin
|
84
|
-
|
85
|
-
attributes :id, :body
|
86
|
-
|
87
|
-
attribute :commenter_names do
|
88
|
-
object.commenters.pluck(:name)
|
89
|
-
end
|
90
|
-
|
91
|
-
has_many :comments, serializer: FastSerializerCommentResource
|
92
|
-
end
|
93
|
-
|
94
|
-
# --- Jserializer serializers ---
|
95
|
-
|
96
|
-
require 'jserializer'
|
97
|
-
|
98
|
-
class JserializerCommentSerializer < Jserializer::Base
|
99
|
-
attributes :id, :body
|
100
|
-
end
|
101
|
-
|
102
|
-
class JserializerPostSerializer < Jserializer::Base
|
103
|
-
attributes :id, :body, :commenter_names
|
104
|
-
has_many :comments, serializer: JserializerCommentSerializer
|
105
|
-
def commenter_names
|
106
|
-
object.commenters.pluck(:name)
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
# --- Panko serializers ---
|
111
|
-
#
|
112
|
-
|
113
|
-
require "panko_serializer"
|
114
|
-
|
115
|
-
class PankoCommentSerializer < Panko::Serializer
|
116
|
-
attributes :id, :body
|
117
|
-
end
|
118
|
-
|
119
|
-
|
120
|
-
class PankoPostSerializer < Panko::Serializer
|
121
|
-
attributes :id, :body, :commenter_names
|
122
|
-
|
123
|
-
has_many :comments, serializer: PankoCommentSerializer
|
124
|
-
|
125
|
-
def commenter_names
|
126
|
-
object.commenters.pluck(:name)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
# --- Representable serializers ---
|
131
|
-
|
132
|
-
require "representable"
|
133
|
-
|
134
|
-
class CommentRepresenter < Representable::Decorator
|
135
|
-
include Representable::JSON
|
136
|
-
|
137
|
-
property :id
|
138
|
-
property :body
|
139
|
-
end
|
140
|
-
|
141
|
-
class PostsRepresenter < Representable::Decorator
|
142
|
-
include Representable::JSON::Collection
|
143
|
-
|
144
|
-
items class: Post do
|
145
|
-
property :id
|
146
|
-
property :body
|
147
|
-
property :commenter_names
|
148
|
-
collection :comments, decorator: CommentRepresenter
|
149
|
-
end
|
150
|
-
|
151
|
-
def commenter_names
|
152
|
-
commenters.pluck(:name)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
# --- SimpleAMS serializers ---
|
157
|
-
|
158
|
-
require "simple_ams"
|
159
|
-
|
160
|
-
class SimpleAMSCommentSerializer
|
161
|
-
include SimpleAMS::DSL
|
162
|
-
|
163
|
-
attributes :id, :body
|
164
|
-
end
|
165
|
-
|
166
|
-
class SimpleAMSPostSerializer
|
167
|
-
include SimpleAMS::DSL
|
168
|
-
|
169
|
-
attributes :id, :body
|
170
|
-
attribute :commenter_names
|
171
|
-
has_many :comments, serializer: SimpleAMSCommentSerializer
|
172
|
-
|
173
|
-
def commenter_names
|
174
|
-
object.commenters.pluck(:name)
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
require 'turbostreamer'
|
179
|
-
TurboStreamer.set_default_encoder(:json, :oj)
|
180
|
-
|
181
|
-
class TurbostreamerSerializer
|
182
|
-
def initialize(posts)
|
183
|
-
@posts = posts
|
184
|
-
end
|
185
|
-
|
186
|
-
def to_json
|
187
|
-
TurboStreamer.encode do |json|
|
188
|
-
json.array! @posts do |post|
|
189
|
-
json.object! do
|
190
|
-
json.extract! post, :id, :body, :commenter_names
|
191
|
-
|
192
|
-
json.comments post.comments do |comment|
|
193
|
-
json.object! do
|
194
|
-
json.extract! comment, :id, :body
|
195
|
-
end
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
# --- Test data creation ---
|
204
|
-
|
205
|
-
100.times do |i|
|
206
|
-
post = Post.create!(body: "post#{i}")
|
207
|
-
user1 = User.create!(name: "John#{i}")
|
208
|
-
user2 = User.create!(name: "Jane#{i}")
|
209
|
-
10.times do |n|
|
210
|
-
post.comments.create!(commenter: user1, body: "Comment1_#{i}_#{n}")
|
211
|
-
post.comments.create!(commenter: user2, body: "Comment2_#{i}_#{n}")
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
posts = Post.all.includes(:comments, :commenters)
|
216
|
-
|
217
|
-
# --- Store the serializers in procs ---
|
218
|
-
|
219
|
-
alba = Proc.new { AlbaPostResource.new(posts).serialize }
|
220
|
-
alba_with_transformation = Proc.new { AlbaPostWithTransformationResource.new(posts).serialize }
|
221
|
-
alba_inline = Proc.new do
|
222
|
-
Alba.serialize(posts) do
|
223
|
-
attributes :id, :body
|
224
|
-
attribute :commenter_names do |post|
|
225
|
-
post.commenters.pluck(:name)
|
226
|
-
end
|
227
|
-
many :comments do
|
228
|
-
attributes :id, :body
|
229
|
-
end
|
230
|
-
end
|
231
|
-
end
|
232
|
-
ams = Proc.new { ActiveModelSerializers::SerializableResource.new(posts, {each_serializer: AMSPostSerializer}).to_json }
|
233
|
-
blueprinter = Proc.new { PostBlueprint.render(posts) }
|
234
|
-
fast_serializer = Proc.new { FastSerializerPostResource.new(posts).to_json }
|
235
|
-
jserializer = Proc.new { JserializerPostSerializer.new(posts, is_collection: true).to_json }
|
236
|
-
panko = proc { Panko::ArraySerializer.new(posts, each_serializer: PankoPostSerializer).to_json }
|
237
|
-
rails = Proc.new do
|
238
|
-
posts.to_json(include: {comments: {only: [:id, :body]}}, methods: [:commenter_names])
|
239
|
-
end
|
240
|
-
representable = Proc.new { PostsRepresenter.new(posts).to_json }
|
241
|
-
simple_ams = Proc.new { SimpleAMS::Renderer::Collection.new(posts, serializer: SimpleAMSPostSerializer).to_json }
|
242
|
-
turbostreamer = Proc.new { TurbostreamerSerializer.new(posts).to_json }
|
243
|
-
|
244
|
-
# --- Execute the serializers to check their output ---
|
245
|
-
GC.disable
|
246
|
-
puts "Checking outputs..."
|
247
|
-
correct = alba.call
|
248
|
-
parsed_correct = JSON.parse(correct)
|
249
|
-
{
|
250
|
-
alba_inline: alba_inline,
|
251
|
-
ams: ams,
|
252
|
-
blueprinter: blueprinter,
|
253
|
-
fast_serializer: fast_serializer,
|
254
|
-
jserializer: jserializer,
|
255
|
-
panko: panko,
|
256
|
-
rails: rails,
|
257
|
-
representable: representable,
|
258
|
-
simple_ams: simple_ams,
|
259
|
-
turbostreamer: turbostreamer
|
260
|
-
}.each do |name, serializer|
|
261
|
-
result = serializer.call
|
262
|
-
parsed_result = JSON.parse(result)
|
263
|
-
puts "#{name} yields wrong output: #{parsed_result}" unless parsed_result == parsed_correct
|
264
|
-
end
|
265
|
-
|
266
|
-
# --- Run the benchmarks ---
|
267
|
-
|
268
|
-
benchmark_body = lambda do |x|
|
269
|
-
x.report(:alba, &alba)
|
270
|
-
x.report(:alba_with_transformation, &alba_with_transformation)
|
271
|
-
x.report(:alba_inline, &alba_inline)
|
272
|
-
x.report(:ams, &ams)
|
273
|
-
x.report(:blueprinter, &blueprinter)
|
274
|
-
x.report(:fast_serializer, &fast_serializer)
|
275
|
-
x.report(:jserializer, &jserializer)
|
276
|
-
x.report(:panko, &panko)
|
277
|
-
x.report(:rails, &rails)
|
278
|
-
x.report(:representable, &representable)
|
279
|
-
x.report(:simple_ams, &simple_ams)
|
280
|
-
x.report(:turbostreamer, &turbostreamer)
|
281
|
-
|
282
|
-
x.compare!
|
283
|
-
end
|
284
|
-
|
285
|
-
require 'benchmark/ips'
|
286
|
-
Benchmark.ips(&benchmark_body)
|
287
|
-
|
288
|
-
require 'benchmark/memory'
|
289
|
-
Benchmark.memory(&benchmark_body)
|
290
|
-
|
291
|
-
# --- Show gem versions ---
|
292
|
-
|
293
|
-
puts "Gem versions:"
|
294
|
-
gems = %w[alba active_model_serializers blueprinter fast_serializer jserializer panko_serializer representable simple_ams turbostreamer]
|
295
|
-
Bundler.load.specs.each do |spec|
|
296
|
-
puts "#{spec.name}: #{spec.version}" if gems.include?(spec.name)
|
297
|
-
end
|
data/benchmark/prep.rb
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
# --- Test data model setup ---
|
2
|
-
|
3
|
-
RubyVM::YJIT.enable if ENV["YJIT"]
|
4
|
-
require "csv"
|
5
|
-
require "pg"
|
6
|
-
require "active_record"
|
7
|
-
require "active_record/connection_adapters/postgresql_adapter"
|
8
|
-
require "logger"
|
9
|
-
require "oj"
|
10
|
-
require "sqlite3"
|
11
|
-
Oj.optimize_rails unless ENV['NO_OJ_OPTIMIZE_RAILS']
|
12
|
-
|
13
|
-
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
14
|
-
# ActiveRecord::Base.logger = Logger.new($stdout)
|
15
|
-
|
16
|
-
ActiveRecord::Schema.define do
|
17
|
-
create_table :posts, force: true do |t|
|
18
|
-
t.string :body
|
19
|
-
end
|
20
|
-
|
21
|
-
create_table :comments, force: true do |t|
|
22
|
-
t.integer :post_id
|
23
|
-
t.string :body
|
24
|
-
t.integer :commenter_id
|
25
|
-
end
|
26
|
-
|
27
|
-
create_table :users, force: true do |t|
|
28
|
-
t.string :name
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
class Post < ActiveRecord::Base
|
33
|
-
has_many :comments
|
34
|
-
has_many :commenters, through: :comments, class_name: 'User', source: :commenter
|
35
|
-
|
36
|
-
def attributes
|
37
|
-
{id: nil, body: nil, commenter_names: commenter_names}
|
38
|
-
end
|
39
|
-
|
40
|
-
def commenter_names
|
41
|
-
commenters.pluck(:name)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
class Comment < ActiveRecord::Base
|
46
|
-
belongs_to :post
|
47
|
-
belongs_to :commenter, class_name: 'User'
|
48
|
-
|
49
|
-
def attributes
|
50
|
-
{id: nil, body: nil}
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
class User < ActiveRecord::Base
|
55
|
-
has_many :comments
|
56
|
-
end
|