Hrb: not as good as I thought

This commit is contained in:
2019-06-18 20:17:37 -04:00
commit 517b911eea
26 changed files with 1152 additions and 0 deletions

44
benchmark/profile.rb Normal file
View File

@@ -0,0 +1,44 @@
# frozen_string_literal: true
require "bundler/inline"
gemfile ENV.key?("INSTALL_GEMS") do
source "https://rubygems.org"
gem "hrb", path: "../"
gem "memory_profiler"
gem "ruby-prof"
gem "stackprof"
end
def run
10_000.times do
Hrb::Template.render_file("../test/fixtures/basic.html.rb", locals: {created: "2019-06-17", numbers: (1..10)})
end
end
def prof
result = RubyProf.profile do
run
end
printer = RubyProf::MultiPrinter.new(result)
printer.print(path: "./profile", profile: "profile")
end
def stackprof
StackProf.run(mode: :wall, out: "./profile/stackprof.dump", interval: 1000) do
run
end
end
def memory_profiler
report = MemoryProfiler.report do
run
end
report.pretty_print
end
memory_profiler
prof
stackprof

78
benchmark/simple.rb Normal file
View File

@@ -0,0 +1,78 @@
# frozen_string_literal: true
require "bundler/inline"
gemfile ENV.key?("INSTALL_GEMS") do
source "https://rubygems.org"
gem "benchmark-ips"
gem "benchmark-memory"
gem "erubi"
gem "markaby"
gem "pry"
gem "rubyoshka"
gem "slim"
gem "tilt"
gem "hrb", path: "../"
end
def render(extension)
Tilt.new(File.join(__dir__, "templates", "simple.#{extension}")).render(nil, created: "2019-06-17", numbers: (1..100))
end
def render_rubyoshka
Rubyoshka.new do
with(created: "2019-06-17", numbers: (1..100)) do
instance_eval(File.read(File.join(__dir__, "templates", "simple.html.rb")))
end
end.render
end
erubi_src = Erubi::Engine.new(File.join(__dir__, "templates", "simple.erb")).src
numbers = (1..100)
created = "2019-06-17"
require "benchmark/ips"
Benchmark.ips do |x|
x.report("Erubi:") do
render("erb")
end
x.report("Erubi fast:") do
eval(erubi_src)
end
x.report("Rubyoshka:") do
render_rubyoshka
end
x.report("Hrb:") do
render("html.rb")
end
x.compare!
end
if ENV.key?("MEMORY")
require "benchmark/memory"
Benchmark.memory do |x|
x.report("Erubi:") do
render("erb")
end
x.report("Erubi fast:") do
eval(erubi_src)
end
x.report("Hrb:") do
render("html.rb")
end
x.report("Rubyoshka:") do
render_rubyoshka
end
x.compare!
end
end

View File

@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello!</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="/test.css">
<meta name="created" content="<%= created %>">
</head>
<body>
<div id="content">
<div class="container">
<% numbers.each do |n| %>
<div>This is div #<%= n %></div>
<% end %>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,23 @@
# frozen_string_literal: true
doctype
html do
head do
title "Hello!"
meta charset: "utf-8"
meta name: "viewport", content: "width=device-width, initial-scale=1"
link rel: "stylesheet", href: "/test.css"
meta name: "created", content: created
end
body do
div(id: "content") do
div(class: "container") do
h1("Hello!")
numbers.each do |i|
div("This is div ##{i}")
end
end
end
end
end

View File

@@ -0,0 +1,24 @@
# frozen_string_literal: true
html5 do
html do
head do
title "Hello!"
meta charset: "utf-8"
meta name: "viewport", content: "width=device-width, initial-scale=1"
link rel: "stylesheet", href: "/test.css"
meta name: "created", content: created
end
body do
div(id: "content") do
div(class: "container") do
h1("Hello!")
numbers.each do |i|
div("This is div ##{i}")
end
end
end
end
end
end

View File

@@ -0,0 +1,14 @@
doctype html
html
head
title Hello!
meta charset="utf-8"
meta name="viewport" content="width=device-width, initial-scale=1"
link rel="stylesheet" href="/test.css"
meta name="created" content=created
body
#content
.container
h1 Hello!
- numbers.each do |i|
div This is div ##{i}