[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / benchmark / io_copy_stream_write.rb
blob3fd87250a4a9135b0905170a1af267e260033700
1 # The goal of this is to use a synthetic (non-IO) reader
2 # to trigger the read/write loop of IO.copy_stream,
3 # bypassing in-kernel mechanisms like sendfile for zero copy,
4 # so we wrap the /dev/zero IO object:
6 class Zero
7   def initialize
8     @n = 100000
9     @in = File.open('/dev/zero', 'rb')
10   end
12   def read(len, buf)
13     return if (@n -= 1) == 0
14     @in.read(len, buf)
15   end
16 end
18 begin
19   src = Zero.new
20   dst = File.open(IO::NULL, 'wb')
21   n = IO.copy_stream(src, dst)
22 rescue Errno::ENOENT
23   # not *nix
24 end if IO.respond_to?(:copy_stream) && IO.const_defined?(:NULL)