Sha256: 2de4935b487fdf11f0edb8cde97df69e1055d012765f60830175a807a776cf6b

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module Ben
  class Disk
    class << self
      def run(p_disk)
        Disk.new(p_disk).run
      end
    end

    # /dev/sda
    attr_reader :p_disk

    def initialize(p_disk)
      @p_disk = p_disk
    end

    def get_model_number
      `hdparm -I #{p_disk} | grep 'Model Number' | awk -F: '{print $2}'`.strip.downcase
    end

    # once
    def benchmark
      read, unit = dd_result(`dd if=/dev/zero of=a.bin bs=1M count=1024 conv=fdatasync,notrunc 2>&1`)
      read = read.to_i

      `su root -c 'echo 3 > /proc/sys/vm/drop_caches'`
      write, unit1 = dd_result(`dd if=a.bin of=/dev/null bs=1M count=1024 2>&1`)
      write = write.to_i

      Ben.ui.say "This Time: #{read}#{unit} #{write}#{unit}"

      [read, write, unit]
    end

    # run n times
    def run
      rst = Rc.disk.counts.times.map { benchmark }
      unit = rst[0][2]

      read, write = rst.transpose[0..1].map {|v|
        v.inject(:+)/v.length.to_f
      }

      Ben.ui.say "Average: #{read}#{unit} #{write}#{unit}"

      today = Date.today.strftime("%Y-%m-%d")
      p_log = Pa("~/.ben/disk.#{get_model_number}")
      File.append(p_log.p, "#{today}: #{read}#{unit} #{write}#{unit}\n")
    rescue => e
      Pa.rm "a.bin"
      raise e
    end

  private
    def dd_result(result)
      _, speed, unit = result.split("\n")[-1].match(/, (\d+) (.+)$/).to_a
      [speed, unit]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ben-0.0.1 lib/ben/disk.rb