Skip to content

Commit 72f13a4

Browse files
committed
Add benchmark
1 parent 531bf3b commit 72f13a4

7 files changed

+48
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>hello <%= @name %></h1>
2+
3+
<% 50.times do %>
4+
<%= render Performance::NestedComplexComponent.new(name: @name) %>
5+
<% end %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Performance::ComplexComponent < ViewComponent::Base
2+
def initialize(name:)
3+
('a'...'z').each do |c|
4+
if rand >= 0.5
5+
instance_variable_set("@rand_var_#{c}", true)
6+
end
7+
end
8+
@name = name
9+
end
10+
end

performance/components/name_component.rb

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
class Performance::NameComponent < ViewComponent::Base
44
def initialize(name:)
5+
('a'...'z').each do |c|
6+
if rand >= 0.0
7+
instance_variable_set("@rand_var_#{c}", true)
8+
end
9+
instance_variable_set("@rand_var_#{c}", true)
10+
end
511
@name = name
612
end
713
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>nested hello <%= @name %></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class Performance::NestedComplexComponent < ViewComponent::Base
4+
def initialize(name:)
5+
('a'...'z').each do |c|
6+
if rand > 0.5
7+
instance_variable_set("@rand_var_#{c}", true)
8+
end
9+
end
10+
@name = name
11+
end
12+
end

performance/components/nested_name_component.rb

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
class Performance::NestedNameComponent < ViewComponent::Base
44
def initialize(name:)
5+
('a'...'z').each do |c|
6+
if rand >= 0.0
7+
instance_variable_set("@rand_var_#{c}", true)
8+
end
9+
end
510
@name = name
611
end
712
end

performance/partial_benchmark.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
require "benchmark/ips"
77

8+
Warning[:performance] = true
9+
810
# Configure Rails Environment
911
ENV["RAILS_ENV"] = "production"
1012
require File.expand_path("../test/sandbox/config/environment.rb", __dir__)
@@ -13,6 +15,8 @@ module Performance
1315
require_relative "components/name_component"
1416
require_relative "components/nested_name_component"
1517
require_relative "components/inline_component"
18+
require_relative "components/complex_component"
19+
require_relative "components/nested_complex_component"
1620
end
1721

1822
class BenchmarksController < ActionController::Base
@@ -21,13 +25,16 @@ class BenchmarksController < ActionController::Base
2125
BenchmarksController.view_paths = [File.expand_path("./views", __dir__)]
2226
controller_view = BenchmarksController.new.view_context
2327

28+
controller_view.render(Performance::ComplexComponent.new(name: "HI there"))
29+
2430
Benchmark.ips do |x|
2531
x.time = 10
2632
x.warmup = 2
2733

2834
x.report("component") { controller_view.render(Performance::NameComponent.new(name: "Fox Mulder")) }
29-
x.report("inline") { controller_view.render(Performance::InlineComponent.new(name: "Fox Mulder")) }
30-
x.report("partial") { controller_view.render("partial", name: "Fox Mulder") }
35+
# x.report("inline") { controller_view.render(Performance::InlineComponent.new(name: "Fox Mulder")) }
36+
# x.report("partial") { controller_view.render("partial", name: "Fox Mulder") }
37+
x.report("complex") { controller_view.render(Performance::ComplexComponent.new(name: "Fox Mulder")) }
3138

3239
x.compare!
3340
end

0 commit comments

Comments
 (0)