opulent 1.7.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e4725037738209f23895d883b56c94866adbb606
4
- data.tar.gz: 33953652ff3ff0a690164bf426eb9f201ab7eeff
3
+ metadata.gz: 7441c9029694ce515a43f6ad45a35bab1b9b7044
4
+ data.tar.gz: 1886d078184cca281cf0cbaaed59b3fb0e5b54ec
5
5
  SHA512:
6
- metadata.gz: eb6aecbe587a2507ed10b93e555d03e485d8b87b6aa9230d6950b46972fc6e0381b92ac0081df5c8714501f16b39da18c4ed4bc5bda6e115bf021c7347697dc4
7
- data.tar.gz: e4250d20292067ce5688553822fce21421545108629786776ea10b0074094c1d45b53fb57a713dbf09ee7120b132a347776ccad53e74c340655146480872f1f9
6
+ metadata.gz: 7c4258afca739fd317c388dbae614368ed6ca1c31b37a6eeccffb05de13a07082251fc6322ff5c60cdbe27f856c49342b5213d835c3469fb50b8af7666052c0b
7
+ data.tar.gz: 98e93d68cf0f1c145836dc83ca8a47048ea23250b2778fc94f5a785e015172720e875c691d62a3a85c5b526e37a39ce16c45cf747d4d3c5b46182433fb18075e
data/Rakefile CHANGED
@@ -1,6 +1,12 @@
1
1
  require "bundler/gem_tasks"
2
- #require "rspec/core/rake_task"
2
+ require "rspec/core/rake_task"
3
3
 
4
- #RSpec::Core::RakeTask.new(:spec)
5
4
 
6
- #task :default => :spec
5
+ # RSpec task
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ task :test => :spec
8
+
9
+ # Benchmarking task
10
+ task :benchmark do
11
+ ruby 'benchmarks/run-benchmarks.rb'
12
+ end
@@ -0,0 +1,11 @@
1
+ class Context
2
+ def header
3
+ 'Colors'
4
+ end
5
+
6
+ def item
7
+ [ { name: 'red', current: true, url: '#red' },
8
+ { name: 'green', current: false, url: '#green' },
9
+ { name: 'blue', current: false, url: '#blue' } ]
10
+ end
11
+ end
@@ -0,0 +1,134 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'), File.dirname(__FILE__))
4
+
5
+ require 'benchmark/ips'
6
+
7
+ require 'tilt'
8
+ require 'opulent'
9
+ require 'erubis'
10
+ require 'erb'
11
+ require 'haml'
12
+ require 'slim'
13
+
14
+ require_relative 'context'
15
+
16
+ class Benchmarks
17
+ def initialize(slow)
18
+ @benches = Hash.new { |h, k| h[k] = [] }
19
+
20
+ @opulent_code = File.read(File.dirname(__FILE__) + '/view.erb')
21
+ @erb_code = File.read(File.dirname(__FILE__) + '/view.erb')
22
+ @haml_code = File.read(File.dirname(__FILE__) + '/view.haml')
23
+ @slim_code = File.read(File.dirname(__FILE__) + '/view.slim')
24
+
25
+ init_compiled_benches
26
+ init_tilt_benches
27
+ init_parsing_benches if slow
28
+ end
29
+
30
+ def init_compiled_benches
31
+ haml_pretty = Haml::Engine.new(@haml_code, format: :html5, escape_attrs: false)
32
+ haml_ugly = Haml::Engine.new(@haml_code, format: :html5, ugly: true, escape_attrs: false)
33
+
34
+ context = Context.new
35
+
36
+ haml_pretty.def_method(context, :run_haml_pretty)
37
+ haml_ugly.def_method(context, :run_haml_ugly)
38
+ context.instance_eval %{
39
+ def run_opulent_ugly; #{Opulent.new(@opulent_code).template}; end
40
+ def run_erb; #{ERB.new(@erb_code).src}; end
41
+ def run_erubis; #{Erubis::Eruby.new(@erb_code).src}; end
42
+ def run_temple_erb; #{Temple::ERB::Engine.new.call @erb_code}; end
43
+ def run_fast_erubis; #{Erubis::FastEruby.new(@erb_code).src}; end
44
+ def run_slim_pretty; #{Slim::Engine.new(pretty: true).call @slim_code}; end
45
+ def run_slim_ugly; #{Slim::Engine.new.call @slim_code}; end
46
+ }
47
+
48
+ bench(:compiled, 'opulent') { context.run_opulent_ugly }
49
+ bench(:compiled, 'erb') { context.run_erb }
50
+ bench(:compiled, 'erubis') { context.run_erubis }
51
+ bench(:compiled, 'fast erubis') { context.run_fast_erubis }
52
+ bench(:compiled, 'temple erb') { context.run_temple_erb }
53
+ bench(:compiled, 'slim pretty') { context.run_slim_pretty }
54
+ bench(:compiled, 'slim ugly') { context.run_slim_ugly }
55
+ bench(:compiled, 'haml pretty') { context.run_haml_pretty }
56
+ bench(:compiled, 'haml ugly') { context.run_haml_ugly }
57
+ end
58
+
59
+ def init_tilt_benches
60
+ tilt_opulent = Opulent::Template.new { @opulent_code }
61
+ tilt_erb = Tilt::ERBTemplate.new { @erb_code }
62
+ tilt_erubis = Tilt::ErubisTemplate.new { @erb_code }
63
+ tilt_temple_erb = Temple::ERB::Template.new { @erb_code }
64
+ tilt_haml_pretty = Tilt::HamlTemplate.new(format: :html5) { @haml_code }
65
+ tilt_haml_ugly = Tilt::HamlTemplate.new(format: :html5, ugly: true) { @haml_code }
66
+ tilt_slim_pretty = Slim::Template.new(pretty: true) { @slim_code }
67
+ tilt_slim_ugly = Slim::Template.new { @slim_code }
68
+
69
+ context = Context.new
70
+
71
+ bench(:tilt, 'opulent') { tilt_opulent.render(context) }
72
+ bench(:tilt, 'erb') { tilt_erb.render(context) }
73
+ bench(:tilt, 'erubis') { tilt_erubis.render(context) }
74
+ bench(:tilt, 'temple erb') { tilt_temple_erb.render(context) }
75
+ bench(:tilt, 'slim pretty') { tilt_slim_pretty.render(context) }
76
+ bench(:tilt, 'slim ugly') { tilt_slim_ugly.render(context) }
77
+ bench(:tilt, 'haml pretty') { tilt_haml_pretty.render(context) }
78
+ bench(:tilt, 'haml ugly') { tilt_haml_ugly.render(context) }
79
+ end
80
+
81
+ def init_parsing_benches
82
+ context = Context.new
83
+ context_binding = context.instance_eval { binding }
84
+
85
+ bench(:parsing, 'opulent') { Opulent.new(@opulent_code).result(context_binding) }
86
+ bench(:parsing, 'erb') { ERB.new(@erb_code).result(context_binding) }
87
+ bench(:parsing, 'erubis') { Erubis::Eruby.new(@erb_code).result(context_binding) }
88
+ bench(:parsing, 'fast erubis') { Erubis::FastEruby.new(@erb_code).result(context_binding) }
89
+ bench(:parsing, 'temple erb') { Temple::ERB::Template.new { @erb_code }.render(context) }
90
+ bench(:parsing, 'slim pretty') { Slim::Template.new(pretty: true) { @slim_code }.render(context) }
91
+ bench(:parsing, 'slim ugly') { Slim::Template.new { @slim_code }.render(context) }
92
+ bench(:parsing, 'haml pretty') { Haml::Engine.new(@haml_code, format: :html5).render(context) }
93
+ bench(:parsing, 'haml ugly') { Haml::Engine.new(@haml_code, format: :html5, ugly: true).render(context) }
94
+ end
95
+
96
+ def run
97
+ @benches.each do |group_name, group_benches|
98
+ puts "Running #{group_name} benchmarks:"
99
+
100
+ Benchmark.ips do |x|
101
+ group_benches.each do |name, block|
102
+ x.report("#{group_name} #{name}", &block)
103
+ end
104
+
105
+ x.compare!
106
+ end
107
+ end
108
+
109
+ puts "
110
+ Compiled benchmark: Template is parsed before the benchmark and
111
+ generated ruby code is compiled into a method.
112
+ This is the fastest evaluation strategy because it benchmarks
113
+ pure execution speed of the generated ruby code.
114
+
115
+ Compiled Tilt benchmark: Template is compiled with Tilt, which gives a more
116
+ accurate result of the performance in production mode in frameworks like
117
+ Sinatra, Ramaze and Camping. (Rails still uses its own template
118
+ compilation.)
119
+
120
+ Parsing benchmark: Template is parsed every time.
121
+ This is not the recommended way to use the template engine
122
+ and Slim is not optimized for it. Activate this benchmark with 'rake bench slow=1'.
123
+
124
+ Temple ERB is the ERB implementation using the Temple framework. It shows the
125
+ overhead added by the Temple framework compared to ERB.
126
+ "
127
+ end
128
+
129
+ def bench(group, name, &block)
130
+ @benches[group].push([name, block])
131
+ end
132
+ end
133
+
134
+ Benchmarks.new(ENV['slow']).run
@@ -0,0 +1,23 @@
1
+ <!DOCTYPE HTML>
2
+
3
+ <html>
4
+ <head>
5
+ <title>Simple Benchmark</title>
6
+ </head>
7
+ <body>
8
+ <h1><%= header %></h1>
9
+ <% unless item.empty? %>
10
+ <ul>
11
+ <% for i in item %>
12
+ <% if i[:current] %>
13
+ <li><strong><%= i[:name] %></strong></li>
14
+ <% else %>
15
+ <li><a href="<%= i[:url] %>"><%= i[:name] %></a></li>
16
+ <% end %>
17
+ <% end %>
18
+ </ul>
19
+ <% else %>
20
+ <p>The list is empty.</p>
21
+ <% end %>
22
+ </body>
23
+ </html>
@@ -0,0 +1,18 @@
1
+ !!! html
2
+
3
+ %html
4
+ %head
5
+ %title Simple Benchmark
6
+ %body
7
+ %h1= header
8
+ - unless item.empty?
9
+ %ul
10
+ - for i in item
11
+ - if i[:current]
12
+ %li
13
+ %strong= i[:name]
14
+ - else
15
+ %li
16
+ %a{:href => i[:url]}= i[:name]
17
+ - else
18
+ %p The list is empty.
@@ -0,0 +1,17 @@
1
+ doctype html
2
+ html
3
+ head
4
+ title Simple Benchmark
5
+ body
6
+ h1 == header
7
+ unless item.empty?
8
+ ul
9
+ - for i in item
10
+ if i[:current]
11
+ li
12
+ strong =~ i[:name]
13
+ else
14
+ li
15
+ a href=~i[:url] =~ i[:name]
16
+ else
17
+ p The list is empty.
@@ -0,0 +1,17 @@
1
+ doctype html
2
+ html
3
+ head
4
+ title Simple Benchmark
5
+ body
6
+ h1 == header
7
+ - unless item.empty?
8
+ ul
9
+ - for i in item
10
+ - if i[:current]
11
+ li
12
+ strong == i[:name]
13
+ - else
14
+ li
15
+ a href==i[:url] == i[:name]
16
+ - else
17
+ p The list is empty.
@@ -1,7 +1,7 @@
1
1
  # @Opulent
2
2
  module Opulent
3
3
  # @OpulentTemplate
4
- class OpulentTemplate < ::Tilt::Template
4
+ class Template < ::Tilt::Template
5
5
  # Allow accessing engine definitions
6
6
  attr_reader :def
7
7
 
@@ -47,5 +47,5 @@ module Opulent
47
47
  end
48
48
 
49
49
  # Register Opulent to Tilt
50
- ::Tilt.register OpulentTemplate, 'opulent', 'op'
50
+ ::Tilt.register Template, 'opulent', 'op'
51
51
  end
@@ -1,4 +1,4 @@
1
1
  # @Opulent
2
2
  module Opulent
3
- VERSION = '1.7.0'
3
+ VERSION = '1.7.1'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opulent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Grozav
@@ -96,6 +96,12 @@ files:
96
96
  - LICENSE.md
97
97
  - README.md
98
98
  - Rakefile
99
+ - benchmarks/context.rb
100
+ - benchmarks/run-benchmarks.rb
101
+ - benchmarks/view.erb
102
+ - benchmarks/view.haml
103
+ - benchmarks/view.op
104
+ - benchmarks/view.slim
99
105
  - bin/opulent
100
106
  - docs/attributes.md
101
107
  - docs/comments.md