6 FILE = 'io_test_bench_file.txt'
8 File.open(FILE, 'w'){ |fh|
10 fh.puts "This is line: #{n}"
13 stat = File.stat(FILE)
14 buf_sizes = (0..16).map { |i| 1 << i }
15 (ARGV[0] || 5).to_i.times do
16 Benchmark.bm(30) do |x|
18 buf_sizes.each { |size|
19 read_iter = BLOCKSIZE / size
20 read_iter = read_iter > 4096 ? 4096 : read_iter < 16 ? 16 : read_iter
22 x.report("#{read_iter}.times { f.read(#{size}) }") {
25 read_iter.times { f.read(size) }
30 x.report('IO.read(file)'){
33 if buf.length < stat.size
34 raise "Incorrect size returned by IO.read() #{buf.length} != #{stat.size}"
39 buf_sizes.each do |size|
40 x.report("IO.read(file, #{size})"){
42 buf = IO.read(FILE, size)
45 x.report("IO.read(file, #{size}, 20)"){
46 IOREPS.times{ IO.read(FILE, size, 20) }
50 x.report('IO.foreach(file)'){
51 MAX.times{ IO.foreach(FILE){} }
55 File.delete(FILE) if File.exists?(FILE)