erubis-cached-text 0.0.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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OGQyZGM1YzMzNmE4ODcxYmYzZGZiMjEwNTM1Njg5OGVlZjEzNzgxNg==
5
+ data.tar.gz: !binary |-
6
+ ZjU1ZDg2ZDY5MTM5OTc2NmFjZTNhNDQwNjBlYzI3N2EwMTZhYzQ5Mw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OWMyYWY2YzEzNTAyYWNiNjYwMjc4NzViZmQ5NTkzZDIxN2Q2YmQ5ZDIwNjVh
10
+ Yjg1ZDMwNTY3NDkxZmIzMzQ4OGFhZTIxODQ5ZDNhZWRjZjg1OWMzZjY2OTJi
11
+ MWNiZDAwNjYxOGM4MjYwZjc1ZTA4NzYwNGMzMDFkZTMxNGYwYmI=
12
+ data.tar.gz: !binary |-
13
+ OWM2ZjU4MTUwYjk3MzlmNjFiMDU5YzA2ZTE2YmFmN2RlZjc1MDg0OGNjMzI3
14
+ MGM1OWVmMDdhMjRlM2VlYTI4M2FjMWMxODUwYmIwZDE5MjAyZDBjYzZkMzA2
15
+ YTAyYjEyNzQ2MjFmNzc3OTU4OTMxMWU3YmY5YTgxMGNiNThkMDg=
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ Files in /benchmark copyright kuwata-lab.com (see benchmark/MIT-LICENSE)
2
+
3
+ Files in /lib are under the following license:
4
+
5
+ Copyright (c) 2014 by Luke Gruber
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included
16
+ in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ Erubis Cached Text: All ur HTML are belong 2 MEMRY
2
+ ==================================================
3
+
4
+ What is it?
5
+ -----------
6
+
7
+ It's an Eruby engine (using Erubis) that caches static text in memory instead
8
+ of instantiating these String objects every time the Eruby template is rendered.
9
+ Basically, this means a bit more memory is consumed by your Ruby process, but
10
+ rendering time is reduced (and how!).
11
+
12
+ How much faster is it?
13
+ ----------------------
14
+
15
+ This depends entirely on how much static text you have in your templates. If
16
+ your template is entirely code, it won't save you any time at all. However,
17
+ for templates with lots of static text (such as HTML, JavaScript, etc), the
18
+ speed difference is quite substantial. I plan on benchmarking it a lot more
19
+ in the near future, but right now I just have a 600 line HTML template with a
20
+ few lines of code and in THIS situation it's 3x faster than normal Erubis
21
+ rendering.
22
+
23
+ Also, less allocations means less time spent in GC :)
24
+
25
+ How can I use it?
26
+ -----------------
27
+
28
+ gem 'erubis-cached-text'
29
+ require 'erubis_cached_text'
30
+
31
+ The rest is the same, just use the class Erubis::ErubyCachedText instead of
32
+ the engine you used to use (such as Erubis::Eruby).
33
+
34
+ How can I use it with Rails?
35
+ ----------------------------
36
+
37
+ I'll be coming out with a gem soon for use with Rails.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,20 @@
1
+ copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+
2
+ bench:
3
+ ruby bench.rb -n 10000
4
+
5
+ clean:
6
+ rm -rf bench_*.rhtml*
@@ -0,0 +1,289 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ###
4
+ ### $Release: 2.7.0 $
5
+ ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
6
+ ###
7
+ #
8
+ # Modifications made by luke-gru 2014
9
+
10
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
11
+
12
+ Dir.chdir File.expand_path('../', __FILE__)
13
+
14
+ require 'erb'
15
+ require 'erubis'
16
+ require 'erubis/tiny'
17
+ require 'erubis/engine/enhanced'
18
+ require 'erubis_cached_text'
19
+ require 'yaml'
20
+ require 'cgi'
21
+ include ERB::Util
22
+
23
+ begin
24
+ require 'eruby'
25
+ rescue LoadError
26
+ ERuby = nil
27
+ end
28
+
29
+ def File.write(filename, content)
30
+ File.open(filename, 'w') { |f| f.write(content) }
31
+ end
32
+
33
+ ## change benchmark library to use $stderr instead of $stdout
34
+ require 'benchmark'
35
+ module Benchmark
36
+ class Report
37
+ def print(*args)
38
+ $stderr.print(*args)
39
+ end
40
+ end
41
+ module_function
42
+ def print(*args)
43
+ $stderr.print(*args)
44
+ end
45
+ end
46
+
47
+
48
+ class BenchmarkApplication
49
+
50
+ TARGETS = %w[
51
+ ERB(cached)
52
+ Erubis::Eruby(cached)
53
+ Erubis::ErubyCachedText(cached)
54
+ Erubis::FastEruby(cached)
55
+ ]
56
+
57
+ def initialize(ntimes, context, targets=nil, params={})
58
+ @ntimes = ntimes
59
+ @context = context
60
+ @targets = targets && !targets.empty? ? targets : TARGETS.dup
61
+ @testmode = params[:testmode] || 'execute'
62
+ @erubyfile = params[:erubyfile] || 'erubybench.rhtml'
63
+ @printout = params[:printout] || false
64
+ end
65
+
66
+ attr_accessor :ntimes, :targets
67
+ attr_accessor :testmode, :erubyfile, :contextfile, :printout
68
+
69
+ def context2code(context, varname='context')
70
+ s = ''
71
+ context.each { |k, | s << "#{k} = #{varname}[#{k.inspect}]; " }
72
+ return s
73
+ end
74
+
75
+ def perform_benchmark
76
+ width = 30
77
+ $stderr.puts "*** ntimes=#{@ntimes}, testmode=#{@testmode}"
78
+ Benchmark.bm(width) do |job|
79
+ for target in @targets do
80
+ method = "#{@testmode}_#{target.gsub(/::|-|\(/, '_').gsub(/\)/, '').downcase}"
81
+ #$stderr.puts "*** debug: method=#{method.inspect}"
82
+ next unless self.respond_to?(method)
83
+ filename = "bench_#{(target =~ /^(\w+)/) && $1.downcase}.rhtml"
84
+ title = target
85
+ output = nil
86
+ job.report(title) do
87
+ output = self.__send__(method, filename, @context)
88
+ end
89
+ File.write("output.#{target.gsub(/[^\w]/,'')}", output) if @printout && output && !output.empty?
90
+ end
91
+ end
92
+ end
93
+
94
+ ##
95
+
96
+ def execute_eruby(filename, context)
97
+ return unless ERuby
98
+ list = context['list']
99
+ @ntimes.times do
100
+ ERuby.import(filename)
101
+ end
102
+ return nil
103
+ end
104
+
105
+ def execute_erb(filename, context)
106
+ list = context['list']
107
+ output = nil
108
+ @ntimes.times do
109
+ eruby = ERB.new(File.read(filename))
110
+ output = eruby.result(binding())
111
+ print output
112
+ end
113
+ return output
114
+ end
115
+
116
+ def execute_erb_cached(filename, context)
117
+ list = context['list']
118
+ output = nil
119
+ cachefile = filename + '.cache'
120
+ File.unlink(cachefile) if test(?f, cachefile)
121
+ if !test(?f, cachefile) || File.mtime(filename) > File.mtime(cachefile)
122
+ eruby = ERB.new(File.read(filename))
123
+ File.write(cachefile, eruby.src)
124
+ end
125
+ @ntimes.times do
126
+ eruby = ERB.new('')
127
+ eruby.instance_variable_set("@src", File.read(cachefile))
128
+ output = eruby.result(binding())
129
+ print output
130
+ end
131
+ return output
132
+ end
133
+
134
+ ## cached
135
+ for klass in %w[Eruby ErubyCachedText FastEruby] do
136
+ s = <<-END
137
+ def execute_erubis_#{klass.downcase}_cached(filename, context)
138
+ list = context['list']
139
+ cachefile = filename + '.cache'
140
+ File.unlink(cachefile) if test(?f, cachefile)
141
+ output = nil
142
+ @ntimes.times do
143
+ eruby = Erubis::#{klass}.load_file(filename)
144
+ output = eruby.result(binding())
145
+ print output
146
+ end
147
+ savefile = cachefile.sub(/\\.cache$/, '.#{klass.downcase}.cache')
148
+ File.rename(cachefile, savefile)
149
+ return output
150
+ end
151
+ END
152
+ eval s
153
+ end
154
+
155
+ ##
156
+
157
+ def convert_eruby(filename, context)
158
+ return unless ERuby
159
+ list = context['list']
160
+ output = nil
161
+ @ntimes.times do
162
+ output = ERuby::Compiler.new.compile_string(File.read(filename))
163
+ end
164
+ return output
165
+ end
166
+
167
+ def convert_erb(filename, context)
168
+ list = context['list']
169
+ output = nil
170
+ @ntimes.times do
171
+ eruby = ERB.new(File.read(filename))
172
+ output = eruby.src
173
+ end
174
+ return output
175
+ end
176
+
177
+ for klass in %w[Eruby ErubyCachedText FastEruby TinyEruby]
178
+ s = <<-END
179
+ def convert_erubis_#{klass.downcase}(filename, context)
180
+ list = context['list']
181
+ output = nil
182
+ @ntimes.times do
183
+ eruby = Erubis::#{klass}.new(File.read(filename))
184
+ output = eruby.src
185
+ end
186
+ return output
187
+ end
188
+ END
189
+ eval s
190
+ end
191
+
192
+ end
193
+
194
+
195
+ require 'optparse'
196
+
197
+ class MainApplication
198
+
199
+ def parse_argv(argv=ARGV)
200
+ optparser = OptionParser.new
201
+ options = {}
202
+ ['-h', '-n N', '-t erubyfile', '-f contextfile', '-A', '-e',
203
+ '-x exclude', '-m testmode', '-X', '-p', '-D'].each do |opt|
204
+ optparser.on(opt) { |val| options[opt[1].chr] = val }
205
+ end
206
+ begin
207
+ targets = optparser.parse!(argv)
208
+ rescue => ex
209
+ $stderr.puts "#{@script}: #{ex.to_s}"
210
+ exit(1)
211
+ end
212
+ return options, targets
213
+ end
214
+
215
+ def execute
216
+ @script = File.basename($0)
217
+ ntimes = 1000
218
+ targets = BenchmarkApplication::TARGETS.dup
219
+ testmode = 'execute'
220
+ contextfile = 'bench_context.yaml'
221
+ #
222
+ options, args = parse_argv(ARGV)
223
+ ntimes = options['n'].to_i if options['n']
224
+ targets = args if args && !args.empty?
225
+ targets = targets - options['x'].split(/,/) if options['x']
226
+ testmode = options['m'] if options['m']
227
+ contextfile = options['f'] if options['f']
228
+ erubyfile = options['t'] if options['t']
229
+ #
230
+ if options['h']
231
+ $stderr.puts "Usage: ruby #{@script} [..options..] [..targets..]"
232
+ $stderr.puts " -h : help"
233
+ $stderr.puts " -n N : loop N times"
234
+ $stderr.puts " -f datafile : context data filename (*.yaml)"
235
+ $stderr.puts " -x exclude : exclude target name"
236
+ $stdout.puts " -m testmode : 'execute' or 'convert' (default 'execute')"
237
+ $stderr.puts " -p : print output to file (filename: 'output.TARGETNAME')"
238
+ return
239
+ end
240
+ #
241
+ #if ! options['t']
242
+ for item in %w[eruby erb erubis]
243
+ fname = "bench_#{item}.rhtml"
244
+ header = File.read("templates/_header.html")
245
+ #body = File.read("templates/#{erubyfile}")
246
+ body = File.read("templates/#{fname}")
247
+ footer = File.read("templates/_footer.html")
248
+ content = header + body + footer
249
+ File.write(fname, content)
250
+ end
251
+ #
252
+ if options['e'] # escape
253
+ tuples = [
254
+ [ 'bench_eruby.rhtml', '<%= CGI.escapeHTML((\1).to_s) %>' ],
255
+ [ 'bench_erb.rhtml', '<%=h \1 %>' ],
256
+ [ 'bench_erubis.rhtml', '<%== \1 %>' ],
257
+ ]
258
+ for fname, replace in tuples
259
+ content = File.read(fname).gsub(/<%= ?(.*?) ?%>/, replace)
260
+ File.write(fname, content)
261
+ end
262
+ targets.delete('Erubis::TinyEruby') ## because TinyEruby doesn't support '<%== =>'
263
+ end
264
+ #
265
+ context = YAML.load_file(contextfile)
266
+ #
267
+ params = {
268
+ :printout=>options['p'],
269
+ :testmode=>testmode,
270
+ }
271
+ app = BenchmarkApplication.new(ntimes, context, targets, params)
272
+ app.perform_benchmark()
273
+ end
274
+
275
+ end
276
+
277
+
278
+ if __FILE__ == $0
279
+
280
+ ## open /dev/null
281
+ $stdout = File.open('/dev/null', 'w')
282
+ at_exit do
283
+ $stdout.close()
284
+ end
285
+
286
+ ## start benchmark
287
+ MainApplication.new().execute()
288
+
289
+ end
@@ -0,0 +1,141 @@
1
+ list:
2
+ - name: Adobe Systems
3
+ name2: Adobe Systems Inc.
4
+ url: http://www.adobe.com
5
+ symbol: ADBE
6
+ price: 39.26
7
+ change: 0.13
8
+ ratio: 0.33
9
+ - name: Advanced Micro Devices
10
+ name2: Advanced Micro Devices Inc.
11
+ url: http://www.amd.com
12
+ symbol: AMD
13
+ price: 16.22
14
+ change: 0.17
15
+ ratio: 1.06
16
+ - name: Amazon.com
17
+ name2: Amazon.com Inc
18
+ url: http://www.amazon.com
19
+ symbol: AMZN
20
+ price: 36.85
21
+ change: -0.23
22
+ ratio: -0.62
23
+ - name: Apple
24
+ name2: Apple Inc.
25
+ url: http://www.apple.com
26
+ symbol: AAPL
27
+ price: 85.38
28
+ change: -0.87
29
+ ratio: -1.01
30
+ - name: BEA Systems
31
+ name2: BEA Systems Inc.
32
+ url: http://www.bea.com
33
+ symbol: BEAS
34
+ price: 12.46
35
+ change: 0.09
36
+ ratio: 0.73
37
+ - name: CA
38
+ name2: CA, Inc.
39
+ url: http://www.ca.com
40
+ symbol: CA
41
+ price: 24.66
42
+ change: 0.38
43
+ ratio: 1.57
44
+ - name: Cisco Systems
45
+ name2: Cisco Systems Inc.
46
+ url: http://www.cisco.com
47
+ symbol: CSCO
48
+ price: 26.35
49
+ change: 0.13
50
+ ratio: 0.5
51
+ - name: Dell
52
+ name2: Dell Corp.
53
+ url: http://www.dell.com/
54
+ symbol: DELL
55
+ price: 23.73
56
+ change: -0.42
57
+ ratio: -1.74
58
+ - name: eBay
59
+ name2: eBay Inc.
60
+ url: http://www.ebay.com
61
+ symbol: EBAY
62
+ price: 31.65
63
+ change: -0.8
64
+ ratio: -2.47
65
+ - name: Google
66
+ name2: Google Inc.
67
+ url: http://www.google.com
68
+ symbol: GOOG
69
+ price: 495.84
70
+ change: 7.75
71
+ ratio: 1.59
72
+ - name: Hewlett-Packard
73
+ name2: Hewlett-Packard Co.
74
+ url: http://www.hp.com
75
+ symbol: HPQ
76
+ price: 41.69
77
+ change: -0.02
78
+ ratio: -0.05
79
+ - name: IBM
80
+ name2: International Business Machines Corp.
81
+ url: http://www.ibm.com
82
+ symbol: IBM
83
+ price: 97.45
84
+ change: -0.06
85
+ ratio: -0.06
86
+ - name: Intel
87
+ name2: Intel Corp.
88
+ url: http://www.intel.com
89
+ symbol: INTC
90
+ price: 20.53
91
+ change: -0.07
92
+ ratio: -0.34
93
+ - name: Juniper Networks
94
+ name2: Juniper Networks, Inc
95
+ url: http://www.juniper.net/
96
+ symbol: JNPR
97
+ price: 18.96
98
+ change: 0.5
99
+ ratio: 2.71
100
+ - name: Microsoft
101
+ name2: Microsoft Corp
102
+ url: http://www.microsoft.com
103
+ symbol: MSFT
104
+ price: 30.6
105
+ change: 0.15
106
+ ratio: 0.49
107
+ - name: Oracle
108
+ name2: Oracle Corp.
109
+ url: http://www.oracle.com
110
+ symbol: ORCL
111
+ price: 17.15
112
+ change: 0.17
113
+ ratio: 1.0
114
+ - name: SAP
115
+ name2: SAP AG
116
+ url: http://www.sap.com
117
+ symbol: SAP
118
+ price: 46.2
119
+ change: -0.16
120
+ ratio: -0.35
121
+ - name: Seagate Technology
122
+ name2: Seagate Technology
123
+ url: http://www.seagate.com/
124
+ symbol: STX
125
+ price: 27.35
126
+ change: -0.36
127
+ ratio: -1.3
128
+ - name: Sun Microsystems
129
+ name2: Sun Microsystems Inc.
130
+ url: http://www.sun.com
131
+ symbol: SUNW
132
+ price: 6.33
133
+ change: -0.01
134
+ ratio: -0.16
135
+ - name: Yahoo
136
+ name2: Yahoo! Inc.
137
+ url: http://www.yahoo.com
138
+ symbol: YHOO
139
+ price: 28.04
140
+ change: -0.17
141
+ ratio: -0.6