Tidy all benchmarks and a tilt comparison (#211)
This commit is contained in:
parent
136ebb5401
commit
e46dc201f8
|
@ -1,64 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "pathname"
|
||||
require "ostruct"
|
||||
require "benchmark/ips"
|
||||
require "hanami/view"
|
||||
require "action_view"
|
||||
require "action_controller"
|
||||
|
||||
TEMPLATES_PATHS = Pathname(__FILE__).dirname.join("templates")
|
||||
|
||||
TEMPLATE_LOCALS = {
|
||||
users: [
|
||||
OpenStruct.new(name: "Jane", email: "Jane@example.com"),
|
||||
OpenStruct.new(name: "Teresa", email: "teresa@example.com")
|
||||
]
|
||||
}.freeze
|
||||
|
||||
ActionController::Base.view_paths = TEMPLATES_PATHS
|
||||
|
||||
class UsersController < ActionController::Base
|
||||
layout "app"
|
||||
|
||||
attr_reader :users
|
||||
|
||||
def index
|
||||
@users = TEMPLATE_LOCALS[:users]
|
||||
render_to_string :index
|
||||
end
|
||||
end
|
||||
|
||||
class HanamiView < Hanami::View
|
||||
config.paths = TEMPLATES_PATHS
|
||||
config.layout = "app"
|
||||
config.template = "users"
|
||||
config.default_format = :html
|
||||
|
||||
expose :users
|
||||
end
|
||||
|
||||
action_controller = UsersController.new
|
||||
hanami_view = HanamiView.new
|
||||
|
||||
action_controller_output = action_controller.index
|
||||
hanami_view_output = hanami_view.(TEMPLATE_LOCALS).to_s
|
||||
|
||||
if action_controller_output != hanami_view_output
|
||||
puts "Output doesn't match:"
|
||||
puts
|
||||
puts "ActionView:\n\n#{action_controller_output}\n"
|
||||
puts "hanami-view:\n\n#{hanami_view_output}\n"
|
||||
end
|
||||
|
||||
Benchmark.ips do |x|
|
||||
x.report("action_controller") do
|
||||
1000.times { action_controller.index }
|
||||
end
|
||||
|
||||
x.report("hanami-view") do
|
||||
1000.times { hanami_view.(TEMPLATE_LOCALS).to_s }
|
||||
end
|
||||
|
||||
x.compare!
|
||||
end
|
|
@ -0,0 +1,34 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "hanami/view"
|
||||
require "ostruct"
|
||||
|
||||
module Benchmarks
|
||||
module Comparative
|
||||
module Hanami
|
||||
class View < ::Hanami::View
|
||||
config.paths = File.expand_path(File.join(__dir__, "hanami", "templates"))
|
||||
config.layout = "app"
|
||||
config.template = "users"
|
||||
config.default_format = :html
|
||||
|
||||
expose :users
|
||||
end
|
||||
|
||||
LOCALS = {
|
||||
users: [
|
||||
OpenStruct.new(name: "Jane", email: "jane@example.com"),
|
||||
OpenStruct.new(name: "Teresa", email: "teresa@example.com")
|
||||
]
|
||||
}.freeze
|
||||
|
||||
def self.prepare
|
||||
@view = View.new
|
||||
end
|
||||
|
||||
def self.run
|
||||
@view.call(**LOCALS).to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
<html>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
|
@ -1,4 +1,4 @@
|
|||
<% users.each_with_index do |user, index| %>
|
||||
<h1><%= user.name %></h1>
|
||||
<%= render 'button', user_link: user.email, index: index + 1 %>
|
||||
<%= render "button", user_link: user.email, index: index + 1 %>
|
||||
<% end %>
|
|
@ -0,0 +1,37 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "action_view"
|
||||
require "action_controller"
|
||||
require "ostruct"
|
||||
|
||||
module Benchmarks
|
||||
module Comparative
|
||||
module Rails
|
||||
class UsersController < ActionController::Base
|
||||
self.view_paths = File.expand_path(File.join(__dir__, "rails", "templates"))
|
||||
|
||||
layout "app"
|
||||
|
||||
def index
|
||||
@users = LOCALS[:users]
|
||||
render_to_string "users/index"
|
||||
end
|
||||
end
|
||||
|
||||
LOCALS = {
|
||||
users: [
|
||||
OpenStruct.new(name: "Jane", email: "jane@example.com"),
|
||||
OpenStruct.new(name: "Teresa", email: "teresa@example.com")
|
||||
]
|
||||
}.freeze
|
||||
|
||||
def self.prepare
|
||||
@controller = UsersController.new
|
||||
end
|
||||
|
||||
def self.run
|
||||
@controller.index
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
<html>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
|
@ -1,4 +1,4 @@
|
|||
<% @users.each_with_index do |user, index| %>
|
||||
<h1><%= user.name %></h1>
|
||||
<%= render 'button', { user_link: user.email, index: index + 1 } %>
|
||||
<%= render "/button", { user_link: user.email, index: index + 1 } %>
|
||||
<% end %>
|
|
@ -0,0 +1,47 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "tilt"
|
||||
require "ostruct"
|
||||
|
||||
module Benchmarks
|
||||
module Comparative
|
||||
module Tilt
|
||||
LOCALS = {
|
||||
users: [
|
||||
OpenStruct.new(name: "Jane", email: "jane@example.com"),
|
||||
OpenStruct.new(name: "Teresa", email: "teresa@example.com")
|
||||
]
|
||||
}.freeze
|
||||
|
||||
TEMPLATES_PATH = File.expand_path(File.join(__dir__, "tilt", "templates")).freeze
|
||||
|
||||
class Scope
|
||||
def initialize
|
||||
@templates = {}
|
||||
end
|
||||
|
||||
def render(partial_name, **locals)
|
||||
_partial_template(partial_name).render(self, locals)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def _partial_template(partial_name)
|
||||
@templates.fetch(partial_name) {
|
||||
@templates[partial_name] = ::Tilt.new(File.join(TEMPLATES_PATH, "_#{partial_name}.html.erb"))
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def self.prepare
|
||||
@scope = Scope.new
|
||||
@layout = ::Tilt.new(File.join(TEMPLATES_PATH, "app.html.erb"))
|
||||
@template = ::Tilt.new(File.join(TEMPLATES_PATH, "users.html.erb"))
|
||||
end
|
||||
|
||||
def self.run
|
||||
@layout.render(@scope) { @template.render(@scope, LOCALS) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1 @@
|
|||
<a href="<%= user_link %>">"User <%= index %>"</a>
|
|
@ -0,0 +1,5 @@
|
|||
<html>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,4 @@
|
|||
<% users.each_with_index do |user, index| %>
|
||||
<h1><%= user.name %></h1>
|
||||
<%= render "button", user_link: user.email, index: index + 1 %>
|
||||
<% end %>
|
|
@ -0,0 +1,45 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "benchmark/ips"
|
||||
require_relative "comparative/hanami"
|
||||
require_relative "comparative/rails"
|
||||
require_relative "comparative/tilt"
|
||||
|
||||
Benchmarks::Comparative::Hanami.prepare
|
||||
Benchmarks::Comparative::Rails.prepare
|
||||
Benchmarks::Comparative::Tilt.prepare
|
||||
|
||||
def normalize(str)
|
||||
str.gsub(/\s+/, " ")
|
||||
end
|
||||
|
||||
outputs = {
|
||||
hanami: Benchmarks::Comparative::Hanami.run,
|
||||
rails: Benchmarks::Comparative::Rails.run,
|
||||
tilt: Benchmarks::Comparative::Tilt.run,
|
||||
}
|
||||
|
||||
if outputs.values.map { normalize(_1) }.uniq.size > 1
|
||||
puts "Outputs do not match\n"
|
||||
|
||||
outputs.each do |system, output|
|
||||
puts "#{system}:"
|
||||
puts normalize(output)
|
||||
end
|
||||
end
|
||||
|
||||
Benchmark.ips do |x|
|
||||
x.report("hanami/view") do
|
||||
Benchmarks::Comparative::Hanami.run
|
||||
end
|
||||
|
||||
x.report("rails") do
|
||||
Benchmarks::Comparative::Hanami.run
|
||||
end
|
||||
|
||||
x.report("tilt") do
|
||||
Benchmarks::Comparative::Tilt.run
|
||||
end
|
||||
|
||||
x.compare!
|
||||
end
|
|
@ -1,29 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "hotch"
|
||||
require "pathname"
|
||||
require "ostruct"
|
||||
require "hanami/view"
|
||||
|
||||
TEMPLATES_PATHS = Pathname(__FILE__).dirname.join("templates")
|
||||
|
||||
TEMPLATE_LOCALS = {
|
||||
users: [
|
||||
OpenStruct.new(name: "Jane", email: "Jane@example.com"),
|
||||
OpenStruct.new(name: "Teresa", email: "teresa@example.com")
|
||||
]
|
||||
}.freeze
|
||||
|
||||
class View < Hanami::View
|
||||
config.paths = TEMPLATES_PATHS
|
||||
config.layout = "app"
|
||||
config.template = "users"
|
||||
|
||||
expose :users
|
||||
end
|
||||
|
||||
view = View.new
|
||||
|
||||
Hotch(filter: /View/) do
|
||||
100.times { view.(**TEMPLATE_LOCALS).to_s }
|
||||
end
|
|
@ -1,8 +0,0 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Enter Your Code Here</title>
|
||||
</head>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,14 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "benchmark/ips"
|
||||
require_relative "comparative/hanami"
|
||||
|
||||
Benchmarks::Comparative::Hanami.prepare
|
||||
|
||||
Benchmark.ips do |x|
|
||||
x.report("hanami/view") do
|
||||
Benchmarks::Comparative::Hanami.run
|
||||
end
|
||||
|
||||
x.compare!
|
||||
end
|
|
@ -0,0 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "hotch"
|
||||
require_relative "comparative/hanami"
|
||||
|
||||
Benchmarks::Comparative::Hanami.prepare
|
||||
|
||||
Hotch(filter: /Hanami::View/) do
|
||||
1000.times { Benchmarks::Comparative::Hanami.run }
|
||||
end
|
Loading…
Reference in New Issue