Sha256: 595b6940202220ba27cc6c37060d1126a0459eabeb93ed948b1908506fef38cd

Contents?: true

Size: 922 Bytes

Versions: 118

Compression:

Stored size: 922 Bytes

Contents

#!/usr/bin/env ruby

require "benchmark"

# This tests demonstrates throughput difference of
# IO#write and IO#write_nonblock. Note that the two
# may not be equivalent depending on your

r, w = IO.pipe

# buffer size
b    = 65536

read_loop = Thread.new do
  loop do
    begin
      r.read_nonblock(b)
    rescue Errno::EWOULDBLOCK, Errno::EAGAIN => e
      IO.select([r])
      retry
    end
  end
end

n = 10_000
# 7 KB
s = "a" * (7 * 1024)
Benchmark.bm do |meter|
  meter.report("write:") do
    n.times { w.write(s.dup) }
  end
  meter.report("write + flush:") do
    n.times { w.write(s.dup); w.flush }
  end
  meter.report("write_nonblock:") do
    n.times do
      s2 = s.dup
      begin
        while !s2.empty?
          written = w.write_nonblock(s2)
          s2.slice!(0, written)
        end
      rescue Errno::EWOULDBLOCK, Errno::EAGAIN
        IO.select([], [w])
        retry
      end
    end
  end
end

Version data entries

118 entries across 118 versions & 1 rubygems

Version Path
bunny-2.17.0 benchmarks/write_vs_write_nonblock.rb
bunny-2.16.1 benchmarks/write_vs_write_nonblock.rb
bunny-2.15.0 benchmarks/write_vs_write_nonblock.rb
bunny-2.14.4 benchmarks/write_vs_write_nonblock.rb
bunny-2.14.3 benchmarks/write_vs_write_nonblock.rb
bunny-2.14.2 benchmarks/write_vs_write_nonblock.rb
bunny-2.14.1 benchmarks/write_vs_write_nonblock.rb
bunny-2.13.0 benchmarks/write_vs_write_nonblock.rb
bunny-2.12.1 benchmarks/write_vs_write_nonblock.rb
bunny-2.12.0 benchmarks/write_vs_write_nonblock.rb
bunny-2.12.0.rc1 benchmarks/write_vs_write_nonblock.rb
bunny-2.11.0 benchmarks/write_vs_write_nonblock.rb
bunny-2.11.0.pre1 benchmarks/write_vs_write_nonblock.rb
bunny-2.10.0 benchmarks/write_vs_write_nonblock.rb
bunny-2.9.2 benchmarks/write_vs_write_nonblock.rb
bunny-2.9.1 benchmarks/write_vs_write_nonblock.rb
bunny-2.6.7 benchmarks/write_vs_write_nonblock.rb
bunny-2.7.4 benchmarks/write_vs_write_nonblock.rb
bunny-2.8.1 benchmarks/write_vs_write_nonblock.rb
bunny-2.9.0 benchmarks/write_vs_write_nonblock.rb