Skip to content

Commit

Permalink
config: Improve benchmark code
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv committed Sep 10, 2024
1 parent 14e0c64 commit 03b1596
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 19 deletions.
19 changes: 19 additions & 0 deletions benchmark/benchmark.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "benchmark"

require "./blueprint_html"
require "./blueprint_raw_html"
require "./ecr"

require "../src/blueprint/version"

blueprint_html = BlueprintHTML::Page.new.to_html
blueprint_raw_html = BlueprintRawHTML::Page.new.to_html
ecr = ECR::Page.new.to_s # ECR is here just to have a base value to compare

raise "Different results" if blueprint_html != blueprint_raw_html && blueprint_html != ecr

Benchmark.ips do |x|
x.report("Blueprint::HTML #{Blueprint::VERSION}") { BlueprintHTML::Page.new.to_html }
x.report("Blueprint::RawHTML #{Blueprint::VERSION}") { BlueprintRawHTML::Page.new.to_html }
x.report("ECR") { ECR::Page.new.to_s }
end
22 changes: 3 additions & 19 deletions benchmark/main.cr → benchmark/blueprint_html.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "benchmark"
require "../src/blueprint/html"

class Example::LayoutComponent
class BlueprintHTML::LayoutComponent
include Blueprint::HTML

def initialize(@title = "Example"); end
Expand Down Expand Up @@ -31,11 +30,11 @@ class Example::LayoutComponent
end
end

class Example::Page
class BlueprintHTML::Page
include Blueprint::HTML

def blueprint
render Example::LayoutComponent.new do
render BlueprintHTML::LayoutComponent.new do
h1 "Hi"

table id: "test", class: "a b c d e f g" do
Expand Down Expand Up @@ -64,18 +63,3 @@ class Example::Page
end
end
end

a = Example::Page.new.to_html
b = Example::Page.new.to_html

raise "Different results" unless a == b

puts(
Benchmark.measure do
300000.times { Example::Page.new.to_html }
end
)

Benchmark.ips do |x|
x.report("Page") { Example::Page.new.to_html }
end
65 changes: 65 additions & 0 deletions benchmark/blueprint_raw_html.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "../src/blueprint/raw_html"

class BlueprintRawHTML::LayoutComponent
include Blueprint::RawHTML

def initialize(@title = "Example"); end

def blueprint(&)
html do
head do
title @title
meta name: "viewport", content: "width=device-width,initial-scale=1"
link href: "/assets/tailwind.css", rel: "stylesheet"
end

body class: "bg-zinc-100" do
nav class: "p-5", id: "main_nav" do
ul do
li(class: "p-5") { a("Home", href: "/") }
li(class: "p-5") { a("About", href: "/about") }
li(class: "p-5") { a("Contact", href: "/contact") }
end
end

div class: "container mx-auto p-5" do
yield
end
end
end
end
end

class BlueprintRawHTML::Page
include Blueprint::RawHTML

def blueprint
render BlueprintRawHTML::LayoutComponent.new do
h1 "Hi"

table id: "test", class: "a b c d e f g" do
tr do
td id: "test", class: "a b c d e f g" do
span "Hi"
end

td id: "test", class: "a b c d e f g" do
span "Hi"
end

td id: "test", class: "a b c d e f g" do
span "Hi"
end

td id: "test", class: "a b c d e f g" do
span "Hi"
end

td id: "test", class: "a b c d e f g" do
span "Hi"
end
end
end
end
end
end
10 changes: 10 additions & 0 deletions benchmark/ecr.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "ecr"

module ECR
class Page
def initialize(@title = "Example")
end

ECR.def_to_s("./benchmark/ecr_template.ecr")
end
end
1 change: 1 addition & 0 deletions benchmark/ecr_template.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head><title><%= @title %></title><meta name="viewport" content="width=device-width,initial-scale=1"><link href="/assets/tailwind.css" rel="stylesheet"></head><body class="bg-zinc-100"><nav class="p-5" id="main_nav"><ul><li class="p-5"><a href="/">Home</a></li><li class="p-5"><a href="/about">About</a></li><li class="p-5"><a href="/contact">Contact</a></li></ul></nav><div class="container mx-auto p-5"><h1>Hi</h1><table id="test" class="a b c d e f g"><tr><td id="test" class="a b c d e f g"><span>Hi</span></td><td id="test" class="a b c d e f g"><span>Hi</span></td><td id="test" class="a b c d e f g"><span>Hi</span></td><td id="test" class="a b c d e f g"><span>Hi</span></td><td id="test" class="a b c d e f g"><span>Hi</span></td></tr></table></div></body></html>

0 comments on commit 03b1596

Please sign in to comment.