-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_resource.rb
300 lines (233 loc) · 6.53 KB
/
single_resource.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
# Benchmark script to run varieties of JSON serializers
# Fetch Alba from local, otherwise fetch latest from RubyGems
require_relative 'prep'
# --- Alba serializers ---
require "alba"
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
# --- Jserializer serializers ---
require 'jserializer'
class JserializerCommentSerializer < Jserializer::Base
attributes :id, :body
end
class JserializerPostSerializer < Jserializer::Base
attributes :id, :body, :commenter_names
has_many :comments, serializer: JserializerCommentSerializer
def commenter_names
object.commenters.pluck(:name)
end
end
# --- Panko serializers ---
require "panko_serializer"
class PankoCommentSerializer < Panko::Serializer
attributes :id, :body
end
class PankoPostSerializer < Panko::Serializer
attributes :id, :body, :commenter_names
has_many :comments, serializer: PankoCommentSerializer
def commenter_names
object.commenters.pluck(:name)
end
end
# --- Primalize serializers ---
class PrimalizeCommentResource < Primalize::Single
attributes id: integer, body: string
end
class PrimalizePostResource < Primalize::Single
alias post object
attributes(
id: integer,
body: string,
comments: array(primalize(PrimalizeCommentResource)),
commenter_names: array(string),
)
def commenter_names
post.commenters.pluck(:name)
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
# --- SimpleAMS serializers ---
require "simple_ams"
class SimpleAMSCommentSerializer
include SimpleAMS::DSL
attributes :id, :body
end
class SimpleAMSPostSerializer
include SimpleAMS::DSL
attributes :id, :body
attribute :commenter_names
has_many :comments, serializer: SimpleAMSCommentSerializer
def commenter_names
object.commenters.pluck(:name)
end
end
require 'turbostreamer'
TurboStreamer.set_default_encoder(:json, :oj)
class TurbostreamerSerializer
def initialize(post)
@post = post
end
def to_json
TurboStreamer.encode do |json|
json.object! do
json.extract! @post, :id, :body, :commenter_names
json.comments @post.comments do |comment|
json.object! do
json.extract! comment, :id, :body
end
end
end
end
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! }
jserializer = Proc.new { JserializerPostSerializer.new(post).to_json }
panko = proc { PankoPostSerializer.new.serialize_to_json(post) }
primalize = proc { PrimalizePostResource.new(post).to_json }
rails = Proc.new { ActiveSupport::JSON.encode(post.serializable_hash(include: :comments)) }
representable = Proc.new { PostRepresenter.new(post).to_json }
simple_ams = Proc.new { SimpleAMS::Renderer.new(post, serializer: SimpleAMSPostSerializer).to_json }
turbostreamer = Proc.new { TurbostreamerSerializer.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
jserializer: jserializer,
panko: panko,
primalize: primalize,
rails: rails,
representable: representable,
simple_ams: simple_ams,
turbostreamer: turbostreamer
}.each do |name, serializer|
puts "#{name.to_s.ljust(24, ' ')} #{serializer.call}"
end
# --- Run the benchmarks ---
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(:jserializer, &jserializer)
x.report(:panko, &panko)
x.report(:primalize, &primalize)
x.report(:rails, &rails)
x.report(:representable, &representable)
x.report(:simple_ams, &simple_ams)
x.report(:turbostreamer, &turbostreamer)
x.compare!
end
require 'benchmark/memory'
Benchmark.memory 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(:jserializer, &jserializer)
x.report(:panko, &panko)
x.report(:primalize, &primalize)
x.report(:rails, &rails)
x.report(:representable, &representable)
x.report(:simple_ams, &simple_ams)
x.report(:turbostreamer, &turbostreamer)
x.compare!
end