dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] another benchmark for IO.copy_stream socket writing
@ 2017-01-17  0:03 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2017-01-17  0:03 UTC (permalink / raw)
  To: spew

Workaround for https://bugs.ruby-lang.org/issues/13085
---
 benchmark/bm_io_copy_stream_write_socket.rb | 36 +++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 benchmark/bm_io_copy_stream_write_socket.rb

diff --git a/benchmark/bm_io_copy_stream_write_socket.rb b/benchmark/bm_io_copy_stream_write_socket.rb
new file mode 100644
index 0000000000..d2c33b3ec2
--- /dev/null
+++ b/benchmark/bm_io_copy_stream_write_socket.rb
@@ -0,0 +1,36 @@
+require 'socket'
+
+# The goal of this is to use a synthetic (non-IO) reader
+# to trigger the read/write loop of IO.copy_stream,
+# bypassing in-kernel mechanisms like sendfile for zero copy,
+# so we wrap the /dev/zero IO object:
+class Zero
+  def initialize
+    @n = 100000
+    @in = File.open('/dev/zero', 'rb')
+  end
+
+  def read(len, buf)
+    return if (@n -= 1) == 0
+    @in.read(len, buf)
+  end
+end
+
+begin
+  src = Zero.new
+  rd, wr = UNIXSocket.pair
+  pid = fork do
+    wr.close
+    buf = ''.b
+    while rd.read(16384, buf)
+    end
+  end
+  rd.close
+  IO.copy_stream(src, wr)
+rescue Errno::ENOENT
+  # not *nix
+ensure
+  wr.close if wr
+  Process.waitpid(pid) if pid
+end if IO.respond_to?(:copy_stream) && RUBY_PLATFORM =~ /linux/ &&
+    Socket.const_defined?(:MSG_DONTWAIT)
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH] another benchmark for IO.copy_stream socket writing
@ 2017-01-18 18:20 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2017-01-18 18:20 UTC (permalink / raw)
  To: spew

Workaround for https://bugs.ruby-lang.org/issues/13085
---
 benchmark/bm_io_copy_stream_write_socket.rb | 35 +++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
 create mode 100644 benchmark/bm_io_copy_stream_write_socket.rb

diff --git a/benchmark/bm_io_copy_stream_write_socket.rb b/benchmark/bm_io_copy_stream_write_socket.rb
new file mode 100644
index 0000000000..11f369bd0d
--- /dev/null
+++ b/benchmark/bm_io_copy_stream_write_socket.rb
@@ -0,0 +1,35 @@
+# The goal of this is to use a synthetic (non-IO) reader
+# to trigger the read/write loop of IO.copy_stream,
+# bypassing in-kernel mechanisms like sendfile for zero copy,
+# so we wrap the /dev/zero IO object:
+class Zero
+  def initialize
+    @n = 100000
+    @in = File.open('/dev/zero', 'rb')
+  end
+
+  def read(len, buf)
+    return if (@n -= 1) == 0
+    @in.read(len, buf)
+  end
+end
+
+begin
+  require 'socket'
+  src = Zero.new
+  rd, wr = UNIXSocket.pair
+  pid = fork do
+    wr.close
+    buf = String.new
+    while rd.read(16384, buf)
+    end
+  end
+  rd.close
+  IO.copy_stream(src, wr)
+rescue Errno::ENOENT, NotImplementedError, NameError
+  # not *nix: missing /dev/zero, fork, or UNIXSocket
+rescue LoadError # no socket?
+ensure
+  wr.close if wr
+  Process.waitpid(pid) if pid
+end if IO.respond_to?(:copy_stream)
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-01-18 18:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-17  0:03 [PATCH] another benchmark for IO.copy_stream socket writing Eric Wong
  -- strict thread matches above, loose matches on Subject: below --
2017-01-18 18:20 Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).