-
Notifications
You must be signed in to change notification settings - Fork 1
/
comparison_spec.rb
179 lines (158 loc) · 5.86 KB
/
comparison_spec.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
# frozen_string_literal: true
require 'spec_helper'
require 'benchmark'
require 'jbuilder'
require 'multi_json'
require 'rabl'
describe 'JBuilder comparison' do
extend SpecHelper::OperatorTesting
before {
stub_const('Author', Babl::Utils::Value.new(:name, :birthyear, :job, :url))
stub_const('Article', Babl::Utils::Value.new(:author, :title, :body, :date, :references, :comments))
stub_const('Comment', Babl::Utils::Value.new(:author, :date, :email, :body))
stub_const('Reference', Babl::Utils::Value.new(:name, :url))
}
let(:data) {
Array.new(100) {
author = Author.new('Fred', 1990, 'Software developer', 'https://github.com/fterrazzoni')
references = [
Reference.new('BABL repo', 'https://github.com/getbannerman/babl/'),
Reference.new('JBuilder repo', 'https://github.com/rails/jbuilder')
]
comments = [
Comment.new(author, Time.now, 'frederic.terrazzoni@gmail.com', 'I like it')
]
Article.new(author, 'Profiling Jbuilder', 'This is a very short explanation', Time.now, references, comments)
}
}
let(:jbuilder_test) {
-> {
Jbuilder.new do |json|
json.articles do
json.array! data do |article|
json.author(article.author, :name, :birthyear, :job)
json.title article.title
json.body article.body
json.date article.date.iso8601
json.references article.references do |reference|
json.name reference.name
json.url reference.url
end
json.comments article.comments do |comment|
json.author(comment.author, :name, :birthyear, :job)
json.email comment.email
json.body comment.body
json.date comment.date.iso8601
end
end
end
end.attributes!
}
}
let(:rabl_test) {
Rabl.configuration.include_child_root = false
code = '
collection self, root: :articles, object_root: false
attributes :title, :body
node(:date) { |article| article.date.iso8601 }
child :author do
attributes :name, :birthyear, :job
end
child :references do
attributes :name, :url
end
child :comments do
attributes :email, :body
node(:date) { |article| article.date.iso8601 }
child :author do
attributes :name, :birthyear, :job
end
end
'
template = Rabl::Engine.new(code)
-> { template.apply(data, {}).to_dumpable }
}
let(:handwritten_test) {
-> {
{
articles: data.map { |article|
{
title: article.title,
date: article.date.iso8601,
body: article.body,
author: {
name: article.author.name,
birthyear: article.author.birthyear,
job: article.author.job
},
references: article.references.map { |reference|
{
name: reference.name,
url: reference.url
}
},
comments: article.comments.map { |comment|
{
email: comment.email,
body: comment.body,
date: comment.date.iso8601,
author: {
name: comment.author.name,
birthyear: comment.author.birthyear,
job: comment.author.job
}
}
}
}
}
}
}
}
let(:babl_template) {
Babl.source {
author = object(:name, :birthyear, :job)
{
articles: each.object(
title: _,
body: _,
author: _.(author),
date: _.nav(&:iso8601),
references: _.each.object(:name, :url),
comments: _.each.object(
author: _.(author),
email: _,
body: _,
date: _.nav(:iso8601)
)
)
}
}
}
let(:babl_test) {
-> { babl_template.compile(pretty: false).render(data) }
}
let(:precompiled_babl_test) {
compiled = babl_template.compile(pretty: false)
-> { compiled.render(data) }
}
let(:benchmarks) {
{
'RABL' => rabl_test,
'JBuilder' => jbuilder_test,
'BABL' => babl_test,
'BABL (compiled once)' => precompiled_babl_test,
'Handwritten Ruby' => handwritten_test
}
}
let(:n) { 80 }
# Ensure all benchmarks are producing the same JSON
before { expect(benchmarks.values.map(&:call).map { |x| MultiJson.load(MultiJson.dump(x)) }.uniq.size).to eq 1 }
# Run benchmarks
it {
Benchmark.bm(30) do |x|
benchmarks.each do |description, benchmark|
x.report(description) { n.times { benchmark.call } }
end
end
}
end