File: bench

package info (click to toggle)
ruby-haml 6.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,004 kB
  • sloc: ruby: 9,908; sh: 23; makefile: 11
file content (66 lines) | stat: -rwxr-xr-x 1,717 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env ruby

require 'bundler/setup'
require 'haml'
require 'thor'
require 'benchmark/ips'
require_relative '../benchmark/utils/benchmark_ips_extension'

class Bench < Thor
  class_option :show_template, type: :boolean, aliases: ['-t']

  desc 'bench HAML', 'Benchmark haml template'
  option :compile, type: :boolean, aliases: ['-c']
  option :show_code, type: :boolean, aliases: ['-s']
  def bench(*files)
    files.each { |file| render(file) }
    files.each { |file| compile(file) if options[:compile] }
    files.each { |file| code(file) if options[:show_code] }
  end

  desc 'compile HAML', 'Benchmark compilation'
  def compile(file)
    puts "#{?= * 49}\n Compilation: #{file}\n#{?= * 49}"
    haml = File.read(file)

    Benchmark.ips do |x|
      x.report("haml v#{Haml::VERSION}") { Haml::Engine.new.call(haml) }
      x.compare!
    end
  end

  desc 'render HAML', 'Benchmark rendering'
  def render(file)
    puts "#{?= * 49}\n Rendering: #{file}\n#{?= * 49}"
    haml = File.read(file)
    puts haml + "\n" if options[:show_template]
    object = Object.new
    ruby_file = file.gsub(/\.haml\z/, '.rb')
    if File.exist?(ruby_file)
      object.instance_eval(File.read(ruby_file))
    end

    object.instance_eval "def haml; #{Haml::Engine.new.call(haml)}; end"

    Benchmark.ips do |x|
      x.report("haml v#{Haml::VERSION}") { object.haml }
      x.compare!
    end
  end

  desc 'code HAML', 'Show compiled code'
  def code(file)
    haml = File.read(file)
    puts "\n#{?= * 49}\n Haml Source: #{file}\n#{?= * 49}"
    puts Haml::Engine.new.call(haml)
  end

  private

  def method_missing(*args)
    return super if args.length > 1
    render(args.first.to_s)
  end
end

Bench.start