Module: Benchmarker::Misc

Defined in:
lib/benchmarker.rb

Class Method Summary collapse

Class Method Details

.cpu_modelObject



734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'lib/benchmarker.rb', line 734

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_infoObject



691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/benchmarker.rb', line 691

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_nameObject



707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# File 'lib/benchmarker.rb', line 707

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_codeObject



757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
# File 'lib/benchmarker.rb', line 757

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