Sha256: cc81f911e1055e71b016a0267949f615e348c17b210e0d08eb5e7a947afb8a81

Contents?: true

Size: 1.68 KB

Versions: 47

Compression:

Stored size: 1.68 KB

Contents

#!/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

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
haml-6.3.0 bin/bench
haml-6.2.5 bin/bench
haml-6.2.4 bin/bench
haml-6.2.3 bin/bench
haml-6.2.2 bin/bench
haml-6.2.1 bin/bench
haml-6.2.0 bin/bench
haml-6.1.4-java bin/bench
haml-6.1.4 bin/bench
haml-6.1.3-java bin/bench
haml-6.1.3 bin/bench
haml-6.1.2-java bin/bench
haml-6.1.2 bin/bench
haml-6.1.1-java bin/bench
haml-6.1.1 bin/bench
haml-6.1.0-java bin/bench
haml-6.1.0 bin/bench
haml-6.0.12-java bin/bench
haml-6.0.12 bin/bench
haml-6.0.11-java bin/bench