From 03b1596e0c9abd4db352cfecf57069677039411a Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Tue, 10 Sep 2024 09:54:13 -0300 Subject: [PATCH] config: Improve benchmark code --- benchmark/benchmark.cr | 19 +++++++ benchmark/{main.cr => blueprint_html.cr} | 22 ++------ benchmark/blueprint_raw_html.cr | 65 ++++++++++++++++++++++++ benchmark/ecr.cr | 10 ++++ benchmark/ecr_template.ecr | 1 + 5 files changed, 98 insertions(+), 19 deletions(-) create mode 100644 benchmark/benchmark.cr rename benchmark/{main.cr => blueprint_html.cr} (77%) create mode 100644 benchmark/blueprint_raw_html.cr create mode 100644 benchmark/ecr.cr create mode 100644 benchmark/ecr_template.ecr diff --git a/benchmark/benchmark.cr b/benchmark/benchmark.cr new file mode 100644 index 0000000..736ef7d --- /dev/null +++ b/benchmark/benchmark.cr @@ -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 diff --git a/benchmark/main.cr b/benchmark/blueprint_html.cr similarity index 77% rename from benchmark/main.cr rename to benchmark/blueprint_html.cr index 78fc4ab..ae6c0da 100644 --- a/benchmark/main.cr +++ b/benchmark/blueprint_html.cr @@ -1,7 +1,6 @@ -require "benchmark" require "../src/blueprint/html" -class Example::LayoutComponent +class BlueprintHTML::LayoutComponent include Blueprint::HTML def initialize(@title = "Example"); end @@ -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 @@ -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 diff --git a/benchmark/blueprint_raw_html.cr b/benchmark/blueprint_raw_html.cr new file mode 100644 index 0000000..18bd228 --- /dev/null +++ b/benchmark/blueprint_raw_html.cr @@ -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 diff --git a/benchmark/ecr.cr b/benchmark/ecr.cr new file mode 100644 index 0000000..5d60dc6 --- /dev/null +++ b/benchmark/ecr.cr @@ -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 diff --git a/benchmark/ecr_template.ecr b/benchmark/ecr_template.ecr new file mode 100644 index 0000000..ba9f8d6 --- /dev/null +++ b/benchmark/ecr_template.ecr @@ -0,0 +1 @@ +
Hi | Hi | Hi | Hi | Hi |