Skip to content

Commit a2f0e8c

Browse files
committed
Updated benchmark program
1 parent 943e3e2 commit a2f0e8c

13 files changed

Lines changed: 336 additions & 54 deletions
Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
11
# frozen_string_literal: true
22

3-
require 'benchmark/ips'
4-
53
class BenchmarksController < ApplicationController
6-
def index(n = '100')
7-
@comments = 1.upto(n.to_i).map { |i| Comment.new(i, nil, "comment #{i}") }
4+
before_action :prepare_comments
5+
6+
def self.comments
7+
@comments ||= 1.upto(1000).map { |i| Comment.new(i, nil, "comment #{i}") }
8+
end
9+
10+
def jb
11+
end
812

9-
jb = render_to_string 'index_jb'
10-
jbuilder = render_to_string 'index_jbuilder'
13+
def jbuilder
14+
end
1115

16+
def simple_json_oj
1217
SimpleJson.json_module = SimpleJson::Json::Oj
13-
simple_json = render_to_string 'index'
18+
end
1419

20+
def simple_json_as_json
1521
SimpleJson.json_module = ActiveSupport::JSON
16-
simple_json_active_support_json = render_to_string 'index'
17-
18-
raise 'jb != jbuilder' unless jb == jbuilder
19-
raise 'simple_json != jbuilder' unless simple_json == jbuilder
20-
raise 'simple_json_active_support_json != jbuilder' unless simple_json_active_support_json == jbuilder
21-
22-
result = Benchmark.ips do |x|
23-
x.report('jb') { render_to_string 'index_jb' }
24-
x.report('jbuilder') { render_to_string 'index_jbuilder' }
25-
x.report('simple_json(oj)') {
26-
SimpleJson.json_module = SimpleJson::Json::Oj
27-
render_to_string 'index'
28-
}
29-
x.report('simple_json(AS::json)') {
30-
SimpleJson.json_module = ActiveSupport::JSON
31-
render_to_string 'index'
32-
}
33-
x.compare!
34-
end
35-
render plain: result.data.to_s
22+
end
23+
24+
private
25+
26+
def prepare_comments
27+
n = params.fetch(:n, 100).to_i
28+
@comments = self.class.comments.take(n)
3629
end
3730
end
Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,55 @@
11
# frozen_string_literal: true
22

3-
{ body: comment.body }
3+
length = comment.body.length
4+
likes = comment.id * 3
5+
dislikes = comment.id % 4
6+
rating = (likes.to_f / (dislikes.zero? ? 1 : dislikes)).round(2)
7+
position = comment_counter + 1
8+
9+
{
10+
id: comment.id,
11+
body: comment.body,
12+
metrics: {
13+
length: length,
14+
readability: (length / 5.0).round(2),
15+
engagement: {
16+
likes: likes,
17+
dislikes: dislikes,
18+
rating: rating
19+
}
20+
},
21+
author: {
22+
name: "user-#{comment.id}",
23+
active: comment.id.odd?,
24+
badges: (comment.id % 3).zero? ? %w[insightful] : [],
25+
contact: {
26+
email: "user#{comment.id}@example.com",
27+
url: "https://example.com/users/#{comment.id}"
28+
},
29+
settings: {
30+
theme: comment.id.even? ? 'dark' : 'light',
31+
timezone: "UTC+#{(comment.id % 5) - 2}"
32+
}
33+
},
34+
tags: ['bench', "comment-#{comment.id}", (comment.id.even? ? 'even' : nil)].compact,
35+
flags: {
36+
pinned: position == 1,
37+
hidden: (comment.id % 5).zero?
38+
},
39+
metadata: {
40+
position: position,
41+
attachments: (comment.id % 4).zero? ? [{ filename: "attachment-#{comment.id}.txt", size: comment.id * 128 }] : [],
42+
history: [
43+
{ version: 1, updated_by: "user-#{comment.id}", updated_at: '2024-01-01T00:00:00Z' },
44+
{ version: 2, updated_by: "user-#{comment.id}", updated_at: '2024-01-02T00:00:00Z' }
45+
]
46+
},
47+
links: {
48+
self: "/comments/#{comment.id}",
49+
post: "/posts/#{comment.post&.id || 1}"
50+
},
51+
extras: {
52+
mood: %w[happy neutral excited][comment.id % 3],
53+
score: comment.id * position
54+
}
55+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,61 @@
11
# frozen_string_literal: true
22

3+
length = comment.body.length
4+
likes = comment.id * 3
5+
dislikes = comment.id % 4
6+
rating = (likes.to_f / (dislikes.zero? ? 1 : dislikes)).round(2)
7+
position = comment_counter + 1
8+
9+
json.id comment.id
310
json.body comment.body
11+
12+
json.metrics do
13+
json.length length
14+
json.readability (length / 5.0).round(2)
15+
json.engagement do
16+
json.likes likes
17+
json.dislikes dislikes
18+
json.rating rating
19+
end
20+
end
21+
22+
json.author do
23+
json.name "user-#{comment.id}"
24+
json.active comment.id.odd?
25+
json.badges ((comment.id % 3).zero? ? %w[insightful] : [])
26+
json.contact do
27+
json.email "user#{comment.id}@example.com"
28+
json.url "https://example.com/users/#{comment.id}"
29+
end
30+
json.settings do
31+
json.theme(comment.id.even? ? 'dark' : 'light')
32+
json.timezone "UTC+#{(comment.id % 5) - 2}"
33+
end
34+
end
35+
36+
json.tags ['bench', "comment-#{comment.id}", (comment.id.even? ? 'even' : nil)].compact
37+
38+
json.flags do
39+
json.pinned position == 1
40+
json.hidden (comment.id % 5).zero?
41+
end
42+
43+
json.metadata do
44+
json.position position
45+
attachments = (comment.id % 4).zero? ? [{ filename: "attachment-#{comment.id}.txt", size: comment.id * 128 }] : []
46+
json.attachments attachments
47+
json.history [
48+
{ version: 1, updated_by: "user-#{comment.id}", updated_at: '2024-01-01T00:00:00Z' },
49+
{ version: 2, updated_by: "user-#{comment.id}", updated_at: '2024-01-02T00:00:00Z' }
50+
]
51+
end
52+
53+
json.links do
54+
json.self "/comments/#{comment.id}"
55+
json.post "/posts/#{comment.post&.id || 1}"
56+
end
57+
58+
json.extras do
59+
json.mood %w[happy neutral excited][comment.id % 3]
60+
json.score comment.id * position
61+
end
Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,56 @@
11
# frozen_string_literal: true
22

3-
->(comment:) {
3+
->(comment:, position:) {
4+
length = comment.body.length
5+
likes = comment.id * 3
6+
dislikes = comment.id % 4
7+
rating = (likes.to_f / (dislikes.zero? ? 1 : dislikes)).round(2)
8+
49
{
5-
body: comment.body
10+
id: comment.id,
11+
body: comment.body,
12+
metrics: {
13+
length: length,
14+
readability: (length / 5.0).round(2),
15+
engagement: {
16+
likes: likes,
17+
dislikes: dislikes,
18+
rating: rating
19+
}
20+
},
21+
author: {
22+
name: "user-#{comment.id}",
23+
active: comment.id.odd?,
24+
badges: (comment.id % 3).zero? ? %w[insightful] : [],
25+
contact: {
26+
email: "user#{comment.id}@example.com",
27+
url: "https://example.com/users/#{comment.id}"
28+
},
29+
settings: {
30+
theme: comment.id.even? ? 'dark' : 'light',
31+
timezone: "UTC+#{(comment.id % 5) - 2}"
32+
}
33+
},
34+
tags: ['bench', "comment-#{comment.id}", (comment.id.even? ? 'even' : nil)].compact,
35+
flags: {
36+
pinned: position == 1,
37+
hidden: (comment.id % 5).zero?
38+
},
39+
metadata: {
40+
position: position,
41+
attachments: (comment.id % 4).zero? ? [{ filename: "attachment-#{comment.id}.txt", size: comment.id * 128 }] : [],
42+
history: [
43+
{ version: 1, updated_by: "user-#{comment.id}", updated_at: '2024-01-01T00:00:00Z' },
44+
{ version: 2, updated_by: "user-#{comment.id}", updated_at: '2024-01-02T00:00:00Z' }
45+
]
46+
},
47+
links: {
48+
self: "/comments/#{comment.id}",
49+
post: "/posts/#{comment.post&.id || 1}"
50+
},
51+
extras: {
52+
mood: %w[happy neutral excited][comment.id % 3],
53+
score: comment.id * position
54+
}
655
}
756
}

test/dummy_app/app/views/benchmarks/index.simple_json.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/dummy_app/app/views/benchmarks/index_jb.json.jb

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/dummy_app/app/views/benchmarks/index_jbuilder.json.jbuilder

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
lengths = @comments.map { |comment| comment.body.length }
4+
avg_length = lengths.empty? ? 0.0 : (lengths.sum.to_f / lengths.size).round(2)
5+
6+
{
7+
meta: {
8+
total_comments: @comments.size,
9+
longest_comment_length: lengths.max || 0,
10+
average_comment_length: avg_length,
11+
tags: %w[bench simple json],
12+
flags: {
13+
has_many: @comments.size > 500,
14+
empty: @comments.empty?
15+
}
16+
},
17+
comments: render(partial: 'comment_jb', collection: @comments, as: 'comment')
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
lengths = @comments.map { |comment| comment.body.length }
4+
avg_length = lengths.empty? ? 0.0 : (lengths.sum.to_f / lengths.size).round(2)
5+
6+
json.meta do
7+
json.total_comments @comments.size
8+
json.longest_comment_length lengths.max || 0
9+
json.average_comment_length avg_length
10+
json.tags %w[bench simple json]
11+
json.flags do
12+
json.has_many @comments.size > 500
13+
json.empty @comments.empty?
14+
end
15+
end
16+
17+
json.comments do
18+
json.partial! 'comment_jbuilder', collection: @comments, as: 'comment'
19+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
lengths = @comments.map { |comment| comment.body.length }
4+
avg_length = lengths.empty? ? 0.0 : (lengths.sum.to_f / lengths.size).round(2)
5+
6+
{
7+
meta: {
8+
total_comments: @comments.size,
9+
longest_comment_length: lengths.max || 0,
10+
average_comment_length: avg_length,
11+
tags: %w[bench simple json],
12+
flags: {
13+
has_many: @comments.size > 500,
14+
empty: @comments.empty?
15+
}
16+
},
17+
comments: @comments.each_with_index.map do |comment, index|
18+
partial!('benchmarks/_comment_simple_json', comment: comment, position: index + 1)
19+
end
20+
}

0 commit comments

Comments
 (0)