module Benchmarker::Misc

Public Instance Methods

cpu_model() click to toggle source
# File lib/benchmarker.rb, line 733
def cpu_model()
  #; [!6ncgq] returns string representing cpu model.
  if File.exist?("/usr/sbin/sysctl")        # macOS
    s = `/usr/sbin/sysctl machdep.cpu.brand_string`
    s =~ /^machdep\.cpu\.brand_string: (.*)/
    return $1
  elsif File.exist?("/proc/cpuinfo")        # Linux
    s = `cat /proc/cpuinfo`
    s =~ /^model name\s*: (.*)/
    return $1
  elsif File.exist?("/var/run/dmesg.boot")  # FreeBSD
    s = `grep ^CPU: /var/run/dmesg.boot`
    s =~ /^CPU: (.*)/
    return $1
  elsif RUBY_PLATFORM =~ /win/              # Windows
    s = `systeminfo`
    s =~ /^\s+\[01\]: (.*)/    # TODO: not tested yet
    return $1
  else
    return nil
  end
end
environment_info() click to toggle source
# File lib/benchmarker.rb, line 690
def environment_info()
  #; [!w1xfa] returns environment info in key-value list.
  ruby_engine_version = (RUBY_ENGINE_VERSION rescue nil)
  cc_version_msg = RbConfig::CONFIG['CC_VERSION_MESSAGE']
  return [
    ["benchmarker"   , "release #{VERSION}"],
    ["ruby engine"   , "#{RUBY_ENGINE} (engine version #{ruby_engine_version})"],
    ["ruby version"  , "#{RUBY_VERSION} (patch level #{RUBY_PATCHLEVEL})"],
    ["ruby platform" , RUBY_PLATFORM],
    ["ruby path"     , RbConfig.ruby],
    ["compiler"      , cc_version_msg ? cc_version_msg.split(/\r?\n/)[0] : nil],
    ["os name"       , os_name()],
    ["cpu model"     , cpu_model()],
  ]
end
os_name() click to toggle source
# File lib/benchmarker.rb, line 706
def os_name()
  #; [!83vww] returns string representing os name.
  if File.file?("/usr/bin/sw_vers")   # macOS
    s = `/usr/bin/sw_vers`
    s =~ /^ProductName:\s+(.*)/;    product = $1
    s =~ /^ProductVersion:\s+(.*)/; version = $1
    return "#{product} #{version}"
  end
  if File.file?("/etc/lsb-release")   # Linux
    s = File.read("/etc/lsb-release", encoding: 'utf-8')
    if s =~ /^DISTRIB_DESCRIPTION="(.*)"/
      return $1
    end
  end
  if File.file?("/usr/bin/uname")     # UNIX
    s = `/usr/bin/uname -srm`
    return s.strip
  end
  if RUBY_PLATFORM =~ /win/           # Windows
    s = `systeminfo`     # TODO: not tested yet
    s =~ /^OS Name:\s+(?:Microsft )?(.*)/; product = $1
    s =~ /^OS Version:\s+(.*)/; version = $1 ? $1.split()[0] : nil
    return "#{product} #{version}"
  end
  return nil
end
sample_code() click to toggle source
# File lib/benchmarker.rb, line 756
    def sample_code()
      return <<'END'
# -*- coding: utf-8 -*-

require 'benchmarker'  # https://kwatch.github.io/benchmarker/ruby.html

nums = (1..10000).to_a

title = "calculate sum of integers"
Benchmarker.scope(title, width: 24, loop: 1000, iter: 5, extra: 1) do
  ## other options -- inverse: true, outfile: "result.json", quiet: true,
  ##                  sleep: 1, colorize: true, filter: "task=*foo*"

  ## hooks
  #before_all do end
  #after_all  do end
  #before do end     # or: before do |task_name, tag| end
  #after  do end     # or: after  do |task_name, tag| end

  ## tasks
  task nil do    # empty-loop task
    # do nothing
  end

  task "each() & '+='" do
    total = 0
    nums.each {|n| total += n }
    total
  end

  task "inject()" do
    total = nums.inject(0) {|t, n| t += n }
    total
  end

  task "while statement" do
    total = 0; i = -1; len = nums.length
    while (i += 1) < len
      total += nums[i]
    end
    total
  end

  #task "name", tag: "curr", skip: (!condition ? nil : "...reason...") do
  #  ... run benchmark code ...
  #end

  ## validation
  validate do |val|   # or: validate do |val, task_name, tag|
    n = nums.last
    expected = n * (n+1) / 2
    assert_eq val, expected
      # or: assert val == expected, "expected #{expected} but got #{val}"
  end

end
END
    end