File: slotable_benchmark.rb

package info (click to toggle)
ruby-view-component 2.74.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 3,156 kB
  • sloc: ruby: 6,731; sh: 163; javascript: 10; makefile: 4
file content (84 lines) | stat: -rw-r--r-- 2,030 bytes parent folder | download
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
# frozen_string_literal: true

# Run `bundle exec rake benchmark` to execute benchmark.
# This is very much a work-in-progress. Please feel free to make/suggest improvements!

require "benchmark/ips"

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

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

class BenchmarksController < ActionController::Base
end

BenchmarksController.view_paths = [File.expand_path("./views", __dir__)]
controller_view = BenchmarksController.new.view_context

Benchmark.ips do |x|
  x.time = 10
  x.warmup = 2

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

    controller_view.render(component) do |c|
      c.with(: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 Performance::SlotsV2Component::ItemComponent.new(classes: "header") do
            item
          end
        end
      end
    end
  end

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

    controller_view.render(component) do |c|
      c.slot(:header, classes: "my-header") do
        "Hello world"
      end

      c.slot(:item, classes: "a") do
        "First item"
      end

      c.slot(:item, classes: "b") do
        "Second item"
      end
    end
  end
  x.report("subcomponent:") do
    component = Performance::SlotsV2Component.new(name: "Fox Mulder")

    controller_view.render(component) do |c|
      c.header(classes: "my-header") do
        "Hello world"
      end

      c.item(classes: "a") do
        "First item"
      end

      c.item(classes: "b") do
        "Second item"
      end
    end
  end

  x.compare!
end