-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.rb
67 lines (54 loc) · 1.22 KB
/
entity.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
require "benchmark"
require "oj"
require "grape-entity"
require "./models"
POSTS = (1..10).to_a.map { |i| Post.gen(i) }
class SimpleRepresenter < Grape::Entity
expose :id
expose :body
expose :created_at
expose :author_id do |post|
post.author.id
end
expose :comment_ids do |post|
post.comments.map(&:id)
end
end
class AuthorRepresenter < Grape::Entity
expose :id
expose :name
end
class CommentRepresenter < Grape::Entity
expose :id
expose :body
expose :created_at
expose :author, using: AuthorRepresenter
end
class NestedRepresenter < Grape::Entity
expose :id
expose :body
expose :created_at
expose :author, using: AuthorRepresenter
expose :comments, using: CommentRepresenter
end
class ListRepresenter < Grape::Entity
present_collection true, :items
expose :items, using: NestedRepresenter
end
Benchmark.bmbm(7) do |bm|
bm.report ("simple") do
1_000.times do
SimpleRepresenter.represent(POSTS.first, serializable: true)
end
end
bm.report("nested") do
1_000.times do
NestedRepresenter.represent(POSTS.first, serializable: true)
end
end
bm.report("list") do
1_000.times do
ListRepresenter.represent(POSTS, serializable: true)
end
end
end