From 537f2d483f050fd077474c3bbd619b0ecdd2448a Mon Sep 17 00:00:00 2001 From: Evan Walsh Date: Mon, 15 Apr 2019 19:48:03 -0400 Subject: [PATCH] Init --- README.md | 13 ++++++ markup.rb | 62 ++++++++++++++++++++++++++ markup/asciidoc.adoc | 5 +++ markup/creole.creole | 5 +++ markup/markdown.md | 5 +++ markup/markup.txt | 5 +++ markup/textile.textile | 5 +++ markup/wikitext.mediawiki | 5 +++ serialization.rb | 91 +++++++++++++++++++++++++++++++++++++++ templates/erubi.erb | 20 +++++++++ templates/fortitude.rb | 29 +++++++++++++ templates/haml.haml | 16 +++++++ templates/handlebars.hbs | 23 ++++++++++ templates/liquid.liquid | 20 +++++++++ templates/markaby.mab | 19 ++++++++ templates/slim.slim | 13 ++++++ templates/template.txt | 7 +++ templating.rb | 84 ++++++++++++++++++++++++++++++++++++ 18 files changed, 427 insertions(+) create mode 100644 README.md create mode 100644 markup.rb create mode 100644 markup/asciidoc.adoc create mode 100644 markup/creole.creole create mode 100644 markup/markdown.md create mode 100644 markup/markup.txt create mode 100644 markup/textile.textile create mode 100644 markup/wikitext.mediawiki create mode 100644 serialization.rb create mode 100644 templates/erubi.erb create mode 100644 templates/fortitude.rb create mode 100644 templates/haml.haml create mode 100644 templates/handlebars.hbs create mode 100644 templates/liquid.liquid create mode 100644 templates/markaby.mab create mode 100644 templates/slim.slim create mode 100644 templates/template.txt create mode 100644 templating.rb diff --git a/README.md b/README.md new file mode 100644 index 0000000..791b070 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Quick & Dirty Ruby Benchmarks + +Just some fun little benchmarks I did with the help of [benchmark-ips](https://github.com/evanphx/benchmark-ips). + +## Usage + +Run the `.rb` files with `INSTALL_GEMS=1` in your environment to make sure you install all the dependencies. + +### Example + +```bash +env INSTALL_GEMS=1 ruby markup.rb +``` diff --git a/markup.rb b/markup.rb new file mode 100644 index 0000000..e6b3454 --- /dev/null +++ b/markup.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require 'bundler/inline' + +gemfile ENV.has_key?('INSTALL_GEMS') do + source 'https://rubygems.org' + gem 'benchmark-ips' + gem 'tilt' + gem 'kramdown' + gem 'rdiscount' + gem 'redcarpet' + gem 'bluecloth' + gem 'maruku' + gem 'commonmarker' + gem 'RedCloth' + gem 'creole' + gem 'rinku' + gem 'wikicloth' + gem 'asciidoctor' +end + +require 'benchmark/ips' + +Benchmark.ips do |x| + x.report('Kramdown:') do + Tilt::KramdownTemplate.new('markup/markdown.md').render + end + + x.report('Redcarpet:') do + Tilt::RedcarpetTemplate.new('markup/markdown.md').render + end + + x.report('BlueCloth:') do + Tilt::BlueClothTemplate.new('markup/markdown.md').render + end + + x.report('Maruku:') do + Tilt::MarukuTemplate.new('markup/markdown.md').render + end + + x.report('CommonMarker:') do + Tilt::CommonMarkerTemplate.new('markup/markdown.md').render + end + + x.report('RedCloth:') do + Tilt.new('markup/textile.textile').render + end + + x.report('Creole:') do + Tilt.new('markup/creole.creole').render + end + + x.report('WikiText:') do + Tilt.new('markup/wikitext.mediawiki').render + end + + x.report('Asciidoctor:') do + Tilt.new('markup/asciidoc.adoc').render + end + + x.compare! +end diff --git a/markup/asciidoc.adoc b/markup/asciidoc.adoc new file mode 100644 index 0000000..d6916e4 --- /dev/null +++ b/markup/asciidoc.adoc @@ -0,0 +1,5 @@ += Hello from Markdown! + +**This is bold** and _this is italic_. **This is _bold and italic_**. + +Another paragraph is finally here. http://evanwalsh.net[Here's a link]. diff --git a/markup/creole.creole b/markup/creole.creole new file mode 100644 index 0000000..6731802 --- /dev/null +++ b/markup/creole.creole @@ -0,0 +1,5 @@ += Hello from LIBRARY! + +**This is bold** and //this is italic//. **This is //bold and italic//**. + +Another paragraph is finally here. [[Here's a link|http://evanwalsh.net]]. diff --git a/markup/markdown.md b/markup/markdown.md new file mode 100644 index 0000000..ef94f91 --- /dev/null +++ b/markup/markdown.md @@ -0,0 +1,5 @@ +# Hello from Markdown! + +**This is bold** and _this is italic_. **This is _bold and italic_**. + +Another paragraph is finally here. [Here's a link](http://evanwalsh.net). diff --git a/markup/markup.txt b/markup/markup.txt new file mode 100644 index 0000000..74fef25 --- /dev/null +++ b/markup/markup.txt @@ -0,0 +1,5 @@ +Hello from LIBRARY! + +**This is bold** and _this is italic_. **This is _bold and italic_**. + +Another paragraph is finally here. [Here's a link](http://evanwalsh.net). diff --git a/markup/textile.textile b/markup/textile.textile new file mode 100644 index 0000000..23ed150 --- /dev/null +++ b/markup/textile.textile @@ -0,0 +1,5 @@ +h1. Hello from Markdown! + +*This is bold* and _this is italic_. *This is _bold and italic_*. + +Another paragraph is finally here. "Here's a link":http://evanwalsh.net. diff --git a/markup/wikitext.mediawiki b/markup/wikitext.mediawiki new file mode 100644 index 0000000..b688be0 --- /dev/null +++ b/markup/wikitext.mediawiki @@ -0,0 +1,5 @@ += Hello from WikiText! + +'''This is bold''' and ''this is italic'''. '''This is ''bold and italic'''''. + +Another paragraph is finally here. [[Here's a link|http://evanwalsh.net]]. diff --git a/serialization.rb b/serialization.rb new file mode 100644 index 0000000..3a0c3d3 --- /dev/null +++ b/serialization.rb @@ -0,0 +1,91 @@ +# frozen_string_literal: true + +require 'bundler/inline' + +gemfile ENV.key?('INSTALL_GEMS') do + source 'https://rubygems.org' + gem 'benchmark-ips' + gem 'bson' + gem 'msgpack' + gem 'cbor' +end + +require 'json' +require 'yaml' +require 'benchmark/ips' + +data = { + account_id: 1, + type: 'artist_update', + subject: { + id: 1, + type: 'Artist' + }, + author: { + id: 1, + type: 'Account' + } +} + +json = JSON.generate(data) +bson = data.to_bson +msgpack = MessagePack.pack(data) +cbor = data.to_cbor +yaml = YAML.dump data + +puts "JSON (#{json.bytesize} bytes): #{json}" +puts "MessagePack (#{msgpack.bytesize} bytes): #{msgpack}" +puts "BSON (#{bson.to_s.bytesize} bytes): #{bson}" +puts "CBOR (#{cbor.to_s.bytesize} byes): #{cbor}" +puts "YAML (#{yaml.bytesize} bytes): #{yaml}" + +puts "\nBenchmarks" + +Benchmark.ips do |x| + x.report('json ->') do + JSON.generate(data) + end + + x.report('msgpack ->') do + MessagePack.pack(data) + end + + x.report('bson ->') do + data.to_bson + end + + x.report('cbor ->') do + data.to_cbor + end + + x.report('yaml ->') do + YAML.dump(data) + end + + x.compare! +end + +Benchmark.ips do |x| + x.report('<- json') do + JSON.parse(json) + end + + x.report('<- msgpack') do + MessagePack.unpack(msgpack) + end + + x.report('<- bson') do + bson.rewind! + Hash.from_bson(bson) + end + + x.report('<- cbor') do + CBOR.decode(cbor) + end + + x.report('<-- yaml') do + YAML.load(yaml) + end + + x.compare! +end diff --git a/templates/erubi.erb b/templates/erubi.erb new file mode 100644 index 0000000..1317116 --- /dev/null +++ b/templates/erubi.erb @@ -0,0 +1,20 @@ + + + + Hello from Erubi! + + + + + + + +
+
+ <% 100.times do |i| %> +
This is div #<%= i %>
+ <% end %> +
+
+ + diff --git a/templates/fortitude.rb b/templates/fortitude.rb new file mode 100644 index 0000000..8abac8c --- /dev/null +++ b/templates/fortitude.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +class TestView < Fortitude::Widget + doctype :html5 + + def content + doctype! + + html do + head do + title 'Hello from Fortitude' + meta charset: 'utf-8' + meta name: 'viewport', content: 'width=device-width, initial-scale=1' + link rel: 'stylesheet', href: '/test.css' + meta name: 'current_time', content: Time.now.to_s + end + + body do + div(id: 'content') do + div(class: 'container') do + 100.times do |i| + div("This is div ##{i}") + end + end + end + end + end + end +end diff --git a/templates/haml.haml b/templates/haml.haml new file mode 100644 index 0000000..ee4cbce --- /dev/null +++ b/templates/haml.haml @@ -0,0 +1,16 @@ +!!! +%html{lang: "en"} + %head + %meta{content: "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/ + %title Hello from Erubi! + %meta{charset: "UTF-8"}/ + %meta{content: "width=device-width, initial-scale=1.0", name: "viewport"}/ + %meta{content: "ie=edge", "http-equiv" => "X-UA-Compatible"}/ + %link{href: "/test.css", rel: "stylesheet"}/ + %meta{content: Time.now.to_s, name: "current_time"}/ + %body + #content + .container + - 100.times do |i| + %div + This is div ##{i} diff --git a/templates/handlebars.hbs b/templates/handlebars.hbs new file mode 100644 index 0000000..87f9572 --- /dev/null +++ b/templates/handlebars.hbs @@ -0,0 +1,23 @@ + + + + + Hello from Liquid! + + + + + + + + +
+
+ {{#each number}} +
This is div #{{@index}}
+ {{/each}} +
+
+ + + diff --git a/templates/liquid.liquid b/templates/liquid.liquid new file mode 100644 index 0000000..1a0acf8 --- /dev/null +++ b/templates/liquid.liquid @@ -0,0 +1,20 @@ + + + + Hello from Liquid! + + + + + + + +
+
+ {% for i in (0..100) %} +
This is div #{{ i }}
+ {% endfor %} +
+
+ + diff --git a/templates/markaby.mab b/templates/markaby.mab new file mode 100644 index 0000000..4b8f760 --- /dev/null +++ b/templates/markaby.mab @@ -0,0 +1,19 @@ +html do + head do + title 'Hello from Markaby' + meta charset: 'utf-8' + meta name: 'viewport', content: 'width=device-width, initial-scale=1' + link rel: 'stylesheet', href: '/test.css' + meta name: 'current_time', content: Time.now.to_s + end + + body do + div(id: 'content') do + div(class: 'container') do + 100.times do |i| + div("This is div ##{i}") + end + end + end + end +end diff --git a/templates/slim.slim b/templates/slim.slim new file mode 100644 index 0000000..e92d0b7 --- /dev/null +++ b/templates/slim.slim @@ -0,0 +1,13 @@ +doctype html +html + head + title Hello from Slim! + meta charset="utf-8" + meta name="viewport" content="width=device-width, initial-scale=1" + link rel="stylesheet" href="/test.css" + meta name="current_time" content=Time.now.to_s + body + #content + .container + - 100.times do |i| + div This is div ##{i} diff --git a/templates/template.txt b/templates/template.txt new file mode 100644 index 0000000..b2f5118 --- /dev/null +++ b/templates/template.txt @@ -0,0 +1,7 @@ + user system total real +Slim: 3.180568 0.027484 3.208052 ( 3.209399) +Markaby: 1.742859 0.023275 1.766134 ( 1.773957) +Erubi: 0.337117 0.022639 0.359756 ( 0.367103) +HAML: 4.148871 0.031099 4.179970 ( 4.188886) + +--- diff --git a/templating.rb b/templating.rb new file mode 100644 index 0000000..4340049 --- /dev/null +++ b/templating.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require 'bundler/inline' + +gemfile ENV.has_key?('INSTALL_GEMS') do + source 'https://rubygems.org' + gem 'benchmark-ips' + gem 'erubi' + gem 'fortitude' + gem 'haml' + gem 'markaby' + gem 'slim' + gem 'tilt' + gem 'arbre' + gem 'liquid' + gem 'tilt-handlebars' +end + +require 'benchmark/ips' + +Benchmark.ips do |x| + x.report('Slim:') do + Tilt.new('templates/slim.slim').render + end + + # x.report('Fortitude:') do + # Tilt.new('templates/fortitude.rb').render + # end + + # x.report('Arbre:') do + # view = Arbre::Context.new do + # html do + # head do + # title 'Hello from Arbre' + # meta charset: 'utf-8' + # meta name: 'viewport', content: 'width=device-width, initial-scale=1' + # link rel: 'stylesheet', href: '/test.css' + # meta name: 'current_time', content: Time.now.to_s + # end + + # body do + # div(id: 'content') do + # div(class: 'container') do + # 100.times do |i| + # div("This is div ##{i}") + # end + # end + # end + # end + # end + # end + + # view.to_s + # end + + x.report('Markaby:') do + Tilt.new('templates/markaby.mab').render + end + + x.report('Erubi:') do + Tilt.new('templates/erubi.erb').render + end + + x.report('ERB:') do + Tilt::ERBTemplate.new('templates/erubi.erb').render + end + + x.report('HAML:') do + Tilt.new('templates/haml.haml').render + end + + x.report('Liquid:') do + Tilt.new('templates/liquid.liquid').render + end + + x.report('Handlebars:') do + Tilt.new('templates/handlebars.hbs', { + time: Time.now.to_s, + numbers: (0..100).to_a + }) + end + + x.compare! +end