Skip to content

Commit ecd2c1a

Browse files
committed
Add a set of benchmarks
1 parent 49b8ffc commit ecd2c1a

17 files changed

+165
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ gemfiles/.bundle
1010
# autogenerated from .rux files
1111
spec/dummy/app/components/button.rb
1212
spec/dummy/app/components/home_component.rb
13+
14+
tmp/
15+
log/

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ end
1111

1212
group :development do
1313
gem 'appraisal'
14+
gem 'benchmark-ips'
15+
gem 'rails', '~> 7.0'
16+
gem 'slim', '~> 5.0'
1417
end
1518

1619
group :test do
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div>
2+
<h1>hello <%= @name %></h1>
3+
<% 50.times do %>
4+
<%= render(Performance::ErbNestedNameComponent.new(name: @name)) %>
5+
<% end %>
6+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Performance::ErbNameComponent < ViewComponent::Base
2+
def initialize(name:)
3+
@name = name
4+
end
5+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>nested hello <%= @name %></p>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Performance::ErbNestedNameComponent < ViewComponent::Base
2+
def initialize(name:)
3+
@name = name
4+
end
5+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Performance::NameComponent < ViewComponent::Base
2+
def initialize(name:)
3+
@name = name
4+
end
5+
6+
def call
7+
Rux.tag("div") {
8+
Rux.create_buffer.tap { |_rux_buf_|
9+
_rux_buf_ << Rux.tag("h1") {
10+
Rux.create_buffer.tap { |_rux_buf_|
11+
_rux_buf_ << "hello "
12+
_rux_buf_ << @name
13+
}.to_s
14+
}
15+
_rux_buf_ << 50.times.map {
16+
render(Performance::NestedNameComponent.new(name: @name))
17+
}
18+
}.to_s
19+
}
20+
end
21+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Performance::NameComponent < ViewComponent::Base
2+
def initialize(name:)
3+
@name = name
4+
end
5+
6+
def call
7+
<div>
8+
<h1>hello {@name}</h1>
9+
{50.times.map do
10+
<Performance::NestedNameComponent name={@name} />
11+
end}
12+
</div>
13+
end
14+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Performance::NestedNameComponent < ViewComponent::Base
2+
def initialize(name:)
3+
@name = name
4+
end
5+
6+
def call
7+
Rux.tag("p") {
8+
Rux.create_buffer.tap { |_rux_buf_|
9+
_rux_buf_ << "nested hello "
10+
_rux_buf_ << @name
11+
}.to_s
12+
}
13+
end
14+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Performance::NestedNameComponent < ViewComponent::Base
2+
def initialize(name:)
3+
@name = name
4+
end
5+
6+
def call
7+
<p>nested hello {@name}</p>
8+
end
9+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
h1
2+
= "hello #{@name}"
3+
4+
- for i in (0..50)
5+
= render(Performance::SlimNestedNameComponent.new(name: @name))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Performance::SlimNameComponent < ViewComponent::Base
2+
def initialize(name:)
3+
@name = name
4+
end
5+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
p
2+
= "nested hello #{@name}"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Performance::SlimNestedNameComponent < ViewComponent::Base
2+
def initialize(name:)
3+
@name = name
4+
end
5+
end

performance/rux_rails_bench.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require 'rux'
2+
require 'benchmark/ips'
3+
4+
puts "Booting Rails..."
5+
6+
require 'rails/all'
7+
require 'slim'
8+
require 'view_component'
9+
10+
# Configure Rails Environment
11+
ENV["RAILS_ENV"] = "production"
12+
require File.expand_path("../spec/dummy/config/application.rb", __dir__)
13+
14+
RuxRails::DummyApplication.initialize!
15+
16+
class BenchmarksController < ActionController::Base
17+
end
18+
19+
BenchmarksController.view_paths = [File.expand_path("./views", __dir__)]
20+
controller_view = BenchmarksController.new.view_context
21+
22+
puts "Transpiling rux components..."
23+
24+
# transpile component files
25+
Dir.glob(File.expand_path('./components/*.rux', __dir__)) do |in_file|
26+
puts "Transpiling #{in_file}"
27+
rux_file = Rux::File.new(in_file)
28+
rux_file.write
29+
end
30+
31+
module Performance
32+
require_relative 'components/name_component'
33+
require_relative 'components/nested_name_component'
34+
require_relative 'components/erb_name_component'
35+
require_relative 'components/erb_nested_name_component'
36+
require_relative 'components/slim_name_component'
37+
require_relative 'components/slim_nested_name_component'
38+
end
39+
40+
Benchmark.ips do |x|
41+
x.time = 10
42+
x.warmup = 2
43+
44+
x.report('rux') do
45+
controller_view.render(Performance::NameComponent.new(name: "Fox Mulder"))
46+
end
47+
48+
x.report('erb') do
49+
controller_view.render(Performance::ErbNameComponent.new(name: 'Fox Mulder'))
50+
end
51+
52+
x.report('slim') do
53+
controller_view.render(Performance::SlimNameComponent.new(name: 'Fox Mulder'))
54+
end
55+
56+
x.report('partial') do
57+
controller_view.render('partial', name: 'Fox Mulder')
58+
end
59+
60+
x.compare!
61+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>nested hello <%= name %></p>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>hello <%= name %></h1>
2+
3+
<% 50.times do %>
4+
<%= render 'nested', name: name, nested: false %>
5+
<% end %>

0 commit comments

Comments
 (0)