"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/benchmark/benchmark.rb" (1 Jun 2019, 5448 Bytes) of package /linux/www/asciidoctor-2.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/env ruby
    2 
    3 =begin
    4 
    5 Use this script to monitor changes in performance when making code changes to Asciidoctor.
    6 
    7  $ ruby benchmark.rb <benchmark-name> <repeat>
    8 
    9 The most common benchmark is the userguide-loop.
   10 It will download the AsciiDoc User Guide automatically the first time, then convert it in memory.
   11 Running it 10 times provides a good picture.
   12 
   13  $ ruby benchmark.rb userguide-loop 10
   14 
   15 Only worry about the relative change to the numbers before and after the code change.
   16 Absolute times are highly dependent on the capabilities of the machine the the version of Ruby.
   17 
   18 To get the best results under MRI, tune Ruby using environment variables as follows:
   19 
   20 .Ruby < 2.1
   21  $ RUBY_GC_MALLOC_LIMIT=90000000 RUBY_FREE_MIN=650000 ruby benchmark.rb userguide-loop 10
   22 
   23 .Ruby >= 2.1
   24  $ RUBY_GC_MALLOC_LIMIT=128000000 RUBY_GC_OLDMALLOC_LIMIT=128000000 RUBY_GC_HEAP_INIT_SLOTS=10000000 RUBY_GC_HEAP_FREE_SLOTS=10000000 RUBY_GC_HEAP_GROWTH_MAX_SLOTS=250000 RUBY_GC_HEAP_GROWTH_FACTOR=2 ruby benchmark.rb userguide-loop 10
   25  $ RUBY_GC_MALLOC_LIMIT=128000000 RUBY_GC_OLDMALLOC_LIMIT=128000000 RUBY_GC_HEAP_INIT_SLOTS=20000000 RUBY_GC_HEAP_FREE_SLOTS=1000000 RUBY_GC_HEAP_GROWTH_MAX_SLOTS=250000 RUBY_GC_HEAP_GROWTH_FACTOR=2 ruby benchmark.rb userguide-loop 10
   26 
   27 Asciidoctor starts with ~ 12,500 objects, adds ~ 300,000 each run, so tune RUBY_GC_HEAP_* accordingly
   28 
   29 See http://globaldev.co.uk/2014/05/ruby-2-1-in-detail/#gc-tuning-environment-variables
   30 
   31 Execute Ruby using the `--disable=gems` flag to speed up the initial load time, as shown below:
   32 
   33  $ ruby --disable=gems ...
   34 
   35 =end
   36 
   37 require 'benchmark'
   38 include Benchmark
   39 
   40 bench = ARGV[0]
   41 $repeat = ARGV[1].to_i || 10000
   42 
   43 if bench.nil?
   44   raise 'You must specify a benchmark to run.'
   45 end
   46 
   47 def fetch_userguide
   48   require 'open-uri'
   49   userguide_uri = 'https://raw.githubusercontent.com/asciidoc/asciidoc/d43faae38c4a8bf366dcba545971da99f2b2d625/doc/asciidoc.txt'
   50   customers_uri = 'https://raw.githubusercontent.com/asciidoc/asciidoc/d43faae38c4a8bf366dcba545971da99f2b2d625/doc/customers.csv'
   51   userguide_content = open(userguide_uri) {|fd2| fd2.read }
   52   customers_content = open(customers_uri) {|fd2| fd2.read }
   53   File.open('sample-data/userguide.adoc', 'w') {|fd1| fd1.write userguide_content }
   54   File.open('sample-data/customers.csv', 'w') {|fd1| fd1.write customers_content }
   55 end
   56 
   57 case bench
   58 
   59 =begin
   60 # benchmark template
   61 
   62 when 'name'
   63 
   64   sample = 'value'
   65 
   66   Benchmark.bmbm(12) {|bm|
   67     bm.report('operation a') { $repeat.times { call_a_on sample } }
   68     bm.report('operation b') { $repeat.times { call_b_on sample } }
   69   }
   70 =end
   71 
   72 when 'userguide'
   73   require '../lib/asciidoctor.rb'
   74   Asciidoctor::Compliance.markdown_syntax = false
   75   Asciidoctor::Compliance.shorthand_property_syntax = false if Asciidoctor::VERSION > '0.1.4'
   76   sample_file = ENV['BENCH_TEST_FILE'] || 'sample-data/userguide.adoc'
   77   backend = ENV['BENCH_BACKEND'] || 'html5'
   78   fetch_userguide if sample_file == 'sample-data/userguide.adoc' && !(File.exist? sample_file)
   79   result = Benchmark.bmbm {|bm|
   80     bm.report(%(Convert #{sample_file} (x#{$repeat}))) {
   81       $repeat.times {
   82         Asciidoctor.render_file sample_file, :backend => backend, :safe => Asciidoctor::SafeMode::SAFE, :eruby => 'erubis', :header_footer => true, :to_file => false, :attributes => {'stylesheet' => nil, 'toc' => nil, 'numbered' => nil, 'icons' => nil, 'compat-mode' => ''}
   83       }
   84     }
   85   }
   86   # prints average for real run
   87   puts %(>avg: #{result.first.real / $repeat})
   88 
   89 when 'userguide-loop'
   90   require '../lib/asciidoctor.rb'
   91   GC.start
   92   Asciidoctor::Compliance.markdown_syntax = false
   93   Asciidoctor::Compliance.shorthand_property_syntax = false if Asciidoctor::VERSION > '0.1.4'
   94   sample_file = ENV['BENCH_TEST_FILE'] || 'sample-data/userguide.adoc'
   95   backend = ENV['BENCH_BACKEND'] || 'html5'
   96   fetch_userguide if sample_file == 'sample-data/userguide.adoc' && !(File.exist? sample_file)
   97 
   98   timings = []
   99   2.times.each do
  100     loop_timings = []
  101     (1..$repeat).each do
  102       start = Time.now
  103       Asciidoctor.render_file sample_file, :backend => backend, :safe => Asciidoctor::SafeMode::SAFE, :eruby => 'erubis', :header_footer => true, :to_file => false, :attributes => { 'stylesheet' => nil, 'toc' => nil, 'numbered' => nil, 'icons' => nil, 'compat-mode' => '' }
  104       loop_timings << (Time.now - start)
  105     end
  106     timings << loop_timings
  107   end
  108   best_time = nil
  109   timings.each do |loop_timings|
  110     puts %(#{loop_timings * "\n"}\nRun Total: #{loop_timings.reduce :+})
  111     best_time = best_time ? [best_time, loop_timings.min].min : loop_timings.min
  112   end
  113   puts %(Best Time: #{best_time})
  114 
  115 when 'mdbasics-loop'
  116   require '../lib/asciidoctor.rb'
  117   GC.start
  118   sample_file = ENV['BENCH_TEST_FILE'] || 'sample-data/mdbasics.adoc'
  119   backend = ENV['BENCH_BACKEND'] || 'html5'
  120 
  121   timings = []
  122   2.times do
  123     loop_timings = []
  124     (1..$repeat).each do
  125       start = Time.now
  126       Asciidoctor.render_file sample_file, :backend => backend, :safe => Asciidoctor::SafeMode::SAFE, :header_footer => false, :to_file => false, :attributes => { 'stylesheet' => nil, 'idprefix' => '', 'idseparator' => '-', 'showtitle' => '' }
  127       loop_timings << (Time.now - start)
  128     end
  129     timings << loop_timings
  130   end
  131   best_time = nil
  132   timings.each do |loop_timings|
  133     puts %(#{loop_timings * "\n"}\nRun Total: #{loop_timings.reduce :+})
  134     best_time = best_time ? [best_time, loop_timings.min].min : loop_timings.min
  135   end
  136   puts %(Best Time: #{best_time})
  137 
  138 end