-
Notifications
You must be signed in to change notification settings - Fork 46
/
local.rb
316 lines (248 loc) · 7.11 KB
/
local.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Benchmark script to run varieties of JSON serializers
# Fetch Alba from local, otherwise fetch latest from RubyGems
# --- Bundle dependencies ---
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "active_model_serializers"
gem "activerecord", "6.1.3"
gem "alba", path: '../'
gem "benchmark-ips"
gem "blueprinter"
gem "jbuilder"
gem "jsonapi-serializer" # successor of fast_jsonapi
gem "multi_json"
gem "oj"
gem "representable"
gem "sqlite3"
end
# --- Test data model setup ---
require "active_record"
require "logger"
require "oj"
require "sqlite3"
Oj.optimize_rails
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
# ActiveRecord::Base.logger = Logger.new($stdout)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :body
end
create_table :comments, force: true do |t|
t.integer :post_id
t.string :body
t.integer :commenter_id
end
create_table :users, force: true do |t|
t.string :name
end
end
class Post < ActiveRecord::Base
has_many :comments
has_many :commenters, through: :comments, class_name: 'User', source: :commenter
def attributes
{id: nil, body: nil, commenter_names: commenter_names}
end
def commenter_names
commenters.pluck(:name)
end
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :commenter, class_name: 'User'
def attributes
{id: nil, body: nil}
end
end
class User < ActiveRecord::Base
has_many :comments
end
# --- Alba serializers ---
require "alba"
Alba.backend = :oj
class AlbaCommentResource
include ::Alba::Resource
attributes :id, :body
end
class AlbaPostResource
include ::Alba::Resource
attributes :id, :body
attribute :commenter_names do |post|
post.commenters.pluck(:name)
end
many :comments, resource: AlbaCommentResource
end
# --- ActiveModelSerializer serializers ---
require "active_model_serializers"
class AMSCommentSerializer < ActiveModel::Serializer
attributes :id, :body
end
class AMSPostSerializer < ActiveModel::Serializer
attributes :id, :body
attribute :commenter_names
has_many :comments, serializer: AMSCommentSerializer
def commenter_names
object.commenters.pluck(:name)
end
end
# --- Blueprint serializers ---
require "blueprinter"
class CommentBlueprint < Blueprinter::Base
fields :id, :body
end
class PostBlueprint < Blueprinter::Base
fields :id, :body, :commenter_names
association :comments, blueprint: CommentBlueprint
def commenter_names
commenters.pluck(:name)
end
end
# --- JBuilder serializers ---
require "jbuilder"
class Post
def to_builder
Jbuilder.new do |post|
post.call(self, :id, :body, :commenter_names, :comments)
end
end
def commenter_names
commenters.pluck(:name)
end
end
class Comment
def to_builder
Jbuilder.new do |comment|
comment.call(self, :id, :body)
end
end
end
# --- JSONAPI:Serializer serializers / (successor of fast_jsonapi) ---
class JsonApiStandardCommentSerializer
include JSONAPI::Serializer
attribute :id
attribute :body
end
class JsonApiStandardPostSerializer
include JSONAPI::Serializer
# set_type :post # optional
attribute :id
attribute :body
attribute :commenter_names
attribute :comments do |post|
post.comments.map { |comment| JsonApiSameFormatCommentSerializer.new(comment) }
end
end
# --- JSONAPI:Serializer serializers that format the code the same flat way as the other gems here ---
# code to convert from JSON:API output to "flat" JSON, like the other serializers build
class JsonApiSameFormatSerializer
include JSONAPI::Serializer
def as_json(*_options)
hash = serializable_hash
if hash[:data].is_a? Hash
hash[:data][:attributes]
elsif hash[:data].is_a? Array
hash[:data].pluck(:attributes)
elsif hash[:data].nil?
{ }
else
raise "unexpected data type #{hash[:data].class}"
end
end
end
class JsonApiSameFormatCommentSerializer < JsonApiSameFormatSerializer
attribute :id
attribute :body
end
class JsonApiSameFormatPostSerializer < JsonApiSameFormatSerializer
attribute :id
attribute :body
attribute :commenter_names
attribute :comments do |post|
post.comments.map { |comment| JsonApiSameFormatCommentSerializer.new(comment) }
end
end
# --- Representable serializers ---
require "representable"
class CommentRepresenter < Representable::Decorator
include Representable::JSON
property :id
property :body
end
class PostRepresenter < Representable::Decorator
include Representable::JSON
property :id
property :body
property :commenter_names
collection :comments
def commenter_names
commenters.pluck(:name)
end
end
# --- Test data creation ---
post = Post.create!(body: 'post')
user1 = User.create!(name: 'John')
user2 = User.create!(name: 'Jane')
post.comments.create!(commenter: user1, body: 'Comment1')
post.comments.create!(commenter: user2, body: 'Comment2')
post.reload
# --- Store the serializers in procs ---
alba = Proc.new { AlbaPostResource.new(post).serialize }
alba_inline = Proc.new do
Alba.serialize(post) do
attributes :id, :body
attribute :commenter_names do |post|
post.commenters.pluck(:name)
end
many :comments do
attributes :id, :body
end
end
end
ams = Proc.new { AMSPostSerializer.new(post, {}).to_json }
blueprinter = Proc.new { PostBlueprint.render(post) }
jbuilder = Proc.new { post.to_builder.target! }
jsonapi = proc { JsonApiStandardPostSerializer.new(post).to_json }
jsonapi_same_format = proc { JsonApiSameFormatPostSerializer.new(post).to_json }
rails = Proc.new { ActiveSupport::JSON.encode(post.serializable_hash(include: :comments)) }
representable = Proc.new { PostRepresenter.new(post).to_json }
# --- Execute the serializers to check their output ---
puts "Serializer outputs ----------------------------------"
{
alba: alba,
alba_inline: alba_inline,
ams: ams,
blueprinter: blueprinter,
jbuilder: jbuilder, # different order
jsonapi: jsonapi, # nested JSON:API format
jsonapi_same_format: jsonapi_same_format,
rails: rails,
representable: representable
}.each { |name, serializer| puts "#{name.to_s.ljust(24, ' ')} #{serializer.call}" }
# --- Run the benchmarks ---
require 'benchmark'
time = 1000
Benchmark.bmbm do |x|
x.report(:alba) { time.times(&alba) }
x.report(:alba_inline) { time.times(&alba_inline) }
x.report(:ams) { time.times(&ams) }
x.report(:blueprinter) { time.times(&blueprinter) }
x.report(:jbuilder) { time.times(&jbuilder) }
x.report(:jsonapi) { time.times(&jsonapi) }
x.report(:jsonapi_same_format) { time.times(&jsonapi_same_format) }
x.report(:rails) { time.times(&rails) }
x.report(:representable) { time.times(&representable) }
end
require 'benchmark/ips'
Benchmark.ips do |x|
x.report(:alba, &alba)
x.report(:alba_inline, &alba_inline)
x.report(:ams, &ams)
x.report(:blueprinter, &blueprinter)
x.report(:jbuilder, &jbuilder)
x.report(:jsonapi, &jsonapi)
x.report(:jsonapi_same_format, &jsonapi_same_format)
x.report(:rails, &rails)
x.report(:representable, &representable)
x.compare!
end