File: render_template_benchmark.rb

package info (click to toggle)
ruby-mustache 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 620 kB
  • sloc: ruby: 2,712; makefile: 2
file content (76 lines) | stat: -rw-r--r-- 1,493 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
$:.unshift 'lib'

require 'benchmark/ips'
require 'mustache'

template = """
{{#products}}
  <div class='product_brick'>
    <div class='container'>
      <div class='element'>
        <img src='images/{{image}}' class='product_miniature' />
      </div>
      <div class='element description'>
        <a href={{url}} class='product_name block bold'>
          {{external_index}}
        </a>
      </div>
    </div>
  </div>
{{/products}}
"""

data_10 = {
  products: []
}

10.times do
  data_10[:products] << {
    :external_index=>"product",
    :url=>"/products/7",
    :image=>"products/product.jpg"
  }
end

data_100 = {
  products: []
}

100.times do
  data_100[:products] << {
    :external_index=>"product",
    :url=>"/products/7",
    :image=>"products/product.jpg"
  }
end

data_1000 = {
  products: []
}

1000.times do
  data_1000[:products] << {
    :external_index=>"product",
    :url=>"/products/7",
    :image=>"products/product.jpg"
  }
end

template = Mustache::Template.new(template)

Benchmark.ips do |x|
  x.report("render list of 10") do |times|
    ctx_10 = Mustache::Context.new(Mustache.new); ctx_10.push(data_10)
    template.render(ctx_10)
  end

  x.report("render list of 100") do |times|
    ctx_100 = Mustache::Context.new(Mustache.new); ctx_100.push(data_100)
    template.render(ctx_100)
  end

  x.report("render list of 1000") do |times|
    ctx_1000 = Mustache::Context.new(Mustache.new); ctx_1000.push(data_1000)
    template.render(ctx_1000)
  end
end