vadviktor
5/5/2016 - 1:56 PM

Benchmark deleting files older than 20 sec (target: jRuby 1.7.4 Ubuntu 14.04)

Benchmark deleting files older than 20 sec (target: jRuby 1.7.4 Ubuntu 14.04)

require 'benchmark/ips'
require 'fileutils'

DIR = '/opt/tmp'

def eachfile
  now = Time.now
  Dir.glob("#{DIR}/puma*").each do |f|
    FileUtils.rm_f f if now - File.mtime(f) > 20
  end
end

def onecommand
  now = Time.now
  FileUtils.rm_f Dir.glob("#{DIR}/puma*").map { |f| f if now - File.mtime(f) > 20 }
end

def onesystemcall
  `find #{DIR} -name "puma*" -a -mmin +$((20/60)) -exec rm -f {} \;`
end

Benchmark.ips do |x|
  x.report('Delete each file') { eachfile }
  x.report('Delete files in one command') { onecommand }
  x.report('Delete files in one system call') { onesystemcall }

  x.compare!
end