Skip to content

Commit

Permalink
Fix benchmark scripts (ViewComponent#1026)
Browse files Browse the repository at this point in the history
Since the test sandbox application was moved, the benchmark scripts have
not been working. This fixes the scripts by requiring files from the new
location, as well as namespacing the components so that the classes do
not conflict with components defined in the sandbox.
  • Loading branch information
andrewjtait authored Aug 3, 2021
1 parent 863d85f commit 3f129ce
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 29 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ title: Changelog

*Ryo.gift*

* Fix benchmark scripts.

*Andrew Tait*

## 2.36.0

* Add `slot_type` helper method.
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ ViewComponent is built by over a hundred members of the community, including:
<img src="https://avatars.githubusercontent.com/matheusrich?s=64" alt="matheusrich" width="32" />
<img src="https://avatars.githubusercontent.com/Matt-Yorkley?s=64" alt="Matt-Yorkley" width="32" />
<img src="https://avatars.githubusercontent.com/ryogift?s=64" alt="ryogift" width="32" />
<img src="https://avatars.githubusercontent.com/andrewjtait?s=64" alt="andrewjtait" width="32" />

<hr />

Expand Down
10 changes: 6 additions & 4 deletions performance/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

require "benchmark/ips"

# Configure Rails Envinronment
# Configure Rails Environment
ENV["RAILS_ENV"] = "production"
require File.expand_path("../test/config/environment.rb", __dir__)
require File.expand_path("../test/sandbox/config/environment.rb", __dir__)

require_relative "components/name_component.rb"
module Performance
require_relative "components/name_component.rb"
end

class BenchmarksController < ActionController::Base
end
Expand All @@ -21,7 +23,7 @@ class BenchmarksController < ActionController::Base
x.time = 10
x.warmup = 2

x.report("component:") { controller_view.render(NameComponent.new(name: "Fox Mulder")) }
x.report("component:") { controller_view.render(Performance::NameComponent.new(name: "Fox Mulder")) }
x.report("partial:") { controller_view.render("partial", name: "Fox Mulder") }

x.compare!
Expand Down
2 changes: 1 addition & 1 deletion performance/components/content_areas_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class ContentAreasComponent < ViewComponent::Base
class Performance::ContentAreasComponent < ViewComponent::Base
with_content_areas :header, :items
end
2 changes: 1 addition & 1 deletion performance/components/global_i18n_component.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class GlobalI18nComponent < ViewComponent::Base
class Performance::GlobalI18nComponent < ViewComponent::Base
def initialize(key)
@key = key
end
Expand Down
2 changes: 1 addition & 1 deletion performance/components/name_component.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class NameComponent < ViewComponent::Base
class Performance::NameComponent < ViewComponent::Base
def initialize(name:)
@name = name
end
Expand Down
4 changes: 2 additions & 2 deletions performance/components/slot_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<header>
<%= render SlotsV2Component::HeaderComponent.new(classes: header.classes) do %>
<%= render Performance::SlotsV2Component::HeaderComponent.new(classes: header.classes) do %>
<%= header.content %>
<% end %>
</header>

<%= items.each do |item| %>
<div>
<%= render SlotsV2Component::ItemComponent.new(classes: item.classes) do %>
<%= render Performance::SlotsV2Component::ItemComponent.new(classes: item.classes) do %>
<%= item.content %>
<% end %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion performance/components/slot_component.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class SlotComponent < ViewComponent::Base
class Performance::SlotComponent < ViewComponent::Base
include ViewComponent::Slotable

with_slot :header, class_name: "Header"
Expand Down
2 changes: 1 addition & 1 deletion performance/components/slots_v2_component.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class SlotsV2Component < ViewComponent::Base
class Performance::SlotsV2Component < ViewComponent::Base
renders_one :header, ->(**kwargs) { HeaderComponent.new(**kwargs) }
renders_many :items, ->(**kwargs) { ItemComponent.new(**kwargs) }

Expand Down
2 changes: 1 addition & 1 deletion performance/components/translatable_component.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class TranslatableComponent < ViewComponent::Base
class Performance::TranslatableComponent < ViewComponent::Base
include ViewComponent::Translatable

def initialize(key)
Expand Down
22 changes: 12 additions & 10 deletions performance/slotable_benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

require "benchmark/ips"

# Configure Rails Envinronment
# Configure Rails Environment
ENV["RAILS_ENV"] = "production"
require File.expand_path("../test/config/environment.rb", __dir__)
require File.expand_path("../test/sandbox/config/environment.rb", __dir__)

require_relative "components/slot_component.rb"
require_relative "components/slots_v2_component.rb"
require_relative "components/content_areas_component.rb"
module Performance
require_relative "components/slot_component.rb"
require_relative "components/slots_v2_component.rb"
require_relative "components/content_areas_component.rb"
end

class BenchmarksController < ActionController::Base
end
Expand All @@ -24,18 +26,18 @@ class BenchmarksController < ActionController::Base
x.warmup = 2

x.report("content_areas:") do
component = ContentAreasComponent.new(name: "Fox Mulder")
component = Performance::ContentAreasComponent.new(name: "Fox Mulder")

controller_view.render(component) do |c|
c.with(:header) do
c.render SlotsV2Component::HeaderComponent.new(classes: "header") do
c.render Performance::SlotsV2Component::HeaderComponent.new(classes: "header") do
"Header"
end
end

c.with(:items) do
["a", "b", "c"].each do |item|
c.render SlotsV2Component::ItemComponent.new(classes: "header") do
c.render Performance::SlotsV2Component::ItemComponent.new(classes: "header") do
item
end
end
Expand All @@ -44,7 +46,7 @@ class BenchmarksController < ActionController::Base
end

x.report("slot:") do
component = SlotComponent.new(name: "Fox Mulder")
component = Performance::SlotComponent.new(name: "Fox Mulder")

controller_view.render(component) do |c|
c.slot(:header, classes: "my-header") do
Expand All @@ -61,7 +63,7 @@ class BenchmarksController < ActionController::Base
end
end
x.report("subcomponent:") do
component = SlotsV2Component.new(name: "Fox Mulder")
component = Performance::SlotsV2Component.new(name: "Fox Mulder")

controller_view.render(component) do |c|
c.header(classes: "my-header") do
Expand Down
16 changes: 9 additions & 7 deletions performance/translatable_benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@

require "benchmark/ips"

# Configure Rails Envinronment
# Configure Rails Environment
ENV["RAILS_ENV"] = "production"
require File.expand_path("../test/config/environment.rb", __dir__)
require File.expand_path("../test/sandbox/config/environment.rb", __dir__)

require_relative "components/global_i18n_component.rb"
require_relative "components/translatable_component.rb"
module Performance
require_relative "components/global_i18n_component.rb"
require_relative "components/translatable_component.rb"
end

class BenchmarksController < ActionController::Base
end

BenchmarksController.view_paths = [File.expand_path("./views", __dir__)]
controller_view = BenchmarksController.new.view_context
I18n.load_path = Dir[File.expand_path("../test/config/locales/*.{rb,yml,yaml}", __dir__)]
I18n.load_path = Dir[File.expand_path("../test/sandbox/config/locales/*.{rb,yml,yaml}", __dir__)]
I18n.backend.load_translations

global_i18n_component = GlobalI18nComponent.new("hello")
translatable_component = TranslatableComponent.new(".hello")
global_i18n_component = Performance::GlobalI18nComponent.new("hello")
translatable_component = Performance::TranslatableComponent.new(".hello")

controller_view.render(global_i18n_component)
controller_view.render(translatable_component)
Expand Down

0 comments on commit 3f129ce

Please sign in to comment.